Define Batch process parameters - batch-file

I define some parameter when calling a batch file:
:: Usertype:I,C
set Usertype=%~1
set Deltaval=%~2
If Usertype=="C" set Gender=NA
set Gender=%~3
If Gender==NA
(
goto
END
)
However, I got issue at line If Usertype=="C" set Gender = NA with following error:
The syntax of the command is incorrect.
Are there any solution to it?

So this is what I am trying to tell you, you need to use % in order to define the words you use as variables and not see them as plain text.
:: Usertype:I,C
set "Usertype=%~1"
set "Deltaval=%~2"
If /i "%Usertype%"=="C" If /i "%Usertype%"=="I" (
set "Gender=NA"
) else (
set "Gender=%~3"
)
If "%Gender%"=="NA" goto :eof
Or you can do
:: Usertype:I,C
set "Usertype=%~1"
set "Deltaval=%~2"
If /i not "%Usertype%"=="C" If /i not "%Usertype%"=="I" do something

Related

Batch - return value from inside a loop

I have the following Problem:
I have 2 functions/labels called STARTERand Get_age. The Get_age function stores ages and names in some related variables and shall return the age of the person i passed to it (passed the name).
But the variable which shall store the return value -> means !arr[%%i].age!seems to be empty all the time. I think this is may of the (ENDLOCAL ...) block.
What confuses me too: if i only want to return the %%i inside the (ENDLOCAL ...) block it works fine, but when there are some !exclamation marks! the variable seems to get empty.
How can i now return the age easily without bloating the code? PS: This piece of code is only a small example to give a better view on it.
ThankĀ“s a lot
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM FUNCTION WHICH ASKS STORES A NAME AND ASKS FOR ITS AGE
:STARTER
setlocal
set "person=Niels"
call :Get_age "%person%" "readback"
echo %person% is -%readback%- years old.
pause
exit /b 0
REM FUNCTION WHICH RETURN THE AGE TO THE GIVEN NAME
:Get_age
setlocal
set "searchName=%~1"
set "retVal=%~2"
set "arr[0].name=Niels"
set "arr[0].age=5"
set "arr[1].name=Julia"
set "arr[1].age=2"
set "arr[2].name=Claus"
set "arr[2].age=9"
set "arr_size=2"
REM Go through the arr and return the age to the given name
for /l %%i in (0 1 %arr_size%) do (
if "!arr[%%i].name!" equ "%searchName%" (
(ENDLOCAL
set %retVal%=!arr[%%i].age!
)
)
)
exit /b 0
Perhaps this structure, which enables delayed expansion only when needed, and ends it again as soon as it has no further purpose, will work better for you:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem Stores a name, determines their age and returns it.
Set "person=Niels"
Call :Get_Age "%person%" "readback"
Echo %person% is -%readback%- years of age.
Pause
Exit /B 0
:Get_Age
Set "arr[0].name=Niels"
Set "arr[0].age=5"
Set "arr[1].name=Julia"
Set "arr[1].age=2"
Set "arr[2].name=Claus"
Set "arr[2].age=9"
Set "arr_size=2"
For /L %%G In (0,1,%arr_size%) Do (
SetLocal EnableDelayedExpansion
If /I "!arr[%%G].name!" == %1 (
For %%H In ("!arr[%%G].age!") Do (
EndLocal
Set "%~2=%%~H"
)
) Else EndLocal
)
GoTo :EOF
You could store the value and return it after the loop.
set "found="
for /l %%i in (0 1 %arr_size%) do (
if "!arr[%%i].name!" equ "%searchName%" (
set "found=!arr[%%i].age!"
)
)
(
ENDLOCAL
set "%retVal%=%found%"
)
goto :eof

how to include or in a loop in batch script

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

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.

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