Reputation: 149
I have created a script to copy a file to a directory, yet prompt you if the file already exists. this current code doesn't work as intended if yes is selected, but the file already exists and then no input is given at the second prompt. Meaning in the exist
section it doesn't default to the no option when the timeout time is over.
:: This is a script to move downloaded files into a folder titled with todays date
@echo ---------------------------------
@echo ARE YOU SURE YOU WANT TO PROCEED?
@echo ---------------------------------
@echo off
::present user with Yes/No option
choice /C YN /M "Press Y for Yes, N for No." /T 10 /D N
if "%errorlevel%"=="1" goto:yes
if "%errorlevel%"=="2" goto:no
:yes
if exist "%date:/=_%\cE0MB.m4a" (
goto EXIST
) else (goto MOVE)
:MOVE
if exist cE0MB.m4a (
md "%date:/=_%"
move cE0MB.m4a "%date:/=_%\cE0MB.m4a"
) else (echo FILES NOT DOWNLOADED, PLEASE DOWNLOAD THEN RUN SCRIPT AGAIN)
if exist "%date:/=_%\cE0MB.m4a" echo FILE SUCCESSFULLY COPIED TO DIRECTORY
TIMEOUT /T 5 >nul
exit
:EXIST
if exist cE0MB.m4a (
choice /C YN /M "Files already exist, overwrite?" /T 5 /D N
if "%errorlevel%"=="1" goto:move
if "%errorlevel%"=="2" goto:no
) else (echo NOTHING TO COPY, CLOSING...)
TIMEOUT /T 3 >nul
exit
:no
echo YOU HAVE SELECTED NO OR WAITED TOO LONG TO RESPOND
echo PRESS ANY KEY OR WAIT TO EXIT
TIMEOUT /T 5 >nul
exit
however if i modify the exist
section to the following, it works as intended
:EXIST
if exist cE0MB.m4a (
choice /C NY /M "Files already exist, overwrite?" /T 5 /D N
if "%errorlevel%"=="1" goto:no
if "%errorlevel%"=="2" goto:move
) else (echo NOTHING TO COPY, CLOSING...)
TIMEOUT /T 3 >nul
exit
Can anybody tell me why? I don't see any obvious conflicts.
Upvotes: 0
Views: 483
Reputation:
To answer the question directly. choice
sets a value based on the position of choices, because setting values inside of a code block requires delayedexpansion
you need to do the same when running choice inside of a code block. Currently, when you enter the code block with the previously defined errorlevel
it will remain 1
inside of the loop. So simply do:
:EXIST
if exist cE0MB.m4a (
setlocal enabledelayedexpansion
choice /C YN /M "Files already exist, overwrite?" /T 5 /D N
if "!errorlevel!"=="1" goto:move
if "!errorlevel!"=="2" goto:no
endlocal
) else (
echo NOTHING TO COPY, CLOSING...
)
TIMEOUT /T 3 >nul
Upvotes: 2