how to change existing variable in a configuration file using batch file - batch-file

I am installing software on a client machine, so I have a configuration file having over 100 variables, so I want to change these variables at run time using batch file.
Here are some of variables for example-
file name: config.asp
...
USPSAccessKey=fsfsfs113bh$dd
USPSUserID=1232445
USPSPassword=#########
USPSServiceCode=PRIORITY
USPSServiceCodeFixed=YES
USPSPackageType=VARIABLE
USPSCustomerClassCode=FLAT
...
So there is any way to read this file and then change variables using batch file
Thanks in advance,

Your question is not clear. If you want to modify config.asp file in order to change certain lines with different values, then you may do this:
#echo off
setlocal EnableDelayedExpansion
rem Define new values of desired variables
set newValue[USPSUserID]=5442321
set newValue[USPSPassword]=$$$$$$$$$
(for /F "tokens=1,2 delims==" %%a in (config.asp) do (
set "value=%%b"
if defined newValue[%%a] set value=!newValue[%%a]!
echo %%a=!value!
)) > newConfig.asp

This should do it.
First read the file with a for loop, then modify some values, then write the variables out using set with redirecting the output. Note: I use config2.asp for the output so you can test if it works first.
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%I in (config.asp) do set "%%I"
:: Set some USPS variables here
::
set USPS > config2.asp

Related

How to parse and echo multiple file paths using batch

I created a batch file that will echo or print the path of a file. Apparently, I am unable to print the path when I send multiple files as parameters to the said batch file.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set string=%1
set string=!string:\=%%5C!
#echo %string% > D:\Playground\test.txt
Any help would be much appreciated. If there's anything unclear please let me know. Thanks.
Note: I send files to the batch file using the procedure below:
Right Click File
Send to
Choose the menu item that I created in sendTo that will pass the file to batch file
I don't know if this info helps but I didn't want to leave it out of my question.
%* will catch each input string passed separated by whitespace, enabledelayedexpansion should be used as you do set inside of a code block (). We also enclose the variables, including variable name with double quotes:
#echo off
setlocal enabledelayedexpansion
for %%a in (%*) do (
set "string=%%~a"
set "string=!string:\=%%5C!"
echo !string! >> D:\Playground\test.txt
)
As you can see, you need to use the for loop to iterate each of the input strings i.e %1 %2 and %3 etc.
As a Side note You can also drag and drop files onto the batch file to get results.
EDIT
Added the quote removal as requested set "string=!string:"=!" but note that using the strings as paths without quoting them will cause an issue in future if the paths contains whitespace.
To pipe to file without newline:
#echo off
setlocal enabledelayedexpansion
for %%a in (%*) do (
set "string=%%~a"
set "string=!string:\=%%5C!"
echo|set /p="!string! " >> D:\Playground\test.txt
)

Add all contents of a file in a single variable in Batch

Suppose I have a text file with following contents:
Hello
World
Abc
Now I want to read this in batch and copy them into a single variable. So my variable should contain:
var=Hello World Abc
What is possible work around for this?
If I am trying I am either getting the first word or the last line word.
Thanks
This seems to work. I have appended \n to each line in the read file. I'm not sure how you expect that behavior to actually work:
#echo off
SETLOCAL EnableDelayedExpansion
set "filecontents="
for /f "delims=" %%x in (input.txt) do (
set filecontents=!filecontents!%%x\n
)
echo %filecontents%
To read the contents of a file in a batch file you can use a FOR /F command. You then use the SET command to assign a value to a variable. Also, you will need to utilized delayed expansion to reassign the existing variable to the original variable. A leading space will get assigned so the echo command is using a substring to remove it.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%G IN (file.txt) do set "var=!var! %%G"
echo %var:~1%

Windows shell: get parameter value from file

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.

How define a big array in Batch?

I want to define a big array (>400 keys) in batch, but when I execute my script the windows close. I use this setting:
set FILE_LIST=(filename1.xxx [...] filename450.yyy)
Some help? Thx
A Windows Batch file have a limit in the value of each variable to 8192 characters, including the name of the variable and the equal sign. If the value of each "filename#.xxx " have 16 characters, you may store up to 8192/16=512 file names in one variable; to do that, you must use Batch commands. For example:
#echo off
setlocal EnableDelayedExpansion
set "FILE_LIST="
for /L %%i in (1,1,450) do set "FILE_LIST=!FILE_LIST!filename%%i.xxx "
echo FILE_LIST=%FILE_LIST%
Please, note that previous variable is a list, NOT and array. To define an array, use this method:
#echo off
setlocal EnableDelayedExpansion
for /L %%i in (1,1,450) do set "FILE_ARRAY[%%i]=filename%%i.xxx"
echo FILE_ARRAY:
set FILE_ARRAY
There is a limit of 64 MegaBytes for the total space occupied by all variables.
For a detailed description of arrays and other data structures in Batch files, see: Arrays, linked lists and other data structures in cmd.exe Batch script
EDIT: Reply to the comments
The Batch file below assume that there is one file name per line in the .txt file, and that file names does not include exclamation marks:
#echo off
setlocal EnableDelayedExpansion
rem Load the .txt file in FILE_ARRAY elements:
set num=0
for /F "delims=" %%a in (fileList.txt) do (
set /A num+=1
set "FILE_ARRAY[!num!]=%%a"
)
rem Process the FILE_ARRAY elements:
for /L %%i in (1,1,%num%) do echo Processing: %%i- "!FILE_ARRAY[%%i]!"
I finaly use this way:
set FILE_ARRAY[0]=filename1.xxx
set FILE_ARRAY[1]=filename2.yyy
set FILE_ARRAY[2]=filename3.zzz
for /F "tokens=2 delims==" %%i in ('set FILE_ARRAY[') do (
echo %%i
)
Thanks for your answers.
Seems like you might be hitting a batch file limitation (link), The following link describes a WA for this issue as dumping what you want in a var into a file, then reading that file back in when you need that massive var.
Ah batch, and your endless workarounds...

Windows batch file: loop through parameters and test if param is file on disk?

I am trying to write a batch file that will loop through command-line parameters, test whether each is a valid filename, and if so, set two variables. (It will also do more than this, but one question at a time seems best.)
There is a lot of advice out there about this kind of question, but after many hours, I still can't make it work. I've tried a dozen variations on the basic idea below (which tries to set the path and filename.extension from the parameter if the file exists.
setlocal EnableDelayedExpansion
echo on
FOR %%a IN (%*) DO (
if exist %%a set VDOSPATH="%~dp1"
if exist %%a set VDOSFILE="%~nx1"
echo !VDOSFILE! !VDOSFILE!
)
Basically, everything about this is wrong. It sets the variables whether or not the file exists, and it doesn't seem to loop through the parameters, but keeps setting the same variable. I'm trying it with these parameters (where the second is a file that really does exist)
a_parameter c:\pathto\existingfile.ext another_parameter
I'll be very grateful for any help with this.
Try this:
#echo off
setlocal EnableDelayedExpansion
FOR %%a IN (%*) DO (
if exist %%a set VDOSPATH="%%~dpa"
if exist %%a set VDOSFILE="%%~nxa"
echo(!VDOSPATH! !VDOSFILE!
)

Resources