bat file is running "if" check before getting input parameter - batch-file

Put this in a file called "mybat.bat"
(put it in c:\temp\
do NOT put it in c:\windows)
set __rightFolder=.
del "%__rightFolder%\system.ini.copy"
If NOT exist "%__rightFolder%\system.ini.copy" (
copy "%windir%\system.ini" "%__rightFolder%\system.ini.copy"
ECHO "%__rightFolder%\system.ini.copy"
SET /P AREYOUSURE="Do you want to run the dir command on '%__rightFolder%\system.ini.copy'"
IF /I "%AREYOUSURE%" EQU "Y" (
dir "%__rightFolder%\system.ini.copy"
)
)
PAUSE
When I run this simple script, the "IF /I" is running before I type anything in.
NOTE, I have a long question with quotes around the question.
It's probably something simple, but I'm .bat skillzz stink.
Here is my output.
C:\Temp>set __rightFolder=.
C:\Temp>del ".\system.ini.copy"
Could Not Find C:\Temp\system.ini.copy
C:\Temp>If NOT exist ".\system.ini.copy" (
copy "C:\Windows\system.ini" ".\system.ini.copy"
ECHO ".\system.ini.copy"
SET /P AREYOUSURE="Do you want to run the dir command on '.\system.ini.copy'"
IF /I "" EQU "Y" (dir ".\system.ini.copy" )
)
1 file(s) copied.
".\system.ini.copy"
Do you want to run the dir command on '".\system.ini.copy"'y
C:\Temp>PAUSE
Press any key to continue . . .
So I put in 'y', but it doesn't do the "dir" command.

Neverending story... EnableDelayedExpansion
Delayed Expansion will cause variables to be expanded at execution
time rather than at parse time, this option is turned on with the
SETLOCAL command. When delayed expansion is in effect variables may
be referenced using !variable_name! (in addition to the normal
%variable_name% )
Patched:
set __rightFolder=.
del "%__rightFolder%\system.ini.copy"
If NOT exist "%__rightFolder%\system.ini.copy" (
copy "%windir%\system.ini" "%__rightFolder%\system.ini.copy"
ECHO "%__rightFolder%\system.ini.copy"
SET /P "AREYOUSURE=Do you want to run the dir command on '%__rightFolder%\system.ini.copy' "
SETLOCAL enabledelayedexpansion
IF /I "!AREYOUSURE!" EQU "Y" (
dir "%__rightFolder%\system.ini.copy"
)
ENDLOCAL
)
PAUSE

Related

How to use a batch file to loop through several executables files to compress them (.7z) and rename the file to avoid ".exe.7z"

I'm trying to zip up 27 exe files in one go using a batch file. The purpose of this file is to create .7z files for a modified HBCD menu. This file is located in, ...\Documents\CD\HBCD\Programs the exe files are in ...\Documents\CD\HBCD\Programs\Files\SysinternalsSuite
I keep getting:
Error:
Incorrect command line
I keep searching around and trying different things but I can't get it to work.
File Name: 7zBatch.bat
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
pushd "%~dp0"
echo "-------------------------------------------------"
echo "||======== BATCH ZIPPER (.7z) for HBCD ========||"
echo "-------------------------------------------------"
:AskSource
echo Enter the location of the exe file(s)
set SOURCE=
set /p SOURCE=">>" %=%
echo "%SOURCE%", is that correct & goto ConfirmSource
:ConfirmSource
echo (Y)es or (N)o?
set INPUT=
set /p INPUT=">>" %=%
if /I "%INPUT%"=="y" goto AskDest
if /I "%INPUT%"=="n" goto AskSource
echo Incorrect input, is the location correct? & goto ConfirmSource
:AskDest
cd %SOURCE%
echo Enter the location of the zip file(s)
set DEST=
set /p DEST=">>" %=%
echo "%DEST%", is that correct & goto ConfirmDest
:ConfirmDest
echo (Y)es or (N)o?
set INPUT=
set /p INPUT=">>" %=%
if /I "%INPUT%"=="y" goto Execute
if /I "%INPUT%"=="n" goto AskDest
echo Incorrect input, is the location correct? & goto ConfirmDest
:Execute
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO (
set fname=%%G
set nfname=!fname:.exe=!
echo.
echo Found: !fname!
..\..\7z.exe a -t7z !nfname!.7z "!fname!" -mx5 -sdel -oC:%DEST%
timeout /t 10 /nobreak
)
ENDLOCAL
echo.
pause
Once it gets to the 7 Zip line it gives the error. I realized that the nfname variable was empty so I tried:
set nfname=!%%G:.exe=!
That didn't work so I tried deleting that and going with:
..\..\7z.exe a -t7z !fname:.exe=!.7z "!fname!" -mx5 -sdel -oC:%DEST%
That didn't work so I tried putting different things in quotes one by one and then everything:
"..\..\7z.exe" a -t7z "!fname:.exe=!.7z" "!fname!" -mx5 -sdel -oC:"%DEST%"
Still nothing. What am I missing?
#echo off
SETLOCAL
echo "-------------------------------------------------"
echo "||======== BATCH ZIPPER (.7z) for HBCD ========||"
echo "-------------------------------------------------"
:AskSource
echo Enter the location of the exe file(s)
set "SOURCE="
set /p "SOURCE=>>"
echo "%SOURCE%", is that correct
:ConfirmSource
echo (Y)es or (N)o?
set "INPUT="
set /p "INPUT=>>"
if /I "%INPUT%"=="y" goto AskDest
if /I "%INPUT%"=="n" goto AskSource
echo Incorrect input, is the location correct?
goto ConfirmSource
:AskDest
echo Enter the location of the zip file(s)
set "DEST="
set /p "DEST=>>"
echo "%DEST%", is that correct
:ConfirmDest
echo (Y)es or (N)o?
set "INPUT="
set /p "INPUT=>>"
if /I "%INPUT%"=="y" goto Execute
if /I "%INPUT%"=="n" goto AskDest
echo Incorrect input, is the location correct?
goto ConfirmDest
:Execute
FOR %%G IN ("%SOURCE%\*.exe") DO (
"%~dp0..\..\7z.exe" a -t7z "%DEST%\%%~nG.7z" "%%~fG" -mx5 -sdel
)
ENDLOCAL
echo.
pause
The pushd and cd removed. Unsure why 7z.exe would be
located 2 directories up from where the input executable
directory is located. 7z.exe is now located 2 directories
up from the script directory.
The -o switch for 7z.exe is only for extraction as
mentioned in the help file so is omitted.
Double quoted set of variables to avoid any trailing spaces.
The for loop changed to simple type as it gives the file
paths required without need of using dir.
EnableDelayedExpansion removed as the for variables
uses modifiers to get fullpath, name, drive etc. See
for /? about modifiers available.
Removed timeout as it complains if stdin is used and see
no reason to use it for this task.
Removed obsolete goto commands that go to the next line
with a command.

How do I stop my script overwriting?

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.

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.)

Batch file perform task in directories recursively when the directory name contains "&"

I made a batch program to perform some simple tasks in directories recursively and it usually works without problems. but if the directory name contains "&", I got an error.
#echo off
setlocal enableextensions disabledelayedexpansion
set "exitCode=0"
set "BaseDir=%~1"
if not defined BaseDir set "BaseDir=%CD%"
for %%f in ("%BaseDir%") do set "BaseDir=%%~ff"
if not exist "%BaseDir%" (
call :error "Base directory does not exist"
goto endProcess
)
call :recursive "%BaseDir%"
if errorlevel 1 set "exitCode=1"
goto :endProcess
:Whattodo
echo %CD%
goto :eof
:recursive BaseDir
setlocal
set "BaseDir=%~f1"
pushd "%BaseDir%"
for /d %%d in (*) do if not errorlevel 1 call :recursive "%%~fd"
call :Whattodo
popd
endlocal
goto :eof
:error
echo(%~1
set "exitCode=1" & cmd /d /q /c exit /b 1
goto :eof
:endProcess
endlocal & exit /b %exitCode%
I will get a output like this:
C:\tmp\01\04
C:\tmp\01\05
C:\tmp\01
C:\tmp\02\06
C:\tmp\02\07\08
C:\tmp\02\07
C:\tmp\02
C:\tmp\03
C:\tmp
but if I have a directory like this "C:\tmp\02\06 & a" I will have this output
C:\tmp\01\04
C:\tmp\01\05
C:\tmp\01
C:\tmp\02\06
'a' is not recognized as an internal or external command,
C:\tmp\02
C:\tmp
I understand that the problem is in the line below, but I don't know how to solve it.
for /d %%d in (*) do if not errorlevel 1 call :recursive "%%~fd"
!! edit
Before I posted it I tried what I can to debug the error. echo %CD% just print the current directory, to help me debug. Could be cd and the error will persist. (I tried)
What I could find out, the variable "%%~fd" is expanded and than the line is processing.
In this way call pass "C:\tmp\02\06" (instead of "C:\tmp\02\06 & a") and try to execult the command a
Like rojo said, your problem is located in the :Whattodo function.
Replace echo %CD% with echo !CD! and it should work.
As percent expansion is vulnerable to special characters, but delayed expansion isn't.
If you don't want to enable it always, you can enabled it only in the function.
:Whattodo
setlocal EnableDelayedExpansion
echo !CD!
endlocal
goto :eof

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