Batch File Setting an Environment Variable - batch-file

I currently have this code in my batch file to store a value in variable %%i:
FOR /F %%i in (AylaUnits.txt) DO set %%i
set "$Unit=%%i"
echo The unit is : %$Unit%
Pause
This seems to loop fine, but for each item I get this response:
set AC000W000004591
Environment variable AC000W000004595 not defined
The unit is : %i
There is going to be 100+ different lines in the text file. Will I need to create an environmental variables for each one? I don't think that's the best way to do it and I'm not sure I'm understanding environmental variables correctly.
I need to plug each line of the file into a url section if that's of any importance.
Thanks,
DM

You have a syntax issue, see the proposed change below:
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F %%i in (AylaUnits.txt) DO (
set "$Unit=%%i"
echo The unit is : !$Unit!
)
Pause
Please see the edit above, since we're setting the variable inside the for loop, we need to use SETLOCAL ENABLEDELAYEDEXPANSION to be able to expand on it. Since we're expanding the var that's set in the for loop, we need to use ! instead of % when using those variables.

Related

Creating a batch script which allows to create specific folders given by the user

I would like to create a batch script which allows to create number and name of folder chosen by the user.
However, I received a syntax error using the script below.
variable "nome" is not taken.
Here is my code:
echo How many folders?
set /p cc=tell me how many
SETLOCAL EnableDelayedExpansion
FOR /L %%G IN (1,1,%cc%) DO (set /p nome=tell me the name
md %nome%)
pause
mr xyz, please use the search bar to search for delayed expansion. It's not that hard.
What Is DelayedExpansion
Batch-file variables are expanding in the moment the line is parsed. That means the nome variable isn't set when the entire for loop is parsed.
How-To Make The Variable Expand At Run-time?
SETLOCAL Method
Add
setlocal enableDelayedExpansion
anywhere before the for loop, so cmd will process the variable at run-time. And change %nome% to !nome!.
CALL MKDIR/MD Method
Change your MD statement to:
call MD %%nome%%
As this will trigger the emulated delayed expansion using the special property of call and %%.

set PATH with multiple lines

In a Batch file I need to add some paths to the PATH env variable. Since its a larger numer of long paths I tried to spread them on multiple line and tried to make the bat-file as clean as I can by indenting the lines.
But it seems that the spaces at the beginning of the newline (and so in the %PATH%) are interpreted as part of the actual path.
So
SET PATH=^
\\somewhere\Tools\strawberryperl\perl\site\bin;^
\\somewhere\Tools\strawberryperl\perl\bin;^
\\somewhere\Tools\strawberryperl\c\bin;^
\\somewhere\Tools\KDiff3;^
%PATH%
does not work (programs are not found). Is there some trick I can use?
Because it is a medium complex batch file some indentation would be nice.
for %%x in (
"\\somewhere\Tools\strawberryperl\perl\site\bin;"
"\\somewhere\Tools\strawberryperl\perl\bin;"
"\\somewhere\Tools\strawberryperl\c\bin;"
"\\somewhere\Tools\KDiff3;"
) do call set "path=%%path%%%%~x"
this will append the extra items to the path. You'd need to initialise path to nothing first if all you want is to build the directory sequence specified.
There is no way to have PATH ignore the leading spaces. I see two possible options if you want indented lines:
Option 1 - Use undefined variable to indent, so spaces never get in the value
#echo off
SET PATH=^
% =%\\somewhere\Tools\strawberryperl\perl\site\bin;^
% =%\\somewhere\Tools\strawberryperl\perl\bin;^
% =%\\somewhere\Tools\strawberryperl\c\bin;^
% =%\\somewhere\Tools\KDiff3;^
% =%%PATH%
Option 2 - Remove the spaces afterwards
#echo off
SET PATH=^
\\somewhere\Tools\strawberryperl\perl\site\bin;^
\\somewhere\Tools\strawberryperl\perl\bin;^
\\somewhere\Tools\strawberryperl\c\bin;^
\\somewhere\Tools\KDiff3;^
%PATH%
set "PATH=%PATH:; =%"
First let me start by informing you that adding to the PATH variable in this way is ONLY for the running session. Once the cmd session is closed that variable returns to its previous value.
Here are a suggestion, append each addition one by one:
SET "ToAdd=\\somewhere\Tools\strawberryperl\perl\site\bin;"
SET "ToAdd=%ToAdd%;\\somewhere\Tools\strawberryperl\perl\bin;"
SET "ToAdd=%ToAdd%;\\somewhere\Tools\strawberryperl\c\bin;"
SET "ToAdd=%ToAdd%;\\somewhere\Tools\KDiff3"
SET "PATH=%PATH%;%ToAdd%"
BTW, if you were hoping to add to the environment variable beyond the running session then it is important that you ignore anyone suggesting you use SETX instead of SET. (the variable will be truncated at 1024 bytes therefore corrupting it). Your best solutions would involve editing the registry and possibly using a built in tool such as powershell.
Edit
This shows the method mentioned in my comment and uses the same structure as Magoo's answer:
C:\MyDir\Paths.txt
\\somewhere\Tools\strawberryperl\perl\site\bin
\\somewhere\Tools\strawberryperl\perl\bin
\\somewhere\Tools\strawberryperl\c\bin
\\somewhere\Tools\KDiff3
batch file
#Echo Off
SetLocal EnableDelayedExpansion
For /F "UseBackQDelims=" %%A In ("C:\MyDir\paths.txt") Do Set "Path=!Path!;%%~A"
Echo(%Path%
EndLocal
Timeout -1
This means that you only really need to include the for loop each time instead of adding each of the paths to it.
Not even remotely bulletproof, but the Magoo's answer reminded me this. Just because someone, somewhere, could find a better usage for this construct
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "delims=" %%a in ('echo "%path:;=" "%"
"\\somewhere\Tools\strawberryperl\perl\site\bin"
"\\somewhere\Tools\strawberryperl\perl\bin"
"\\somewhere\Tools\strawberryperl\c\bin"
"\\somewhere\Tools\KDiff3"
""') do (set "path=%%~a") & call set "path=%%path:" "=;%%"
path

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!
)

how to change existing variable in a configuration file using 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

Find and Replace not working for batch file

Ok so I am creating a script with a built in updater, it creates a new file with the following code and updates several variables, but for some reason this isn't working anyone have any idea how to fix it or a similar script that will do roughly the same thing.
#echo off
setlocal enabledelayedexpansion
set /p "findthis"="1"
set /p "replacewith"="1.2.3"
call:updater
set /p "findthis"="2"
set /p "replacewith"="2.3.4"
call:updater
set /p "findthis"="3"
set /p "replacewith"="3.4.5"
call:updater
goto:eof
:updater
for /f "tokens=*" %%a in (updateme.bat) do (
set write=%%a
if %%a==%findthis% set write=%replacewith%
echo !write!
echo !write! >>%~n1.replaced%~x1
)
goto:eof
there are several errors in this BAT.
Some are obvious syntax errors.
Read help set and correct all the set /p "this"="value" (hint: don't use /p option and correct the usage of " in the variable name)
you try to use %1 in a CALLed label. This is a passed parameter, and you are not passing it in your CALL. Read HELP CALL.
Some are logic errors.
The :updater code appends the updated string to the output file. It does so three times, so the final code is three times the original code with the strings changed.
Also, the code does try to find the string as a full line, a line containing just "1" in a BAT file does not make too much sense to me. You would probably want to find any text occurrence of "1".
Also, when you fix the previous problems, and if I understand correctly the intention of the code, you will eventually replace all "1" to "1.2.3" and then you replace all "2" to "2.3.4", so the original "1" will get replaced by "1.2.3.4.3".. and later on again, so it will finally be "1.2.3.4.5.4.3.4.5". Be careful with that.

Resources