I have a file C:\parameters.txt that contains different parameters, for example:
env_user=username123
env_pw=password123
env_url=example.com
Now I created a .cmd file that needs to get these values and put them in a variable, for example:
SET var_user=<Here I need 'username123'>
SET var_pw=<Here I need 'password123'>
SET var_url=<Here I need 'example.com'>
How do I write this in my cmd script to get the correct values for my variables?
You need to set a delimiter for = character so that words before/after = will be separated. Besides that, you need an array to set each of the parameters. You can do it like this:
#echo off
setlocal enabledelayedexpansion
set increment=0
for /f "tokens=1* delims==" %%a in (C:\parameters.txt) do (
set parameters_array[!increment!]=%%b
set /a increment+=1
)
echo %parameters_array[0]%
echo %parameters_array[1]%
echo %parameters_array[2]%
pause >nul
Keep in mind, array always starts from 0. You could change to set increment=1 if you prefer the array starts from 1.
Just a slight alternative to dark fang's solution, since your parameters.txt file's contents are already in the format of variable=value, you could
#echo off
setlocal
for /f "usebackq delims=" %%I in ("c:\parameters.txt") do set "%%I"
rem // display env_* variables
set env_
pause
The usebackq option allows you to quote the file name, which might be needed if you ever move c:\parameters.txt to a location containing spaces, ampersands, or other tricksy characters. It's a good habit to follow when reading the contents of text files with for /f.
Also, it's better not to use delayed expansion if you don't need it, as delayed expansion can sometimes corrupt values containing exclamation marks -- a situation that is reasonably possible when dealing with passwords.
I've found the solution thanks to different inputs.
#echo off
For /F "tokens=1* delims==" %%A IN (C:\parameters.txt) DO (
IF "%%A"=="env_user" set var_user=%%B
IF "%%A"=="env_pw" set var_pw=%%B
IF "%%A"=="env_url" set var_url=%%B
)
This will set the correct variables (not local) once the specific code name (before the = in parameters.txt) has been found.
Related
i have a problem, i need a code as in title. purpose of the script: i need to copy the file with a different language code, f.e. XXX_x_xYYY_EN_xx to XXX_x_xYYY_ES_xx and XXX_x_xYYY_DE_xx. So far i have this, but it does not work:
setlocal enableDelayedExpansion
for /l %%H in (1,1,2) do (
set endf[%%H]=!fName[%%H]:*_EN_=!
set trim[%%H]=_EN_!endf[%%H]!
set beginf[%%H]=!fName[%%H]:%trim[%%H]%=!
)
fName is set elswere, its something like this
fName[1]=XXX_x_xYYY_EN_xx
fName[2]=XXXXX_x_xYYYY_EN_x
Everything works except the set beginf(it spits out "beginf[1]=fName[1]:="), i've tryed myriads of % nad ! combos. Fun fuct it works in this case (other script, same puprose but work for only one file in directory, i'd like to make it more versatile):
set beginf=!NAME:%langcode%=!
help :) thanks!
So you have two diferent conversions for two array elements, isn't it? So you just need to match each filename with its corresponding conversion, right?
Something like this, perhaps?
set "conv[1]=ES"
set "conv[2]=DE"
for /l %%H in (1,1,2) do (
for %%c in (!conv[%%H]!) do set "newName[%%H]=!fName[%%H]:_EN_=_%%c_!"
)
I suggest you to read this answer.
EDIT: New method added
This code do exactly the same than the one in your posted answer:
#echo off
setlocal EnableDelayedExpansion
for %%a in (*.xml) do (
set "fName=%%a"
for %%c in (ES DE PL) do (
copy "%%a" "!fName:_EN_=_%%c_!"
)
)
A comparison of your code vs. this one:
You really not need an array of file names. If you create the array just to process its elements once with no further processing, then you are wasting the space occupied by the array. You may do the same thing using an individual file name variable.
Also, you not need an array of conversions. If you just want to repeat a command with several conversions, then it is much simpler to use a list of conversions instead.
It is a bad idea to use special characters as FOR replaceable parameters (where the documentation specifies a letter). Batch files are intrinsically cryptic, so there is no need to include additional complexities...
If you just want to process all files (no rename they) then it is simpler to use a plain for command instead of a for /F one on a 'dir /B' command. The second form requires to execute an additional copy of cmd.exe program and to create a temporary disk file...
If the newfName (array) variable is used just to execute the copy command in the next line, then such a variable is (again) a waste of space. You may create the new name in the copy command itself.
I found the solution :)
Full script with file counting below:
#echo off
setlocal enableDelayedExpansion
set /a count=0
for /f %%# in ('dir *.xml /b') do (
set /a count+=1
set fName[!count!]=%%~xn#
)
set "conv[1]=ES"
set "conv[2]=DE"
set "conv[3]=PL"
for /l %%H in (1,1,!count!) do (
for /l %%G in (1,1,3) do (
for %%c in (!conv[%%G]!) do (
set "newfName[%%G]=!fName[%%H]:_EN_=_%%c_!"
copy !fName[%%H]! !newfName[%%G]!
)
)
)
loops clarification:
for /l %%H - loops through the fName array
for /l %%G - loops through the lang table (conv) array
for %%c - combine above loops and copy files with changed names
thanks #Aacini for your input, it put me on the right tracks :)
Below is my code and i am not able to print the variable in windows batch file that stores an extracted value from CSV file
#echo off
Set _InputFile=D:\TH_Scripts\InputParamTest.csv
for /F "usebackq tokens=* delims=" %%A in (%_InputFile%) do (
set the_line=%%A
goto process_line
)
:process_line
echo i am here
pause
for /F "usebackq tokens=1,2,3,4,5,6,7 delims=[,]" %%1 in (%the_line%) do (
set hexcode=%%1
set country=%%2
set reg=%%3
set owner=%%4
set callsign=%%5
set planetype=%%6
set model=%%7
set THISLINE=%hexcode%,%country%,%reg%,%owner%,%callsign%,%planetype%,%model%
echo %THISLINE% > %THEOUTPUTFILE%
pause
)
for /F "usebackq tokens=1,2,3,4,5,6,7 delims=[,]" %%1 in (%the_line%) do (
%%number is not supported by batch syntax. %n means "the nth parameter to the procedure".
change %%1 to %%a. The variables assigned are then %%a, %%b etc. to %%g note case is important. %%a can be %%i if you like - the values extracted are then assigned to %%i..%%o
2.
You need to search for innumerable articles on delayed expansion on SO. The value of %var% within a code block (parenthesised series of instructions) will be the value that the variable had when the code block was encountered - not the value as it is varied in the block (the "run-time" value)
To extract the run-time value, you need to first invoke delayedexpansion mode by executing a setlocal enabledelayedexpansion instruction (usually done directly after the initial #echo off) and then access the run-time value of the variable by using !var!.
That having been said, unless you are actually using the variables you are establishing in the block, you can directly output your list using %%a, etc, not %hexcode% or !hexcode!. Without further information about how you intend to use these variables elsewhere, this may or may not be useful in your case.
BTW - 1,2,3... is not incorrect syntax, but 1-7 is shorter and means the same in this context.
set var=C:\Users\user\Desktop\bla\bla.exe
set var=%var:*\%
echo %var%
this returns Users\user\Desktop\bla\bla.exe - is there any way to make it focus on the last \ and not the first one so that it would just return bla.exe? bear in mind that this will be used on multiple files and folders so i won't always know how many sub-folders there are.
#ECHO OFF
SETLOCAL
set "var=C:\Users\user\Desktop\blah blah\bla.exe"
FOR %%a IN ("%var%") DO (
SET "filename=%%~nxa"
FOR %%b in ("%%~dpa.") DO SET "lastleaf=%%~nxb"
)
ECHO filename is "%filename%"
ECHO lastleaf is "%lastleaf%"
GOTO :EOF
Normally, the next question is about how to obtain the last leaf of the directory-tree. No subroutines required...
Note positioning of quotes to minimise problems with separators. ALso minor directory name-change to exhibit differences.
The following snippet shows one way to do it.
#setlocal enableextensions enabledelayedexpansion
#echo off
:main
set var=C:\Users\user\Desktop\blah blah\yada yada.exe
call :basename result "%var%"
echo %result%
endlocal
goto :eof
:basename
set %1=%~nx2
goto :eof
It basically calls a function basename (named after the UNIX utility), passing the full name and the variable you want to assign the base name to, and you need to make sure you quote it properly lest filenames containing spaces will cause you problems.
The full set of variable modifiers can be seen in the call /? help output.
Alternatively, you can use the same basename functionality in a one-liner for statement:
for /f "delims=" %%I in ("%var%") do set result=%%~nxI
This allows you to get the base name without having to call a function. I tend to prefer the function myself since it's more readable but you could probably alleviate that by just including a comment:
rem Get base name of var into result:
rem eg: var = C:\Users\user\Desktop\blah blah\yada yada.exe
rem result = yada yada.exe
for /f "delims=" %%I in ("%var%") do set result=%%~nxI
I wanna save the volume label into the variable %volume%.
The normal Syntax would be:
C:\Users\volk>for /f "tokens=1-5*" %%1 in ('vol C:') do set volume=%%6
This way I get an syntax error that says %%1 was unexpected at this time.
So I tried a different solution:
C:\Users\volk>for /f "tokens=1-5*" %1 in ('vol C:') do set volume=%6
C:\Users\volk>set volume=System
C:\Users\volk>set volume=
But this way %volume% is filled with System and in the next step overwritten
with 'nothing' (variable is empty and not existing again).
I´m working in a Windows 8 PE(dont´t know if it´s important for a solution).
for /f "tokens=5,*" %a in ('vol C:') do set volume=%b
works for me. If you get a second setcommand, just filter out the needed lines:
for /f "tokens=5,*" %a in ('vol C:^|find ":"') do set volume=%b
Note: %x is for use at the command prompt. Inside a batch file you need to use %%x
Try to distinguish SET outputs by adding suffix to a variable name, like:
for /f "tokens=1-5*" %1 in ('vol C:') do set volume%2=%6
As long as you can consistently identify this suffix in a 'vol C:' output, you know the variable name storing your Volume name.
In my case %2="in", so my desired variable names "volumein".
I am writing a CMD/batch file (running under Win-7 cmd.exe) and I seem to be getting hung up on delayed variable expansion.
I am using text file input that is in the form:
9 .CN=ISRJX.OU=Linc.OU=thisco.O=UCOM.T=UCOM. 8-20-13
10 .CN=FXXLISHER.OU=Linc.OU=thisco.O=UCOM.T=UCOM. 10-13-13
11 .CN=QXX004F.OU=Linc.OU=thisco.O=UCOM.T=UCOM. 10-14-13
12 *.CN=QXX1001OB.OU=Linc.OU=thisco.O=UCOM.T=UCOM. 10-15-13
as contents in "inputfile.txt". Purpose is to extract the first word after ".CN=", at this point in the process.
Note that I can't strip based on number of chars before "CN=" because the number of chars will vary.
My script is:
setlocal enableextensions enableDelayedExpansion
REM #echo off
for /f "tokens=3 delims==." %%a in (inputfile.txt) do (
set "acct =%%a"
echo. %%a,%acct%
)
endlocal
I've tried every set of combination of quote, !, % etc. and both enabled & disabled delayed expansion, and still can't get this to work. For the most part, when I use ! I end up echoing the actual !. i.e. "echo !acct!" will echo the actual text "!acct!".
I have read many examples and answers, here and elsewhere, about delayed variable expansion. I just can't figure out how to work around it in this example, where I want "acct" to expand within the loop.
Suspect this is a simple punctuation problem, but I'm out of ideas. When I run this, I see acct set to the value for %%a, but when it echoes, clearly it doesn't expand to the new value -- instead it will echo whatever it was set to previously, or blank.
I have also tried disabledelayedexpansion, which makes no difference in my results (including when I use !acct! instead of %acct%.)
Remove the space after the acct variable name and use the ! marks.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "tokens=3 delims==." %%A in (inputfile.txt) do (
set "acct=%%A"
echo. %%A,!acct!
)
endlocal