I was wondering if there's a simple way to loop through all the variables that end with a character in Batch?
For instance, I have the following variables set:
SET id1=something
SET id2=something1
SET id3=something3
then I would like to loop through them using id* something like:
for %%a in ("%id*%") do echo %%a
Using array is not an option for the use case that I have.
any ideas would be extremely appreciated.
The answer you're looking for is:
Set id
If you want to be able to split the result up then you can put it into a for loop:
For /F "Tokens=1* Delims==" %A In ('Set id') Do #Echo Variable %A is %B
Double the % in a batch file.
Related
I have the following code that gets a list of comma-separated strings, surrounds them with some tag and writes the output to a file.
Works well for short lists, but for long (200 strings) lists, I get half-items
setlocal enabledelayedexpansion
SET /p CS_VALUES=<%1
SET TEMP_FILE="D:\tmp.txt"
>%TEMP_FILE% ECHO:
FOR %%i in (%CS_VALUES%) do (
SET query="<quer:string>%%i</quer:string>"
>>%TEMP_FILE% ECHO !query:~1,-1!
)
ECHO %TEMP_FILE%
So for the input: A00000, A00001, .... , A00200
I will get something like:
<quer:string>A00000</quer:string>
<quer:string>A00001</quer:string>
<quer:string>A00002</quer:string>
....
<quer:string>A00</quer:string>
(stopped at some variable <200 in the middle of that item)
I assume its related to some limitation but can't figure out which and how do I extend it (or if its not possible to extend - would be nice knowing whats the limitation)
The set /P command accepts a line of text with a length of up to 1021 characters/bytes, that is why your text becomes truncated.
To accept longer lines, namely such with about 8190 characters/bytes, use a for /F loop instead. In your code, this means to replace:
SET /p CS_VALUES=<%1
By:
for /F "usebackq delims=" %%A in ("%~1") do set "CS_VALUES=%%A"
Here's a For /F option example for you.
#Echo Off
>"D:\tmp.txt" (For /F "UseBackQ Delims=" %%A In ("%~1"
) Do Echo ^<quer:string^>%%~A^</quer:string^>)
USING BATCH/BASH On Windows:
I'm wondering how to search a text file (.txt) line by line as each line is its own string, look for the location directory (e.g, C:...) which is part of this string. and just take out that specific part and store in a variable.
This is to be done for each line though.
Any help would be appreciated.
TestRecID TestUserID 5 2017-04-20 TestTAtRec No 2.560 No C:\Test1\Test3 Tester
TestRecID TestUserID 5 2017-04-20 TestTAtRec No 2.560 No C:\Test2\Test2 Tester
TestRecID TestUserID 5 2017-04-20 TestTAtRec No 2.560 No C:\Test3\Test1 Tester
Presume that the above are each row in the text file.
At each space in the line that is where the different columns would have been in the DB.
The Expected Result would be to have:
C:\Test1\Test3 --> Variable 1
C:\Test2\Test2 --> Variable 2
C:\Test3\Test1 --> Variable n
Stored in some variable.
I really can't overstate enough how much of a newb I am with Batch/Bash for Windows
Follow Up Question:
for /f "tokens=9 delims= " %%a in (%findfile%) do echo %%a
I want to then store the %%a in a variable_name
I thought it would be a case of:
for /f "tokens=9 delims= " %%a in (%findfile%) do echo %%a
set variable_name = %%a
But it is not working??
Was Answered but I got given out to for asking a question in an answer,
The Answer given:
for /f "tokens=9 delims= " %%b in (%findfile%) do set "location=%%b"
EDIT
Working Loop for taking unique variables (Thanks to #Stephen):
REM Get Locations from textfile:
for /f "skip=1 tokens=9 delims= " %%a in (%findfile%) do set "_%%a=yes"
REM Translate into proper variables:
set count = 0
for /f "tokens=1* delims==#" %%a in ('set _') do (
set /a count+=1
set _location[%count%]=%%a
)
set _location
This is the FFMPEG function I'm using but I now need it to take in each variable separately:
for %%i in (%_location%\*.mp4) do (if not exist "%%~ni\" MD "%%~ni"
ffmpeg -i "%%i" -vframes 1 -f image2 -start_number 0 "%%~ni\%%~ni_Summary_%%3d.jpeg"
)
The %_location% part in the function above is where my issue is? of it not taking multiple variables at once
As it makes no sense to store the whole output into one variable, I assume, you want one variable for each of the lines.
#echo off
setlocal EnableDelayedExpansion
set n=0
for /f "tokens=9" %%a in (file.txt) do (
set /a n+=1
set _Var[!n!]=%%a
)
set _Var[
Note the parantheses after do. This is called a code block and is parsed and executed like a single command. (this is why you need delayed expansion).
EDIT to remove duplicates, you can either use a lot of code to compare the (maybe) new data with each of the already existing variables or use a strange trick: use the data as variable names (and some dummy string as value), so each duplicate will just "overwrite" an already existing variable and in a second step put the data (now variable names) as values into variables.
#echo off
SetLocal EnableDelayedExpansion
REM clear variables:
for /f "delims==" %%a in ('set _') do set "%%a="
REM get locations from textfile:
for /f "tokens=9" %%a in (file.txt) do set "_%%a=yes"
REM translate into proper variables:
set count=0
echo on
for /f "tokens=1* delims==#" %%a in ('set _') do (
set /a count+=1
set x=%%a
set _var[!count!]=!x:~1!
)
set _var
Note: your data shouldn't contain exclamation marks (!) due to delayed expansion.
I have these environment variables : OUTPUT_FOR_TEST1, OUTPUT_FOR_TEST2, OUTPUT_FOR_TEST3 etc.
Also these variables are dynamically constructed in a batch script basically by appending OUTPUT_FOR_ with TEST1, TEST2, TEST3 because I don't know ahead what are TEST1, TEST2, TEST3.
At the end of my batch file, I want to echo these variables. But because these variables are dynamically created, how can I print them all? Can I use a wild card like : echo %OUTPUT_FOR_*% , I don't think so. Any suggestions?
I know power shell can do it, but I prefer to stick to batch only solution if there one exists.
Also to make clear : I want to print just values not the whole environment variable description like OUTPUT_FOR_TEST1=TEST1_RESULT
May be I can use set OUTPUT_FOR_ >> temp.txt, parse temp.txt to get values, but that is more work.
set output_for
this will print all variables which name starts with output_for.
Then you can use (from command line):
for /f "tokens=2* delims==" %# in ('set output_for') do #echo %#
from batch:
for /f "tokens=2* delims==" %%# in ('set output_for') do #echo %%#
or
(for /f "tokens=2* delims==" %%# in ('set output_for') do #echo %%#)>>output
I have a file named like
HelfTool.txt
Code1=Value1
Code2=Value2
I am trying to get the variable named as Code1 and code 2 in cmd batch file with corresponding values. I have written below code but it gives me error stated below.
for /f tokens^=1^,^2^ delims^=^*^=^" %%b in (C:\HelfTool.txt) do if not defined "%%b" set "%%b"=%%c
Environment variable Code1 not defined
Environment variable Code2 not defined
I tried to define these variable at the beginning of the batch file but no use. Can anyone help here.
Your if not defined is wrong - the variable name should not be quoted. It should be
if not defined %%b
Your set command is wrong - it creates a variable with quotes in the name. It should be
set %%b=%%c
or better yet, enclose the entire assignment within one set of quotes:
set "%%b=%%c"
Your FOR /F options are mostly correct, but I do not understand why you took the difficult route of escaping a bunch of characters instead of simply using quotes. Also, I don't think you want to include * as a delimiter. You could have used
for /f "tokens=1,2 delims=="
or better yet (just in case the value contains an =, though it will not preserve a leading = in the value)
for /f "tokens=1* delims=="
But I don't see why you are parsing the line at all, or why you think you must test if the variable is defined yet. It seems to me you could simply use:
for /f "delims=" %%A in (C:\HelfTool.txt) do set "%%A"
Next CLI output could help:
==>for /f tokens^=1^,^2^ delims^=^*^=^" %b in (HelfTool.txt) do #echo set "%b=%c"
set "Code1=Value1"
set "Code2=Value2"
Another approach:
==>for /f "tokens=*" %b in (HelfTool.txt) do #echo set "%b"
set "Code1=Value1"
set "Code2=Value2"
Double the % (percent sign) to use in a .bat batch script, e.g. last command should be
for /f "tokens=*" %%b in (HelfTool.txt) do #echo set "%%b"
I want to set environment variables based on a text file.
The text file has the following format
name=value
In my script i simply iterate the file and perform a set command:
FOR /F "tokens=* delims=" %%A IN ('type test.ini') DO (
SET %%A
)
The problem is that the text file can contain something like
folder=%HOMEPATH%\test
The set command does not evaluate the %HOMEPATH% environment variable.
Instead of \Users\john\test the variables value is %HOMEPATH%\test.
Is there a way do accomplish this?
Thanks!
Use: CALL SET %%A then.
CALL will itself expand %%A and then SET will expand %HOMEPATH%.