how to include or in a loop in batch script - batch-file

i'm trying to write in a folder the result of my my compilation : if there is one type of file (file1 or file2) it means that my compilation succeded. And I want to report this result in a txt file
I tried to do it with a flag using booleans but it just does not work. i'm parsing my folders and in each output/NameofaRun I'm looking if there is file1 or file2.
for /D %%X in (%1\*) do (
set flag = 0
if exist %%X\OUTPUT\%2\file1.txt ( flag = 1)
if exist %%X\OUTPUT\%2\file2.txt ( flag = 1)
if %flag% == 1 ( echo %%X>>%3\Compilation_Check.txt Succeeded )
if %flag% == 0 (
echo %%X>>%3\Compilation_Check.txt Failed Warning
)
)
I want to write in "compilation_check.txt" the result of my txt. and I'm running this batch with path to folders/name of folder as parameters

Your logic is correct; several details not...
The set command get as variable name all characters before the equal sign including spaces. You should use set flag=0 or, better yet, set "flag=0".
It is convenient to enclose file names between quotes in order to avoid problems if any name may include spaces...
You missed the set command when you change flag value.
When a variable value changes inside a (block), the new value can not be accessed via %var%, but using !var! syntax AND including setlocal EnableDelayedExpansion command at beginning of the program. There are a lot of detailed explanations about this point; look for "delayed expansion".
This is your code with previous modifications:
setlocal EnableDelayedExpansion
for /D %%X in (%1\*) do (
set "flag=0"
if exist "%%X\OUTPUT\%2\file1.txt" ( set "flag=1" )
if exist "%%X\OUTPUT\%2\file2.txt" ( set "flag=1" )
if !flag! == 1 ( echo %%X Succeeded >> "%3\Compilation_Check.txt")
if !flag! == 0 (
echo %%X Failed Warning >> "%3\Compilation_Check.txt"
)
)
However, I would do it this way:
for /D %%X in (%1\*) do (
set "anyFile="
if exist "%%X\OUTPUT\%2\file1.txt" set "anyFile=1"
if exist "%%X\OUTPUT\%2\file2.txt" set "anyFile=1"
(if defined anyFile (
echo %%X Succeeded
) else (
echo %%X Failed Warning
)) >> "%3\Compilation_Check.txt"
)
You are not using the value of flag variable. The if defined command allows to check for the existence of a variable; this "trick" allows to avoid the Delayed Expansion problem...
You may enclose several commands in a (block) so the output of all of them is redirected to the same file...

Related

How to set a variable value based on if-statement in batch file

I am trying to assign a value to a variable based on an if-else statement in a batch file. I am searching a text file for a string and if the string is found I need to assign a certain value to the variable.
I have used the if-else statement to assign the value but after exiting the if-else statement when I try to echo the variable i get value as 0
#echo off
set "logfile=results.txt"
find /n /i "Orientation : 0" "%logfile%" >nul
if %errorlevel% equ 0 (
set org = "landscape"
) else (
set org = "portrait"
)
echo %org%
The output for this should be 'landscape', I have verified that the string does exist in the file. For some reason, I am getting 0 as the output.
As an addition to the already provided answer, where it has been highlighted that you have used spaces either side of your = character. It means that you are setting, for example:org = "portrait".Unless you remove those spaces, you'd need to Echo:%org %to see the value you'd set, (and that also includes the doublequotes).
I would additionally suggest that you use this syntax:
#Echo Off
Set "logfile=results.txt"
Set "org="
Find /I "Orientation : 0" <"%logfile%" >Nul && (
Set "org=landscape"
) || (
Set "org=portrait"
)
Echo %org%
Alternatively, using FindStr gives you a little manoeuvre with the existence or number of spaces, e.g. the following would still match:Orientation:0orOrientation : 0
#Echo Off
Set "logfile=results.txt"
Set "org="
FindStr /IRC:"Orientation[\ :]*0" "%logfile%" >Nul && (
Set "org=landscape"
) || (
Set "org=portrait"
)
Echo %org%
Put into simple terms, the conditional operators && and || effectively mean, if the previous command was successful and if the previous command failed, respectively.
Remove the space around the equal sign:
set org="landscape"
set org="portrait"

Batch Get String between 2 characters

I have a text file with a content similar like this:
test.txt:
FIPS job <1532602344643_1> of size <134> successfully created.
<134> files successfully exported.
<0> files failed.
I want to store the string between the first two angle brackets in a variable. In this example it is 1532602344643_1. The string should have the same length but in all other brackets the length could change.
Im new to this so can someone help me?
Here the batch file that do what you need:
#echo off
set TEXT_FILE=text.txt
setlocal enabledelayedexpansion
set modules=
for /f "tokens=*" %%i in (%TEXT_FILE%) do (
set "tmp=%%~i"
set modulesFromLine=
call :getNext modulesFromLine "!tmp!"
if "-!modules!"=="-" (
set modules=!modulesFromLine!
) else (
set modules=!modules!,!modulesFromLine!
)
)
endlocal & set REZULT=%modules%&
echo. %REZULT%
exit /b 0
:getNext
setlocal
set accumulator=
set "tmp=%~2"
set "tmp=!tmp:*<=!"
set "tail=>!tmp:*>=!"
call set "module_name=%%tmp:!tail!=%%"
if not "-!module_name!"=="-" (
call :getNext %1 "!tail!"
if "-!%1!"=="-" (
set "accumulator=!module_name!"
) else (
set "accumulator=!module_name!,!%1!"
)
)
endlocal & set %1=%accumulator%&
exit /b 0
Put your file to TEXT_FILE and take RESULT. Remember that string should not contain ".
If you want to read someting about batch wiki is good for start.

how to match the command line arguments in the batch file

The following script commands check matching the command line argument %1 against the fixed word ala
<code>
#echo off
set one=%1
set two=%2
If NOT "%one%"=="%one:ala=%" ( echo the first argument contains the word "ala")
else ( echo no matching ! )
</code>
How to replace the fixed word "ala" with an argument %2 from the command line instead.
(because the simple replacement ala with %2 doesnt work).
Is there any better solution for comparing the argument strings ?
#ECHO OFF
SETLOCAL
ECHO %~1|FIND "%~2">NUL
IF ERRORLEVEL 1 (
ECHO "%~2" NOT found IN "%~1"
) ELSE (
ECHO "%~2" WAS found IN "%~1"
)
GOTO :EOF
Use the find facility. This avoids delayedexpansion but is relatively slow.
You need to use delayed expansion to accomplish that type of string replacement.
#echo off
setlocal enabledelayedexpansion
set "one=%~1"
set "two=%~2"
If NOT "%one%"=="!one:%two%=!" (
echo the first argument contains the word "%two%"
) else (
echo no matching
)
And you could also do it without delayed expansion using a technique with the CALL command.
#echo off
set "one=%~1"
CALL set "two=%%one:%~2=%%"
If NOT "%one%"=="%two%" (
echo the first argument contains the word "%two%"
) else (
echo no matching
)

Optional arguments parsed in batch using shift

I would like to know if it is possible to make a more elegant solution for a batch file accepting arguments from another script.
Looked at the solution here: Windows Bat file optional argument parsing, but can't make it work.
I have 19 arguments passed to the batch file. Arguments from the 7th are optional (come from checkboxes).
I have a working code with this structure:
REM arg7 parsing
set LOGFILE=%7
if not "%LOGFILE%" == "LOGFILE" (
set LOGFILE=
) else (
shift /7
)
REM arg8 parsing
set ARG8=%7
if not "%ARG8%" == "ARG8" (
set ARG8=
) else (
shift /7
)
REM arg9 parsing
set ARG9=%7
if not "%ARG9%" == "ARG9" (
set ARG9=
) else (
shift /7
)
And so on for each optional argument.
But now there are a lot of lines of similar code and it feels to me it could be optimized somehow. I'm not very familiar with batch scripting, so my tries of storing arguments in a list and trying to apply the above mentioned solution didn't work.
Based on what you've posted, try this:
SET "_="
FOR %%I IN (
LOGFILE ARG8 ARG9
) DO IF "%7" EQU "%%I" (SET "%%I=%7" & SET _=T)
IF DEFINED _ SHIFT /7
Just insert the other arguments on line three as is obvious.
#echo off
REM process optional parameters:
shift&shift&shift&shift&shift
rem shift /5 :: couldn't get this to work...
set narg=6
:loop
shift
set /a narg+=1
if "%1" == "" goto :done
set arg%narg%=%1
goto :loop
:done
echo no more parameters
set arg

What is the reason for command not recognized error message on executing my batch file?

I have written this batch code:
#echo on
set variable=z
set t=%time:~0,2%
IF %t% LSS 12 (
%z%=_1
) ELSE (
%z%=_2
)
And when run the batch file from command window, I get this error:
IF 14 LSS 12 (_1) ELSE (_2)
'_2' is not recognized as an internal or external command, operable program or batch file.
Where is the mistake in my code resulting in this error message?
I'm not sure what you want to achieve. I guess you want to check the condition %t%<12 and set your variable z to _1 if this is the case and to _2 otherwise.
Your error is that %z%=_1 is not a valide statement. To assign a value to a variable you always have to use SET:
SET z=_1 will result in %z% holding the value _1. So your code can be fixed like this:
#echo on
set variable=z
set t=%time:~0,2%
IF %t% LSS 12 (
SET z=_1
) ELSE (
SET z=_2
)
Based on the rest of your code you also might need to add the line SETLOCAL EnableDelayedExpansion at the beginning of your code and access your variable z as !z! instead of %z% below your IF-construct.
EDIT:
To fit your desctiprion from your comment you should do this:
#echo on
set t=%time:~0,2%
IF %t% LSS 12 (
SET z=_1
) ELSE (
SET z=_2
)
set folderName=%date%%z%
mkdir %folderName%
An easier way would be to set folderName inside the IF-counstruct:
#echo on
setlocal enabledelayedexpansion
set t=%time:~0,2%
IF %t% LSS 12 (
set folderName=%date%_1
) ELSE (
set folderName=%date%_2
)
mkdir !folderName!

Resources