this is my very first post on this site but as I already found much help on stackoverflow, I now ask this question myself. I did not find an answer to this issue. My English isn't that good and as my Problem is quite complex, I hope you guys are all able to understand me. If not or if there are any questions that are needed to be able to help me, feel free to ask.
So I want to make a Batch file that moves files by the Extension (in my case, it's .pdf) but not all of them. I know there is this command:
move "Y:\Scan\*.pdf" "Y:\Scan\Archive\"
But this will move all pdf files to the Archive Folder and I don't want that. To get a better overview what my plan is, a small description is needed I guess. Imagine you have a home Server and your Computer is connected to it, a Printer and a scanner aswell. I have many documents in Folders in my home Office and I want to have them all on my Server. This is done trough scanning all Sheets of paper in These Folders. Everytime I scan something, this is saved as .pdf in a Folder on my local Computer, sorted by time and Looks like this: 2017-06-15-10-30.pdf
My current script is asking 3 times for userinput. One time for a village Name, one time for a street's Name and one time for the number of the house because the Folders for the Archive should be named like the corresponding house they are referring to. So I have These 3 Inputs in my Batch already and also the whole Folder Name saved in a new variable.
My plan is to now ask the user how many pdf's are corresponding to the last created Folder and whatever the user is typing, this amount of files will be stored in that Folder. To understand this a Little better, here is what i've done so far (my Batch code).
@echo off
cls
title Archive-Script (by Relentless)
color 0b
:reset
cls
REM This is the village's name
SET ON=""
REM This is the street's name
SET SN=""
REM This is the number of the house
SET HN=""
REM This is a variable to store whether the user wants to reset the variables or not (used later)
SET RESET=""
:start
cls
IF %RESET% == N echo To overwrite the village's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the village's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %ON%
set /p "ON="
echo The village's name was set to %ON%!
echo.
IF %RESET% == N echo To overwrite the street's name, please enter the name again and hit enter. If you don't want to overwrite the name, just hit enter!
IF NOT %RESET% == N echo Please enter the street's name (confirm by pressing enter):
IF %RESET% == N echo Current name: %SN%
set /p "SN="
echo The street's name was set to %SN%!
echo.
IF %RESET% == N echo To overwrite the house's number, please enter the number again and hit enter. If you don't want to overwrite the number, just hit enter!
IF NOT %RESET% == N echo Please enter the house's number (confirm by pressing enter):
IF %RESET% == N echo Current number: %HN%
set /p "HN="
echo The house's number was set to %HN%!
echo.
echo.
echo.
echo.
echo.
SET FOLDERNAME="%ON%, %SN% %HN%"
echo The folder %FOLDERNAME% will now be created!
mkdir ""%FOLDERNAME%""
timeout /t 2 /nobreak
:pdfmove
cls
echo How many pdf-files are corresponding to the latest created folder?
set /p "PDF="
for /l %%x in (1,1,%PDF%) do (
echo %%x
move Y:\Scan\*.pdf Y:\Scan\%FOLDERNAME%\
)
:resetoption
cls
echo %ON%
echo %SN%
echo %HN%
echo.
echo Should the variables be resetted again? (J/N)
set /p "RESET="
IF %RESET% == J goto reset
IF %RESET% == N goto start
As you can see, I already tried to implement the move Option, but this will move ALL pdf files instead of one on each for Loop. Can anyone help me with that? I guess the code is pretty simple to understand.
Greetz Relentless
for /l %%x in (1,1,%PDF%) do (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
move "Y:\Scan\%%a" Y:\Scan\%FOLDERNAME%\
)
)
I believe what you want to do is to move a user-input number of pdfs.
The above change will, for each %%x
, first set the flag moved
to nothing, then perform a directory-scan, in /b
basic form (filename only) /a-d
with no directorynames. Each of the filenames present in the directory will be assigned to %%a
in turn. The delims=
option allows filenames that contain spaces to be processed.
When the first file is found, its name is echo
ed with the sequence number and the flag is set
to Y
(any non-empty string will do). The file is then moved.
When the for...%%a...
processes the next filename, moved
is now set to a value, so no more file-moves are performed.
The entire process is then repeated for each value of %%x
, so %PDF%
moves of the first .pdf
filename found in the source directory will be moved because one is moved each time and the dir
command will not find the file that was last moved because well - it's been moved and hence is no longer in the source directory.
OK, here's a fix to produce a variable number of spaces
SET "tspaces=T"
:addspace
SET "tspaces=%tspaces% "
CALL SET "testspace=%%tspaces:~%pdf%%%"
IF "%testspace%" neq " " GOTO addspace
FOR /L %%x IN (1,1,%pdf%) DO (
set "moved="
for /f "delims=" %%a in ('dir /b /a-d "y:\scan\*.pdf" '
) do if not defined moved (
echo %%x : %%a
set "moved=Y"
call move "Y:\Scan\%%a" "Y:\Scan\%FOLDERNAME%\olddocumen%%tspaces:~0,%%x%%.pdf"
)
)
The first part sets up tspaces
to t
+a sufficient number of spaces.
Note that in the second part, the move
is now call
ed which resolves the variable-substring. Since %%x
varies from 1 to %pdf%
then the selection from tspaces
is %%x
characters - it has a minimum of 1, when the final character of olddocument
is required; thereafter a number of spaces follow, so the t
is the initial value of tspaces
.