Windows : print environment variables with wildcard? - windows

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

Related

Looping through all the variables that ends with a character in Batch

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.

How to search a text file for a specific part of a string (line) then insert it into a variable?

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.

cmd for loop variable set issue

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"

Store Multiline String in Batch Variable

I need to remove a string that may occur inside a file. The string has many lines. Can I perform this action using a Batch script?
I've heard that you cant have variables with more than one line in Batch? The string will come from another file that I will read into a variable using Batch.
The following code seems to only store the 1st/last line of a file in the string?? Whats going wrong?
Rem Read file and store all contents in string
Set replace=
Set target=
Set OUTFILE=res.txt
for /f "delims=" %%i in (myFile.txt) do set target=%target% %%i
echo %target%
Rem When I print target I only have one line not many lines?? Whats going wrong
Rem Remove the target string from myOtherFile.txt: this code is from http://stackoverflow.com/questions/5273937/how-to-replace-substrings-in-windows-batch-file
for /f "tokens=1,* delims=ΒΆ" %%A in ( '"type myOtherFile.txt"') do (
SET string=%%A
SET modified=!string:%target%=%replace%!
echo !modified! >> %OUTFILE%
)
Try this:
#echo off &setlocal enabledelayedexpansion
for /f "delims=" %%i in (myFile.txt) do set "target=!target! %%i"
echo %target%
Inside a code block you need always delayed expansion for variables with variable values.
Try this, change last to:
echo !target!

How to read from a .properties file using batch script

I have a requirement where i'd like to read values from a .properties file
my properties file test.properties content
file=jaguar8
extension=txt
path=c:\Program Files\AC
From the above file I need to fetch jaguar or anything after =
Please help me. Thanks
For /F "tokens=1* delims==" %%A IN (test.properties) DO (
IF "%%A"=="file" set file=%%B
)
echo "%file%"
hope this could help
#echo off
FOR /F "tokens=1,2 delims==" %%G IN (test.properties) DO (set %%G=%%H)
echo %file%
echo %extension%
echo %path%
Note that there is no space after %%H. Else this causes a space to be appended, to file paths for example, and will cause file not found errors when the variables from the property files are used as part of a file path.Struggled for hours because of this!
A solution with support for comments (# style). See comments in code for explanation.
test.properties:
# some comment with = char, empty line below
#invalid.property=1
some.property=2
some.property=3
# not sure if this is supported by .properties syntax
text=asd=f
properties-read.bat:
#echo off
rem eol stops comments from being parsed
rem otherwise split lines at the = char into two tokens
for /F "eol=# delims== tokens=1,*" %%a in (test.properties) do (
rem proper lines have both a and b set
rem if okay, assign property to some kind of namespace
rem so some.property becomes test.some.property in batch-land
if NOT "%%a"=="" if NOT "%%b"=="" set test.%%a=%%b
)
rem debug namespace test.
set test.
rem do something useful with your vars
rem cleanup namespace test.
rem nul redirection stops error output if no test. var is set
for /F "tokens=1 delims==" %%v in ('set test. 2^>nul') do (
set %%v=
)
output from set test. (see above):
test.some.property=3
test.text=asd=f
The most important parts are:
the for-loop with the eol and delims option and
the if-checks that both variables %%a and %%b are set.
What you do in the for-loop with the variable and its value is certainly up to you - assigning to some prefixed variables was just an example. The namespacing approach avoids that any other global variable gets overridden.
For example if you have something like appdata defined in your .properties file.
I'm using this to get rid of an extra config.bat and instead using one .properties file for both the java app and some support batch files.
Works for me, but certainly not every edge case is covered here, so improvements welcome!
Try this
echo off
setlocal
FOR /F "tokens=3,* delims=.=" %%G IN (test.properties) DO ( set %%G=%%H )
rem now use below vars
if "%%G"=="file"
set lfile=%%H
if "%%G"=="path"
set lpath=%%H
if "%%G"=="extension"
set lextention=%%H
echo %path%
endlocal
I know this is ancient post but I would like to expand on toschug's great answer.
If the path in the .properties file would be defined as %~dp0 or any other variable that needs to be expanded first before using it, I recommend doing it the following way:
In the .properties file:
path=%~dp0
In the batch file you can then use it the following way (the code is to be used between the two for(s) defining one <=> cleanup one):
if "!test.path!" NEQ "" (
echo Not expanded path: !test.path!
call :expand !test.path! test.path
)
echo test.path expanded: !test.path!
pause
:expand
SET "%~2=%~1"
GOTO :EOF
Don't forget to use (at the start of the batch file):
SETLOCAL ENABLEDELAYEDEXPANSION
you can try this:
#ECHO OFF
#SETLOCAL
FOR /F "tokens=1* delims==" %%A IN (test.properties) DO (
ECHO %%A = %%B
)
#ENDLOCAL

Resources