windows command line evaluate inner variable expression - windows

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

Related

How to loop an array in batch and move files

I have the following:
#echo off
set Arr[0]=1234
set Arr[1]=2351
set Arr[2]=4321
set Arr[3]=6106
How can I loop in an array? Also at each loop I want to see if a file exists that contains like a wildcard the number of the Array. For example for Arr[0] if there's a file that contains 1234 like ABC_1234.txt or 1234_ABC.txt or 1234.txt then returns true otherwise false.
I'm a complete beginner in batch so any help appreciated.
Given your provided example variable value strings, I'd suggest this simple sort of approach. It parses the variable names defined within the environment, via the Set command.
#Echo Off
SetLocal EnableExtensions
Rem Ensure that there are no existing Arr[n] variables.
For /F "Delims==" %%G In ('2^> NUL Set Arr[') Do Set "%%G="
Rem Define new Arr[n] variables.
Set "Arr[0]=1234"
Set "Arr[1]=2351"
Set "Arr[2]=4321"
Set "Arr[3]=6106"
Rem Loop through the Arr[n] variables.
For /F "Tokens=1,* Delims==" %%G In ('2^> NUL Set Arr[') Do Echo "%%H"
Pause
I've just used Echo "%%H" in the loop, you'd obviously replace that with your If Exist, Dir etc. command to search for the files with respect to those variable value strings.

Batch script remove everything after a dynamic text from the string/filename

I have a string containing filename like below batch script lines:
set "file1=name.php_2017_23_08_12_22_07"
set "file2=name.vbs_2016_16_12_12_13_03"
....
Now as you see above, the list of filenames and their extensions is variety and I need to get the real filenames with their actual extensions (.php, .vbs) respectively. So when I pass in a function with filename with extension I should get actual filenames like below:
call :restoreFile %file2% ".vbs"
should give me output, removing everything after .vbs in the variable %file2%:
name.vbs
And so on for other variables too.
How can set a function like that, which strips a dynamic substring from filename string ?
I tried the below code, but that doesn't work with dynamic substring or text, which can be substitued from variable:
set targetfile=%file2:.vbs=&rem.%
No need to specify the extension - it's still in the name.
Use a for /f to split at the first underscore.
:restoreFile
Set "FileName="
For /f "tokens=1* delims=_" %%N in ("%~1") Do Set "FileName=%%N"
If defined FileName Echo Ren %1 %FileName%
Goto :Eof
You may even put the date_time in front of the extension and so avoid possible dupes:
:restoreFile
Set "FileName="
For /f "tokens=1* delims=_" %%N in ("%~1") Do Set "FileName=%%~nN_%%M%%~xN"
If defined FileName Echo Ren %1 %FileName%
Goto :Eof
The ren commands are prepended with an echo, if the output looks OK remove the echo.

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.

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!

Resources