Reputation: 33
I have zero experience with writing batch files but my goal does not seem difficult. I'm trying to write a small script that will prompt a user to enter an ID number and then search a directory of .jpgs and copy any pictures containing the ID number to a separate directory. The .jpgs are names xxxxxx_zzz.jpg where xxxxxx is the ID number up to 6 digits and zzz is the sequence number. For example, one ID can have many pictures: 123456_001.jpg, 123456_002.jpg, 123456_003.jpg, etc.
I've played with several IF
and GOTO
commands, but I seem to be diverging from my goal. I know the two IF
statements I currently am using below are garbage, but I left them in the syntax to convey the direction I think I should be moving in.
I can't get the IF
statements to use the GOTO
commands. I also don't think I fully understand how labels are supposed to work.
@ECHO OFF
CLS
:START
DEL /q C:\Users\ADMIN\Desktop\temp\*.*
:GETINPUT
set /p propkey=Enter the Property Key of pictures to retrieve.:
IF EXIST C:\Users\ADMIN\Desktop\photos\*%propkey%_???.jpg GOTO Success
IF "%propkey%"=="" ECHO No Property Key was entered. GOTO START
:Success
ECHO Pictures from Property Key %propkey% will now be moved to the Temp folder on the Desktop. && PAUSE
COPY "C:\Users\ADMIN\Desktop\photos\*%propkey%*.jpg" "C:\Users\ADMIN\Desktop\temp\"
START C:\Users\ADMIN\Desktop\temp\
:END
Upvotes: 3
Views: 25205
Reputation: 41234
Your code was very close. It needed a goto :eof
to stop it falling through to the main routine. Change &&
to &
and an &
after the echo statement was missing. I changed the goto start
to the next label, to avoid a harmless error message.
@ECHO OFF
CLS
:START
DEL /q C:\Users\ADMIN\Desktop\temp\*.*
:GETINPUT
set /p propkey=Enter the Property Key of pictures to retrieve.:
IF EXIST "C:\Users\ADMIN\Desktop\photos\*%propkey%_???.jpg" GOTO Success
IF "%propkey%"=="" ECHO No Property Key was entered. & GOTO GETINPUT
goto :EOF
:Success
ECHO Pictures from Property Key %propkey% will now be moved to the Temp folder on the Desktop. & PAUSE
COPY "C:\Users\ADMIN\Desktop\photos\*%propkey%*.jpg" "C:\Users\ADMIN\Desktop\temp\"
start "" "C:\Users\ADMIN\Desktop\temp"
:END
Upvotes: 0
Reputation: 2754
Try this:
:Start
@echo off
cls
:: store source folder
set _sd=C:\Users\ADMIN\Desktop\photos
:: store target folder
set _td=C:\Users\ADMIN\Desktop\temp
:CheckFolders
if not exist %_sd% echo Source folder not found.&goto End
if not exist %_td% echo Target folder not found.&goto End
:: wipe target folder
del /q %_td%\*.*
:GetKey
set /p _key=Enter the property key:
if '%_key'=='' echo No property key entered. Please retry.&goto GetKey
if not exist "%_sd%\*%_key%*.jpg" echo No photos matched the key.&goto End
echo Copying pictures to %_td%...
copy "%_sd%\*%_key%*.jpg" "%_td%"
:OpenFolder
start "%windir%\explorer.exe" "%_td%"
:End
A label is a way to arbitrarily designate a line in a batch file. It is used by the GOTO
command to change the usual top-to-bottom progression of command execution, specifying which line should be processed next. As you probably figured out, using IF
commands in unison with GOTO
allows for conditional processing, such as when values are met or errors are encountered.
Another use of a label could be for documentation or clarity. In my example above, "CheckFolders" isn't used by a GOTO
, but it lets the programmer hint at what that section of code does.
Upvotes: 4
Reputation: 67216
Although wild-cards do works in if exist
, there are certain combinations that may fail. Also, you must enclose the name in quotes if the path may contain spaces. Try:
IF EXIST "C:\Users\ADMIN\Desktop\photos\*%propkey%_*.jpg" GOTO Success
IF "%propkey%"=="" ECHO No Property Key was entered. & GOTO START
Upvotes: 1