Windows Batch loop all variables with findstr - variables

In my Windows batch file I have a various amount of variables. Lets say I have the following variables:
set varTest1=test1
set varTest2=test2
set otherVar=variable500
set varS=string
set yetAnotherVar=foo
They do really make no sense buts thats not the point. I am looking for a method that prints out all values of variables that start with var:
So when I run my batch with a certain help parameter it should print out all three variables starting with var and its value.
The output could look like this:
These are the available variables:
varTest1 : test1
varTest2 : test2
varS : string
I created the following for reading the parameter:
IF "%1" == "" (
echo No help parameter was set. Program will exit. ) ELSE (
IF "%1" == "help" (
call :showAllAvailableVars ) ELSE (
echo Do something else))
Now I would have my method
:showAllAvailableVars
I think the solution could be something with the findstr method but I could not figure it out how to do that because findstr is mainly for files and not for searching through own program variables.

Create array instead of different variables. Like,
set var[0]=test1
set var[1]=test2
set var[2]=string
then in your 'showAllAvailableVars' function do this
for /L %%i in (1,1,%n%) do echo !var[%%i]!

You could use set var to print all variables which begins with var.
See also set /?

Related

Batch (Windows) set variable to first argument doesn't work

I have a batchscript mybatch in which I try to store the first user argument in a variable called FILE
set FILE = %1
if defined FILE (
echo defined
echo do something with %1
) else (
echo not defined %1
)
If I execute my batch via mybatch test1 I get always not defined test1. Why is variable FILE not defined?
You have unwanted spaces in your variable assignment, so you have defined a variable with a space in the name that always has a value beginning with a space. Your IF statement is checking if a variable without a space exists.
See Declaring and using a variable in Windows batch file (.BAT)
I recommend your first line should be:
set "FILE=%~1"
You can try like this :
#echo off
set "FILE=%~1"
if Exist "%FILE%" (
echo.
echo "%FILE%" Exist
echo do something with "%FILE%"
) else (
echo "%FILE%" is not defined
)
Pause

batch file difference between %variable% and %variable % or variable call with spaces

I've just started with batch file programming and testing variable usage momentary.
Does anyone know what is the difference between the 2 variable calls, the space before the last %
#echo off
set pathOS1="\\o1511\Pcs7ProjectO1511\OS1511\GraCS\"
ECHO We're working with %pathOS1%
ECHO We're working with %pathOS1 %
since the echo is different:
We're working with "\\o1511\Pcs7ProjectO1511\OS1511\GraCS\"
We're working with \\o1511\Pcs7ProjectO1511\OS1511\GraCS\
Delayed expansion is not enabled.
Spaces are allowed in the variable name.
set "data=100"
set "data =101"
echo %data%
echo %data %
set data
You have two variables with two similar values
So I summarize your answers:
This can happen if there's no setlocal at the start of the routine, any values set in the environment remain set, so if "pathos1 " was set previously, its value will be retained until it's cleared by a set "pathos1 =" statement or cmd is closed.
So, since I didn't close the cmd and didn't set setlocal, I called the previously set variable "pathos1 =".
It is good example for setlocal :)

for command to set a variable does not work

I'm running this command and I don't see why it won't work
setlocal EnableDelayedExpansion
for %%a in (harry-boy) do set %%a:-==
echo %harry%
pause
And this is the result I get -
e:\6\1>setlocal EnableDelayedExpansion
e:\6\1>for %a in (harry-boy) do set %a:-==
e:\6\1>set harry-boy:-==
e:\6\1>echo
ECHO is on.
e:\6\1>pause
Press any key to continue . . .
I'm changing the hyphen sign to a equals sign then running the set command on that. I expect to see that the variable "harry" = "boy"??
Here is a simple test -
set file=play=here.mkv
set %file:==-%
echo %file%
pause
and I get this -
set file=play=here.mkv
==-%
was unexpected at this time.
set %file:==-%
I thought I would get the new contents of file = play-here.mkv. Ok, I see that this makes the syntax wrong and the set command stops. So how do I change the = to a hyphen?
The string replacement format:
%var:old-string=new-string%
does NOT work on for replaceable parameters, just in Batch variables. The equivalent way for your example, using a variable instead, would be:
set a=harry-boy
set %a:-==%
echo %harry%
pause
Output:
C:>set a=harry-boy
C:>set harry=boy
C:>echo boy
boy
C:>pause
Press any key to continue . . .
Easy soloution:
for /f "tokens=1,2 delims=-" %%a in ("harry-boy") do set %%a=%%b
Echo %harry%
And that should do your job for you. But it will only work with one - in the quote.
SET will assign the value on the right of the first = to an environment variable named on the left.
Hence you would be assigning a value of = to a variable named harry-boy:- in BOTH cases.
You can verify this by executing
set harr
which will display any variable starting harr

Windows Batch - How to search muliple variables then create new variables with their name

I have a batch file that pulls in all the command line arguments and stores them as variables. I then search for a name in each, then make a new variable of that name but with new content (rem). I have the first search done here but I don't know how to search all the others without repeating the same commands. I tried it by doing that and it didn't work. Anyway if there is an easier way than this please tell.
#echo off
setlocal enableDelayedExpansion
set var1=noisereduction-01
set var2=sharpener-03
set var3=resize-02
set var4=output-01
set var5=grain-02
set var6=crf-21
set var7=preset-veryfast
set var8=tune-film
set var9=ref-2
set var10=outputdir-g:\output
set var11=mode-3
for /l %%N in (1 1 11) do call :test var%%N
exit /b
:test variableName
echo !%1!|>nul findstr /rx .*noisereduction.* && set !%1!=rem
echo %1 = !%1!
exit /b
Whoops, there is an error above. I want to create a new var with the name of "noisereduction-01" and a value of "rem", not just give var1 a value of rem. I thought the set !%1!=rem would do it but it is not.
Let's see this problem. When
set var1=noisereduction-01
... and
for /l %%N in (1 1 11) do call :test var%%N
... then the first call to test is with "var1" parameter. This mean that into test:
set !%1!=rem IS set !var1!=rem IS set noisereduction-01=rem
... that correctly do what you want.
HOWEVER:
echo %1 = !%1! IS echo var1 = !var1! IS echo var1 = noisereduction-01
If you want to show the value of noisereduction-01 variable, then you must perform an additional expansion step:
for %%v in (!%1!) do echo param: %1, variable: !%1!, value: !%%v!

capturing CMD batch file parameter list; write to file for later processing

I have written a batch file that is launched as a post processing utility by a program. The batch file reads ~24 parameters supplied by the calling program, stores them into variables, and then writes them to various text files.
Since the max input variable in CMD is %9, it's necessary to use the 'shift' command to repeatedly read and store these individually to named variables. Because the program outputs several similar batch files, the result is opening several CMD windows sequentially, assigning variables and writing data files. This ties up the calling program for too long.
It occurs to me that I could free up the calling program much faster if maybe there's a way to write a very simple batch file that can write all the command parameters to a text file, where I can process them later. Basically, just grab the parameter list, write it and done.
Q: Is there some way to treat an entire series of parameter data as one big text string and write it to one big variable... and then echo the whole big thing to one text file? Then later read the string into %n variables when there's no program waiting to resume?
Parameter list is something like 25 - 30 words, less than 200 characters.
Sample parameter list:
"First Name" "Lastname" "123 Steet Name Way" "Cityname" ST 12345 1004968 06/01/2010 "Firstname+Lastname" 101738 "On Account" 20.67 xy-1z 1 8.95 3.00 1.39 0 0 239 8.95
Items in quotes are processed as string variables. List is space delimited.
Any suggestions?
echo %* 1>args.txt
%* references all arguments: %1 %2 %3...
It also works with subroutines.
call :test 1 2 3
goto :eof
:test
echo 1: %1
echo 2: %2
echo 3: %3
echo *: %*
exit /b
output:
1: 1
2: 2
3: 3
*: 1 2 3
See the following website for more information:
http://ss64.com/nt/syntax-args.html
Interesting Post. It sparked my interest.
I too am needing something that could accept parameters and although this probably isn't useful to you now I thought it might be useful at some later date.
My solution is less simple - because there just isn't an elegant way to do it.
Basically, in this example the "-" can be used to identify a parameter, and the next space is assumed to be set to a value.
Legal Stuff:
So this is all my code and I don't really care how or where you choose to use it. No need to cite me it's just an example anyway.
Like this:
Microsoft Batch:Begin Copy below and save as filename.bat
#ECHO OFF
REM USAGE: this-batch-name.bat -BUILD "1.2.3 build 405" -JOB "Running This Job" -run RUN_FUNCTION
SET __CURRENT_WORKING_DIRECTORY__=%~dp1
ECHO.__CURRENT_WORKING_DIRECTORY__=%__CURRENT_WORKING_DIRECTORY__%
REM # Clear Previous Variables
SET PACKAGING_BUILD_NUMBER=
SET PACKAGING_JOB_NAME=
SET GO_DEEPER=
SET RUN_COMMAND=
REM ## In order to read variables set while in a "FOR" loop
REM ## you have to set the 'ENABLEDELAYEDEXPANSION' with 'SETLOCAL'.
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
REM ## Capture Command line parameters here with a %*
FOR %%A IN (%*) DO (
REM ## If we found something with a '-' in previous pass run GO_DEEPER will be defined and thus set to the command line argument.
IF DEFINED GO_DEEPER (
REM ## When ENABLEDELAYEDEXPANSION is Set with setlocal command you have to use exclamation: i.e. '^!'
IF /I "-BUILD"=="!GO_DEEPER!" SET PACKAGING_BUILD_NUMBER=%%A
IF /I "-JOB"=="!GO_DEEPER!" SET PACKAGING_JOB_NAME=%%A
IF /I "-RUN"=="!GO_DEEPER!" SET RUN_COMMAND=%%A
SET SET GO_DEEPER=
)
IF /I "%%A" GEQ "-" (
REM ## Wow we found your command line argument that started with a '-' so set the GO_DEEPER Var
SET GO_DEEPER=%%A
) ELSE (
SET SET GO_DEEPER=
)
)
REM ## Time to grab the variables set while in delayed expansion mode
ENDLOCAL && SET PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER% && SET PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME% && SET RUN_COMMAND=%RUN_COMMAND%
REM ## Sucks, but you have to clear the '"' and "'" if it exists.
IF DEFINED RUN_COMMAND (
SET RUN_COMMAND=%RUN_COMMAND:"=%
SET RUN_COMMAND=%RUN_COMMAND:'=%
)
IF DEFINED PACKAGING_JOB_NAME (
SET PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME:"=%
SET PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME:'=%
)
IF DEFINED PACKAGING_BUILD_NUMBER (
SET PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER:"=%
SET PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER:'=%
)
REM ## Now we can try to run the command function if the -run was used...
IF DEFINED RUN_COMMAND (
CALL:--%RUN_COMMAND% "'%PACKAGING_JOB_NAME%'","'%PACKAGING_BUILD_NUMBER%'"
) ELSE (
ECHO Try running:
ECHO %0 -BUILD "1.2.3 build 405" -JOB "Running This Job" -run RUN_FUNCTION
)
GOTO DONE
:--RUN_FUNCTION
ECHO running... %~0
SET VARPASSED1=%~1
SET VARPASSED2=%~2
IF DEFINED VARPASSED1 ECHO VARPASSED1 was %VARPASSED1%
IF DEFINED VARPASSED2 ECHO VARPASSED2 was %VARPASSED2%
ECHO Add your code to process here...
GOTO:EOF
:DONE
ECHO We got the following results...
IF DEFINED PACKAGING_JOB_NAME ECHO PACKAGING_JOB_NAME=%PACKAGING_JOB_NAME%
IF DEFINED PACKAGING_BUILD_NUMBER ECHO PACKAGING_BUILD_NUMBER=%PACKAGING_BUILD_NUMBER%
IF DEFINED RUN_COMMAND ECHO RUN_COMMAND=%RUN_COMMAND%
</pre> </code>
Microsoft Batch END Copy
RESULTS:
__CURRENT_WORKING_DIRECTORY__=C:\dev\a\win\sysprep\
running... :--RUN_FUNCTION
VARPASSED1 was "'Running...'"
VARPASSED2 was "'This...'"
We got the following results...
PACKAGING_JOB_NAME="Running This Job"
PACKAGING_BUILD_NUMBER="1.2.3 build 405"
RUN_COMMAND=RUN_FUNCTION