How do I stop my script overwriting? - batch-file

Below is a batch script that I have put together which copies the content of another folder and pastes it into a new folder that it creates called IC_Transfer.
#echo off
#break off
#title IC Transfer
setlocal EnableDelayedExpansion
if not exist "C:\Temp\IC_Transfer" (
md "C:\Temp\IC_Transfer"
ROBOCOPY /-y "Q:\Work\Temp\Dan" "C:\Temp\IC_Transfer" /mir
if "!errorlevel!" EQU "0" (
echo Transfer Successful! )
) else (
if exist "C:\Temp\IC_Transfer" (
echo Error Transferring File
echo File Already Exists
:Choice
set /P c=Would You Like To Overwrite[Y/N]?
if /I "%c%" EQU "Y" goto Overwrite
if /I "%c%" EQU "N" goto Stop
) )
:Overwrite
md "C:\Temp\IC_Transfer"
ROBOCOPY "Q:\Work\Temp\Dan" "C:\Temp\IC_Transfer" /mir
if "!errorlevel!" EQU "0" (
echo Transfer Successful )
goto End
:Stop
echo Transfer Cancelled
goto End
:End
pause
exit
pause
exit
The make directory and robocopy functions work as they should by purging the directory, re-creates it and pastes the contents. What I cannot get working is the choice command.
Regardless of whether I choose Y or N it overwrites the file contents.
I'm new to batch scripts so any help would be appreciated

#echo off
#break off
#title IC Transfer
setlocal
if not exist "C:\Temp\IC_Transfer" (
md "C:\Temp\IC_Transfer"
ROBOCOPY /-y "Q:\Work\Temp\Dan" "C:\Temp\IC_Transfer" /mir
if not errorlevel 1 (
echo Transfer Successful!
) else (
echo Error Transferring File
echo File Already Exists
call :Choice
)
)
exit /b
:Choice
setlocal
set "c="
set /P "c=Would You Like To Overwrite[Y/N]? "
if /I "%c%" == "Y" (
call :Overwrite
) else if /I "%c%" == "N" (
call :Stop
) else call :Stop
exit /b
:Overwrite
ROBOCOPY "Q:\Work\Temp\Dan" "C:\Temp\IC_Transfer" /mir
if not errorlevel 1 echo Transfer Successful
pause
exit /b
:Stop
echo Transfer Cancelled
pause
exit /b
To fix the issue, labels inside parenthese code blocks is a
bad idea and prone to error. So call the label instead.
I replaced the goto's with calls as :Choice will work with a call.
This makes :End obsolete.
You used delayed expansion for errorlevel, though you could just use
i.e. if not errorlevel 1 to check if integer value is less than 1.
Delayed expansion has been removed.

Labels within code blocks (parenthesised series of commands) cause problems (:choice) and are better regarded as illegal.
You have invoked delayedexpansion and recognised that errorlevel may change with the code block. Your set /p changes the variable c, but you are using the parse-time value of c (%c%) in your if statements, not the run-time value (!c!)
What happens should the user enter not-YN? Hint: See the choice command - choice /? from the prompt.
Since c is not set at the start of the code, "%c%" will evaluate to "" which is neither "Y" nor "N" so the ifs fail and the code will then proceed to the next statement (:overwrite)
Note that md will create a directory or generate an error message if it does not exist, hence
md directoryname 2>nul
should create the directory and suppress error messages (like 'directory already exists') obviating the test-for-existence gating the md.

Related

Why isn't error triggered IF/THEN working correctly?

Coding is just something I like to play with so I have limited knowledge and research hasn't helped. If I run the script I get both success and error messages instead of one or the other.
#ECHO OFF
SET /p HOSTFILE=Host File Name?&CLS
SET /p HIDEFILE=File To Hide?&CLS
SET /p OUTPUT=Output File Name?&CLS
SET /p EXTENSION=Output File Extension (.Zip, .Jpg, .M4A)?&CLS
COPY /b "%HOSTFILE%" + "%HIDEFILE%" %OUTPUT%%EXTENSION% >null
if %ERRORLEVEL% EQU 0 GOTO continue
if %ERRORLEVEL% EQU 2 GOTO error
:continue
ECHO %OUTPUT%%EXTENSION% Created.
:error
ECHO File Not Created.
TIMEOUT /T 2 >null
As Compo said in the comment you should probably check if your files exist.
when both hostfile and hidefile exist, a success message is indicated otherwise error message follows.
#ECHO OFF
SET /p HOSTFILE=Host File Name?&CLS
SET /p HIDEFILE=File To Hide?&CLS
SET /p OUTPUT=Output File Name?&CLS
SET /p EXTENSION=Output File Extension (.Zip, .Jpg, .M4A)?&CLS
IF EXIST "%HOSTFILE%" IF EXIST "%HIDEFILE%" (
echo exist hidefile
COPY /b "%HOSTFILE%" + "%HIDEFILE%" %OUTPUT%%EXTENSION% > nul
GOTO continue
)
) ELSE (
echo does not exist
GOTO error
)
:continue
ECHO %OUTPUT%%EXTENSION% Created.
GOTO timer
exit /b 0
:error
ECHO File Not Created.
GOTO timer
exit /b 0
:timer
TIMEOUT /T 2 > nul
exit /b 0

check for spaces in file name inside of for loop in batch file

I have a batch script that is calling a VBscript file. It reiterates through all files in a watched folder.
It needs to verify if the file name has spaces in it and if so reject the file and not process it with the VBS.
I must have an error on the code as I get the error message:
ELSE was not expected at this time.
I have looked at the documentation and searched for the answer for quite some time, including this question: check "IF" condition inside FOR loop (batch/cmd)
But still, I can't see what is wrong in my syntax:
#ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
call :ReadIni Infolder inFolder
call :ReadIni Outfolder outFolder
echo %inFolder%
echo %outFolder%
pause
:StartLoop
FOR %%I in (%inFolder%\*.srt) DO (
ECHO %%I
ECHO %%~nxI
SET TESTNAME=%%~nxI
ECHO !TESTNAME!
ECHO !TESTNAME: =_!
PAUSE
IF NOT !TESTNAME!==!TESTNAME: =_! (
move "%~sdp0%%~nxI" "%outFolder%\ERROR_IN_FILE_NAME_%%~nxI"
) ELSE (
copy /y "%%I" "%~sdp0%%~nxI"
%~sdp0SRToffset.vbs "%~sdp0%%~nxI" "%~sdp0%%~nxI"
IF %ERRORLEVEL%==1 (
Goto StartLoop
) else (
move "%~sdp0%%~nxI" "%outFolder%\"
move "%~sdp0QC_%%~nxI" "%outFolder%\"
del "%%I"
)
)
)
timeout /t 1
goto StartLoop
:ReadIni
FOR /F "tokens=2 delims==" %%a in ('find "%~1=" config.ini') do set %~2=%%a
exit /b
Any help would be appreciated.
IF NOT "!TESTNAME!"=="!TESTNAME: =_!" (
...
IF %ERRORLEVEL%==1 (
Quoting the strings causes cmd to regard the string as a single entity.
Note that the following if %errorlevel% will be executed using the value of errorlevel at :startloop. (See delayed expansion for reasoning.)
Cure by using if !errorlevel!==1 (. (Using the runtime value of errorlevel as established by the vbs routine.)

My batch script doesn't use if command correctly

I've spent a few days trying to get this batch script to work, but it just does not seem to work properly. It seems to just do whatever it wants after it prompts me to set a variable and i set it.
For example, I might enter n when it says that it doesn't seem to exist, and it will just end the script like it should. But if I re-open it, and it says the same thing as before, and I enter n again, it might just jump to :DeleteCalc, as if I typed y.
Here's my script:
#echo off
:Begin
color fc
title My script
cls
if not exist "C:\calc.exe" (
echo calc.exe doesn't seem to exist. Attempt deletion anyway? ^(Y/N^)
set "calcnotexist="
set /p "calcnotexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal EnableDelayedExpansion
if not !calcnotexist!==!calcnotexist:^"=! set "calcnotexist="
endlocal & if "%calcnotexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto Begin
)
if /i "%calcnotexist%"=="Y" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="Yes" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="N" goto End
if /i "%calcnotexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto Begin
)
:calcDoesExist
title My script
cls
echo calc.exe found. Delete? ^(Y/N^)
set "calcexist="
set /p "calcexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal enabledelayedexpansion
if not !calcexist!==!calcexist:^"=! set "calcexist="
endlocal & if "%calcexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto calcDoesExist
)
if /i "%calcexist%"=="Y" goto DeleteCalc
if /i "%calcexist%"=="Yes" goto DeleteCalc
if /i "%calcexist%"=="N" goto End
if /i "%calcexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto calcDoesExist
:DeleteCalc
cls
echo Deleting...
if not exist C:\calc.exe goto Success
del /f /q C:\calc.exe >nul 2>nul
if not exist C:\calc.exe goto Success
echo Fail!
echo.
echo calc.exe could not be deleted.
echo.
pause
goto End
:Success
echo Deleted!
echo.
echo calc.exe successfully deleted.
echo.
pause
goto End
:End
exit /b
What could I possibly be doing wrong?
Thanks
P.S. I tested this by opening CMD and running the batch script multiple times in there. (but it also doesn't work right when just double clicking it)
If you restructure your script there will be no need for the extended If blocks and therefore no necessity to EnableDelayedExpansion. Also if you use Choice you will not have to do all of the verification of responses.
Example:
#Echo Off
Title My script
Color FC
:Begin
If Exist "C:\calc.exe" GoTo calcDoesExist
Echo(calc.exe doesn't seem to exist.
Choice /M "Attempt deletion anyway"
If ErrorLevel 3 (ClS & GoTo Begin)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo Success
GoTo End
:calcDoesExist
Echo(calc.exe found.
Choice /M "Delete"
If ErrorLevel 3 (ClS & GoTo calcDoesExist)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo DeleteCalc
GoTo End
:DeleteCalc
ClS
Echo(Deleting...
Del /A /F "C:\calc.exe">Nul 2>&1
If Not Exist "C:\calc.exe" GoTo Success
Echo(Fail!
Echo(
Echo(calc.exe could not be deleted.
GoTo End
:Success
Echo(
Echo(Deleted!
Echo(
Echo(calc.exe does not exist.
:End
Echo(
Echo(Exiting...
Timeout 3 /NoBreak>Nul
Exit /B

Delete duplicate files using Windows command line

Given two directories c:\foo and c:\bar I want to delete the files in c:\bar that are identical to files present in c:\foo. I can use the fc command to compare each file in c:\bar with a file of the same name in c:\foo and delete duplicates manually. Is there a simple way to automate this using CMD?
If identical means similar or alike in every way: in every way, not only in date and size, therefore forced binary comparison:
#ECHO OFF >NUL
SETLOCAL enableextensions
pushd "D:\bat\FooBar"
for /F "delims=" %%G in ('dir /B /A:-D *.*') do (
call :proFC "%%~fG" "D:\bat\FooFoo\%%~nxG"
)
popd
ENDLOCAL
goto :eof
:raiseerror
exit /B %1
:proFC
call :raiseerror 321
fc /B "%~1" "%~2" >NUL 2>&1
if %errorlevel% EQU 0 (
echo del "%~1"
) else (
echo %errorlevel% "%~2"
)
goto :eof
Commented crucial points in the above script:
pushd ... switches current working directory
for /F ... loop treats static file list of the Bar folder
call :proFC ... with properly quoted line arguments parameters
popd switches current working directory back
goto :eof ends the script
:raiseerror subroutine returns exit code via exit /B %1
:proFC productive subroutine
call :raiseerror 321 important as For an invalid switch (with two passed files) an error message is printed but the errorlevel is not changed
fc /B "%~1" "%~2" >NUL 2>&1 output and error messages redirected to NUL as the errorlevel is important;
if %errorlevel% EQU 0 (
echo del "%~1" a file delete merely _echo_ed for debugging purposes
) else (
echo %errorlevel% "%~2" for debugging purposes (see below).
goto :eof returns from the subroutine
FC will set an ErrorLevel as follows (but see a note at the call :raiseerror 321 point):
-1 Invalid syntax (e.g. only one file passed)
0 The files are identical.
1 The files are different.
2 Cannot find at least one of the files.
#echo off
cd c:\bar
for %%a in (*.*) do for %%b in ("c:\foo\%%a") do (
if exist "%%b" (
if "%%~Ta %%~Za" equ "%%~Tb %%~Zb" (
del "%%a"
) else (
fc "%%a" "%%b" > NUL
if not errorlevel 1 del "%%a"
)
)
)
If two files may have different modification dates but be equal, remove the %%~T.. parts in the comparison.

How can I set a batch file to find a file and show its emplacement?

I need this for a basic program. What i can do so far is track if a predefined file exists, but not much more.
I'm trying to do this:
Select a file
See if it exists
Find the path to it
save the path under a variable, or something like that
Show the path to the user
(Execute the file, optional)
Is there a way to do it, and what is the script?
Please tell me if i am not clear.
Thanks in advance
#echo off
setLocal
set /p file=choose a file:
set /p option=do you want to execute the file?[y/n]
if "%option%" equ "y" set /p directory=directory where you want to execute the file:
if "%option%" equ "Y" set /p directory=directory where you want to execute the file:
if not exist "%directory%\" echo directory does not exist %% exit /b 3
if not exist %file% echo file does not exists && exit /b 1
for %%F in (%file%) do (
set file_atr=%%F
set full_path=%%~fF
)
if "%file_atr::~0,1%" equ "d" echo this is a directory && exit /b 2
echo the full path is : %full_path%
if "%option%" equ "y" (
pushd %directory%
call %full_path%
popd
)
if "%option%" equ "Y" (
pushd %directory%
call %full_path%
popd
)
endLocal

Resources