If statement in batch file doesn't execute(Possible syntax issue?) - windows

I am using the below script to help automate some processes that would make my work life easier. When running this current version it faults out and closes the program right as soon as the first if statement executes. Did quite a bit of research on my own and the code looks to be correct. The program closed so fast I couldn't read a reason why. So I ran all the output into a txt file. It looks as if the program faults out for a syntax reason. I unfortunately don't have the file with me and don't have the exact error. I can post it tomorrow when it is in front of me.
::Turns off unnecessary messages from Command Prompt
echo off
::Copies files over from the NAS drive that are required for setup
echo Transfering files from NAS1...
if not exist "%userprofile%\Desktop\Install_Files" mkdir %userprofile%\Desktop\Install_Files
xcopy /Y \\nas1\Volume_1\"Tech Department"\"General Windows POS Preperation"\* "%userprofile%\Desktop\Install_Files"
echo File Transfer Complete
::Start installation of Foxit Reader
echo Installing Foxit Reader...
start /w %userprofile%\Desktop\Install_Files\"FoxitReader831_current version".exe
echo Installations Complete
::Changes background by changing the file pathway in the registry value
echo Setting Background...
REG ADD "HKCU\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d %userprofile%\Desktop\Install_Files\NewTMS1024x768.jpg /f
::Changes the Workgroup and Computer Name
echo Setting Computer Name...
SET /P PCNAME=Please enter computer name:
wmic computersystem where "Name='%computername%'" rename "%PCNAME%"
echo Setting Workgroup...
SET /P WGNAME=Please enter workgroup name:
Wmic computersystem where name="%computername%" call joindomainorworkgroup name="%WGNAME%"
::Selecting which POS Software to install
SET /P POSNAME=Please enter POS Software to install (a:Aldelo m:MAPOS t:TRPOS):
if /i %POSNAME% == "m"
(
::Transfers required files from NAS drive to Install Folder
echo Transferring install files...
xcopy /Y \\nas1\Volume_1\"Tech Department"\"POS Software"\MAPOS\* "%userprofile%\Desktop\Install_Files"
::Installs MAPOS and Groovv SDK for card processing
echo Installing GroovvSDK...
start /w %userprofile%\Desktop\Install_Files\GroovvSDK_Client_Setup_v3.9.6
echo Installing MAPOS...
start /w %userprofile%\Desktop\Install_Files\mapos_install
)
if /i %POSNAME% == "t"
(
::Transfers required install file for TRPOS
echo Transferring install files...
xcopy /Y \\nas1\Volume_1\"Tech Department"\"POS Software"\TRPOS\TRPOS_install.exe "%userprofile%\Desktop\Install_Files"
::Installs TRPOS
start /w %userprofile%\Desktop\Install_Files\TRPOS_install.exe
)
if /i %POSNAME% == "a"
(
)
else
(
echo No POS Software selected or improper input
)
::Force restarts the computer so changes will take effect
::shutdown.exe /r /t 00

There are two problems with your ifs
The first one is related to how the parser handles the commands. The line
if %POSNAME% == "m"
is not comparing the value inside the variable against a literal string. What is happening is that the parser expands the variable reference (%POSNAME%) replacing the reference with the value inside the command and then tries to execute the resulting command, without any variable reference, only the value. So, for expected values stored in POSNAME variable, the command executed and the result will be
if %POSNAME% == "m"
value parsed as result
--------------------------------------------------------------
POSTNAME is empty -> if == "m" syntax error
POSTNAME is a -> if a == "a" false
POSTNAME is m -> if m == "m" false
In the first case the command fails because there is not any value in the left side of the == operator. The variable is empty and nothing can be placed in the command to be executed.
The second case seems logical, but sometimes the third case is not so obvious. Why false? Because the value in the right side of the == is a quoted literal while the value in the left side is an unquoted literal, so both values do not match.
You can solve this problem simply quoting both sides
if "%POSNAME%"=="m"
value parsed as result
--------------------------------------------------------------
POSTNAME is empty -> if "" == "m" false
POSTNAME is a -> if "a" == "a" false
POSTNAME is m -> if "m" == "m" true
(note: you can also unquote both sides, but it is not recommended unless you are completely sure what the values on both sides are and that the resulting command will not generate problems)
The second problem in your code is parenthesis placement. The batch syntax requires them to be properly placed:
If present, the opening parenthesis in the if clause must be in the same line that contains the if
If there is an else clause then the closing if parenthesis must be in the else line.
If there is an else opening parenthesis, it must be in the else line
So, this
if "%POSNAME%"=="m"
(
.....
)
is not a valid syntax. You can see here samples of how to place the parenthesis.

For starters... there is a problem with the IF statements. You need to quote both sides of the == and remove spaces. Change this format
if /i %POSNAME% == "m"
to this
if /i "%POSNAME%"=="m"
Try that and post results.

Related

What does this script do (Windows CMD)

SET /P A=SPR
IF /I "%A:~,1%" EQU "d" (
IF EXIST %1 (
IF NOT EXIST %2 (
COPY %1 %2
)
)
)
First, I suggest to open a command prompt window and run the following commands:
set /?
if /?
copy /?
call /? ... explains %1 and %2.
For each command the help is output which you should read from top to bottom.
This small batch file first prompts the batch file user for a string with prompt text SPR. The string entered by the user is assigned to environment variable A if the user enters anything at all as expected by this batch file.
Next a case-insensitive string comparison is done to check if the first character of the string entered by the user is d or D.
It would be much better to use comparison operator == instead of EQU in this case. == always makes a string comparison while EQU first tries to compare integers and if that fails because it is not possible to convert both comparison arguments to signed 32-bit integers, a string comparison is done. The second argument d is not a signed 32-bit integer.
The batch file must be started with two arguments being in this case two file names without or with wildcards. I suppose the batch file expects two file names without or with path without wildcards.
If the first condition is true, the batch file checks next if source file with name passed to batch file as first argument exists and next if target file passed to batch file as second argument does not exist. The source file is copied to target file if those 2 conditions are true.
More fail safe would be:
SET "Input=?"
SET /P "Input=SPR: "
IF /I "%Input:~0,1%" == "d" (
IF "%~1" == "" GOTO :EOF
IF "%~2" == "" GOTO :EOF
IF EXIST "%~1" IF NOT EXIST "%~2" COPY "%~1" "%~2"
)
The environment variable Input is now predefined with ? as string. So when the user just hits RETURN or ENTER without entering anything at all, the environment variable Input is nevertheless defined with ? as string and next command IF works, but of course the condition is in this case false.
The IF condition as is results in an exit of batch execution because of an syntax error only when the first entered character is ". In all other cases the IF condition works now and runs a case-insensitive string comparison.
The improved batch file checks next if the batch file was really started with two argument strings as expected and exit the batch file with a jump to predefined label for End Of File if either first or second argument is an empty string (or a string consisting only of one or two double quotes).

Trying to conditionally change a file path in a windows batch script

I have a simple script that launches an XML file path in Notepad++
Unfortunately, the generated file path for some of the files is incorrect and I'm trying to compensate for it in the script (ideal fix is clearly to resolve the path issue but at the moment this is not an option).
Here is what I have, apologies for the code I'm very new to Batch Script...
set /p filePath= Enter file path:
if "%filePath%" == "1" goto xmlMenu
else if "%filePath%" == "file://path/of/file/*/*/*/*/A/*.XML"
set filePath="file://path/of/file/*/*/*/*/B/*.XML"
goto openXML
I would like the filePath variable to inherit the rest of the path from the user input but at the moment its explicitly setting the path with the wildcards. There also seems to be a problem with the way I have stated the condition as it appears to set the path to /B/*.XML regardless of the else if condition.
There are many errors, but as you say, youre new.
First, if the filepath you enter is "1" then you should arrive safely at xmlmenu.
The next line will generate an error because with an else clause,
the If-true instruction must be (parenthesised)
the ( must occur on the same physical line as the if
the sequence ) else ( must all be on the same physical line (which need not be the same as the if)
In any case, the else is redundant. If the condition is not true, the jump will not happen, hence the next statement line will be executed.
if...==... is true if the string on the left is identical to the sring on the right, hence you would need for the entered string to be absolutely identical to file://path/of/file/*/*/*/*/A/*.XML for the if to be true. The action-statment also must be on the same physical line as the if, so this is a second source of syntax errors.
the set is then executed, assigning that famous string (in quotes) to the variable filepath. And then we're off to openxml.
Note that \ is a path separator in Windows; / is a switch-specifier.
Now - if you were to explain what you want to do - perhaps enter a filename which may be somewhere in a directory structure and if it's found then assign what? to the variable filepath. One or two examples would be a good idea.
This should get a usable system going. The numeric check is incomplete, but would be adequate. I've not made it bullet-proof against a user determined to break it...
#ECHO OFF
SETLOCAL
:again
SET "filepath="
set /p filePath="Please enter filepath as yyyymmdd : "
IF NOT DEFINED filepath GOTO :EOF
if "%filePath%" == "1" goto xmlMenu
:: test to see whether entered data is numeric
SET /a "filepath=%filepath%" 2>nul
IF %filepath%==0 echo invalid entry - numerics only, please&GOTO again
IF %filepath% lss 19800100 GOTO badymd
IF %filepath% gtr 20991231 GOTO badymd
SET filepath=\path\prefix\%filepath:~0,4%\%filepath:~4,2%\%filepath:~6,2%
:: This is just to show the filepath constructed
ECHO filepath is "%filepath%"
IF NOT exist "%filepath%\A\*.xml" ECHO "%filepath%\A\*.xml" does NOT exist&GOTO again
IF NOT exist "%filepath%\B\*.xml" ECHO "%filepath%\B\*.xml" does NOT exist&GOTO again
SET "filepath=%filepath%\B\*.xml"
goto openXML
:badymd
ECHO invalid entry - 4 digits FOR year, 2 FOR month an 2 FOR day, please
GOTO again
:xmlmenu
ECHO AT xmlmenu&GOTO :eof
:openxml
ECHO AT openxml with filepath="%filepath%"&GOTO :eof
GOTO :EOF
Obviously, change the path prefix to suit.
For an input of 20140220, the path ....\2014\02\20 would be constructed. Then there's a check for and this +\B\*.xml - I can only presume both must exist to proceed to openxml.

Windows: copy a file until the file not exist

I want to use a Windows batch file in to copy a file (myfile0001.bdg) from one specific directory to another. But I want to check if the file in the target directory exists and if the answer is yes, increment the file with 0001 and check again if the file exists (myfile0002.bdg) and so on, until the file does not exist, and copy the file with the new title.
So, if in the target directory, I have these files:
myfile0001.bdg
myfile0002.bdg
myfile0003.bdg
myfile0004.bdg
myfile0005.bdg
myfile0006.bdg
The new file should be named myfile0007.bdg. The next time I will execute the batch, the new file will be myfile0008.bdg, etc.
I know there is a command "IF EXIST" but I don't know to do what I need.
==============
I'm under Windows 7 x32
The source directory is "C:\USERS\RAMBYTES\DOCUMENTS\"
The target directory is "P:\BACKUP\"
The file is "MYFILE0001.BDG"
Something like this:
#echo off
set source_file=C:\USERS\RAMBYTES\DOCUMENTS\MYFILE0001.BDG
set target_dir=P:\BACKUP\
set done=0
for /l %%i in (1,1,1000) do (
call :check_and_copy %%i
if errorlevel 1 goto :eof
)
goto :eof
:check_and_copy
setlocal EnableDelayedExpansion
set num=000000%1
set fnum=!num:~-4!
set fname=%target_dir%\myfile%fnum%.bdg
rem echo %fname%
if not exist "%fname%" (
echo copying %source_file% to %fname%
exit /b 1
)
exit /b 0
There is no error handling in case there are more than a 1000 files present in the target directory. If you want to increas the file limit, you need to adjust the "main" for loop and the "formatting" of the number in the sub-program
The trick with adding the leading zeros was taken from here: https://stackoverflow.com/a/9430912/330315
#ECHO OFF
SET destdir=c:\destdir
SET newname=myfile0000
FOR /f %%i IN (' dir /b /on %destdir%\myfile????.bdg ' ) DO SET newname=%%~ni
SET newname=1%newname:~-4%
SET /a newname+=1
SET newname=myfile%newname:~-4%.bdg
COPY myfile0001.bdg %destdir%\%newname%
change the destination directory as desired, and include the source directory if required.
Take the file name.
Extract the numeric part.
Check if the corresponding target name exists.
If so,
4.1) increase the numeric part;
4.2) if it doesn't exceed the highest possible number go to Step 3;
4.3) otherwise terminate.
If the target name doesn't exist, copy the file with the current numeric part and terminate.
Although algorithmically the condition in 4.2 may be more natural to be checked just after increasing the numeric part, like I put it above, the below script performs the check at a different point, at the beginning of the loop, which starts just after extracting the original numeric value from the source filename. Implementationally, that seemed to me more convenient.
In all other respects, the script implements the same algorithm:
#ECHO OFF
SET "fname=%~n1"
SET counter=1%fname:~-4%
:loop
IF %counter% GTR 19999 (
1>&2 ECHO Cannot copy the file: no free slots.
EXIT /B 1
)
SET "targetname=%~2\%fname:~0,-4%%counter:~1%%~x1"
IF EXIST "%targetname%" (
SET /A counter+=1
GOTO loop
) ELSE (
COPY %1 "%targetname%"
)
To explain some parts:
The tilde (~) in references to positional parameters means de-quoting of the correspondent parameter.
Sometimes in the script, the tilde is also directly followed by a modifier. Two modifiers are used here, n and x. The former causes the parameter to expand to the corresponding file name only (without the path and the extension) and the latter extract only the extension.
You can learn more about modifier in the built-in of the FOR command (by running
The fname environment variable is needed because extracting of name parts can only be done on environment variables. The %fname:~-4 expression, in particular, evaluates to the last four characters of the fname value. More specifically, it reads: extract the substring that starts at the 4th character from the end and, as -4 isn't followed by another argument, includes all the characters from that point till the end of the string.
Another similar-looking expression, %fname:~0,-4%, does the opposite: it returns the contents of fname except the last four characters. The meaning of the numbers is this: extract the substring that starts at the beginning of the string (offset 0) and spans the range up to and including the character at the offset of 4 from the end.
One more expression of this kind, %counter:~1, extracts the characters starting from the second one (i.e. offset 1) and up to the end of string (no second argument).
Run SET /? to find out more about string expressions.
The counter implementation may also require explanation. The 1 added in front of the numeric part of the file name is needed so that the entire value could be interpreted and processed correctly when incrementing it.
The thing is, a numeric value starting with a 0 is treated as an octal by the CMD command processor, so, putting 1 at the beginning makes it to interpret the number as a decimal, which it actually is. When constructing the complete name of the target file, we simply need to discard the added 1, which is what the %counter:~1 is used for.

Brackets aren't working on this Windows File?

I am new here and my english is not very good so first, pardon me.
My problem is that this batch keep reading the strings inside the variable "IF EXIST %FSIZE%"; i meant, in case that variable does not exist, the batch keep reading inside the () brackets instead go on the rest of the stings.
If %FSIZE% exist, the batch perform the 2 task i assign: (1.) If size is equal, goto cifiles5x. (2.) If size is NOT equal it uses 7z to extract the file i want to be there.
If %FSIZE% DOES NOT EXIST, the batch keep saying "765952 was not expect at this moment".
I am taking the advices on ss64.com like don't use brackets or quotes when comparing numeric values (%size% EQU 765952) but i don't understand why it does not continue to where the ) ends.
I have also try to link the commands with "&&" so i can erase the brackets but the results are the same.
I know there's 2 spaced patches without quotes; they are unquoted because if i did the size checker won't work.
Thanks for reading this.
EDIT: Batch modified according to suggestions made.
#ECHO OFF
TITLE Log checker
COLOR 0F
SET FSIZE=%ProgramFiles(x86)%\Ces\Log Files Ver\LogVbReg_r2.dll
SET InsDIR=%ProgramFiles(x86)%\Ces\Log Files Ver\
REM I didn't add "" on FSIZE and InsDIR because if i did, quote the variable will
REM result a doubled quoted patch and won't work.
CLS
ECHO ==============================================================================
ECHO = Log checker =
ECHO ==============================================================================
ECHO Checking if exist:
ECHO "%FSIZE%"
IF EXIST "%FSIZE%" (
ECHO It does exist, checking size...
FOR %%A IN ("%FSIZE%") DO SET SIZE=%%~ZA
IF "%SIZE%" EQU "765952"
ECHO Size is right, jumping to CIFILES5
GOTO CIFILES5x
) ELSE (
ECHO Size is not right, extracting the needed file...
7z.exe e "Data_X.*" -o"%InsDIR%" -y "LogVbReg_r2.dll"
GOTO CIFILES5x)
ECHO Does not exist; extracting file...
REN Data_X.* Data_X.exe
Data_X.exe
TIMEOUT 2>NUL
REN DATA_X.* Data_X.dat
:CIFILES5x
ECHO Reach cifiles5x
PAUSE
IF EXIST "%TEMP%\9513.CES" (GOTO OPS) ELSE (GOTO LNKD)
You have space in your file name of %FSIZE%. As result text after space it is treated as command for if. Try using quotes around "%FSIZE%" or do CD to folder first and than check for just file name.
You have several minor errors:
If the file name may have spaces, it MUST be enclosed in quotes in all cases:
IF EXIST "%FSIZE%" (
FOR /F is used to read file CONTENTS. If you want to process file NAME, don't use /F option, but plain FOR command:
FOR %%A IN ("%FSIZE%") DO SET SIZE=%%~ZA
The ELSE part of an IF command must appear in a line alone:
GOTO CIFILES5x
) ELSE (
The same thing for a parentheses:
GOTO CIFILES5x
)
Excepting if the opening parentheses appear in the same line. This is correct:
IF EXIST "%TEMP%\9513.CES" (GOTO OPS) ELSE (GOTO LNKD)
If you compare numbers for equality (EQU, NEQ) then don't matter if they are enclosed in quotes or not. Just in case of GTR, GEQ, LSS and LEQ they must have NOT quotes. However, in your case, this is not a problem...

windows batch file with goto command not working

I have a problem with GOTO command and affiliated labels.
Facts: Given a bunch of files from a folder (they are log errors) I need to open them and check if they contain a specific string. If yes then eliminate some characters (all the chars after the last appearance of "_", including itself) from the file names and do other operations.
For cutting off the chars I'm using GOTO command in a loop manner as I found it described here: http://www.robvanderwoude.com/battech_while_loops.php
The script is:
#echo off
setlocal EnableDelayedExpansion
cls
for %%X in (D:\e-pub\outbox\logs\*.*) do (
for /F "tokens=7" %%S in (%%X) do (
if /i "%%S"=="<ml>" (
SET fisier=%%~nX
SET cond=!fisier:~-1!
SET fisier=!fisier:~0,-1!
:loopStart
rem condition to break the loop
if !cond!==_ goto loopEnd
SET cond=!fisier:~-1!
SET fisier=!fisier:~0,-1!
goto loopStart
:loopEnd
rem here it should be out of a loop
rem other stuff to do with var !fisier!
rem the following line is not executed because of the label loopEnd
echo !fisier!
)
)
)
pause
The script is not running because there is an empty line after the label loopEnd?!
If I'm writing any instructions right after that label they will be executed but the rest of iterations from the first for statement won't be executed (the log errors folder contains more one file)
Can someone provide help?
You've got two problems.
One problem is that a goto breaks a for-loop.
The other, labels are quite difficult in parenthesis.
The goto breaks always and all nested loops, even if the label of the goto is in the same block, and the for-variables are lost immediately after the jump.
In parenthesis lables are "two line" oriented!
I experimented with labels and here are some results for parenthesis.
When a label occurs, the next line has to be in the correct format for a "secondary" line.
That's why this fails.
(
:this label fails with a syntax error
)
(
:this works
:because this line is a "legal" secondary line
)
(
:: The remark style
:: fails, because it's not "legal" to use a double colon, because it's not a legal path (in the most cases)
)
(
:and now I got courious & echo This will not echo'd
:but & echo You can see this !
)
For the second line some steps of the batch parser are skipped.
# doesn't work, #echo Hello tries to start a file named #echo.bat.
Splitting of parenthesis fails, like in echo( hello.
Labels are handeled as a file name, :echo checks only if :echo is a valid file name and then skip this part.
::hello searches on the drive ::.
For test purposes the drive :: can be created with subst :: c:\temp.
As labels are simply ignored on the second line, ampersands and also pipes work, but the file on :: have to exist.
(
echo #echo This is %~f0
) > %TEMP%\testLabel.bat
REM create Drive ::
subst :: %temp%
(
:Label
::\testLabel.bat The bat will not be executed | echo But this
)
subst /D ::
Comments / Remarks
:: This is a REMark
A colon (:), which is actually the LABEL tag, can be used for comments instead of REM, by doubling it (::), except within parentheses (i.e. except within a FOR loop).
Using a double-label within a loop can cause the batch script to fail, but ONLY if:
The double-label is followed by a second double-label on the next line
The double-label is followed by an empty line on the next line
The double-label is the last line in the loop
In other words: if used within a loop, the double-label must be followed by a line which contains normal (i.e. valid) syntax. Even a single-label is valid syntax.
This error never occurs if the double-label is replaced with REM.
The error arising from a double-label occurs because CMD.EXE interprets :: as a drive letter (like C:).
.
Note -
This is an explanation for one of the problems mentioned in Jeb's answer, a point which he raises but doesn't deal with.

Resources