Why does this batch variable never change even when set? - windows

#echo off
SET first=0
FOR %%N IN (hello bye) DO (
SET first=1
echo %first%
echo %%N
)
It seems that the variable "first" is always 0. Why?

With batch files, variables are expanded when their command is read - so that would be as soon as the for executes. At that point, it no longer says echo %first%, it literally says echo 0, because that was the value at the point of expansion.
To get around that, you need to use delayed expansion by surrounding your variable name with ! instead of % - so that would be echo !first!. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion in the beginning of your batch file (just after echo off).
If you type set /?, you'll see a much more detailed explanation of this at the end of the output.

Related

Batch file, string variable comparison, if statement

I am trying to compare the logs of a file with a plain string, but it is not getting into if comparison. I am getting " Connect failed" as my 2nd token in echo statement, but not getting any result of IF statement.
#echo off
rem start cmd.exe
for /f "tokens=2 delims=:" %%n IN (C:\Users\rohit.bagjani\Desktop\result\telnetresult.txt) DO (
SET str1 = " Connect failed"
echo %%n
if \i %str1%==%%n echo "true"
)
echo.
pause
The first mistake is in line:
SET str1 = " Connect failed"
This line defines an environment variable with name str1  with a space at end of name with the value  " Connect failed" assigned to it. The leading space and the two double quotes are also assigned to the variable as part of the string.
As the answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? explains in detail, the right syntax would be:
set "str1=Connect failed"
This command line defines an environment variable str1 with the value Connect failed assigned to it.
Run in a command prompt window set /? to get displayed the help for this command on several display pages.
The second mistake is in line:
if \i %str1%==%%n echo "true"
Options/switches are on Windows specified with / and \ is used as directory separator. So the switch for case-insensitive comparison must be /i and not \i.
Run in a command prompt window if /? for help on IF command.
The third mistake is the attempt to define an environment variable within a command block with assigning a string value to the environment variable and reference the value of this environment variable not using delayed expansion in same command block.
Whenever Windows command interpreter encounters an opening round bracket ( being interpreted as begin of a command block, it parses everything to matching parenthesis ) and replaces all environment variable references done with %VariableName% by current value of the environment variable.
In posted code this means the line
if \i %str1%==%%n echo "true"
is changed by Windows command interpreter to
if \i == %n echo "true"
before FOR is executed at all because of environment variable str1 is not defined above the FOR command block.
This can be easily seen by changing echo off to echo on or remove the line with echo off or comment it out with command rem and run the batch file from within a command prompt window. Then Windows command interpreter outputs each command block and each command line after preprocessing before execution.
Double clicking on a batch file to execute it is not good as the window is automatically closed on an exit of batch processing because of a syntax error like this one. The usage of pause is no help as this command line is not reached at all on detecting a syntax error by cmd.exe.
A solution would be:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq tokens=2 delims=:" %%I in ("%USERPROFILE%\Desktop\result\telnetresult.txt") do (
set "str1=Connect failed"
echo %%I
if /I "!str1!" == "%%~I" echo true
)
endlocal
echo/
pause
But much easier and also working would be:
#echo off
for /F "usebackq tokens=2 delims=:" %%I in ("%USERPROFILE%\Desktop\result\telnetresult.txt") do (
echo %%I
if /I "Connect failed" == "%%~I" echo true
)
echo/
pause
For the reason using echo/ instead of echo. to output an empty line see What does an echo followed immediately by a slash do in a Windows CMD file?
The usage of I or any other upper case letter instead of n as loop variable is more safe. Why? Run in a command command window for /? and read the output help explaining also %~nI. On usage of %%~n in a batch file it could be unclear for Windows command interpreter if the current value of loop variable n should be used with surrounding double quotes removed or there is a syntax error as the loop variable is missing after modifier ~n. Loop variables are case-sensitive. The usage of upper case letters avoids conflicts in interpretation of loop variables with modifiers.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
if /?
pause /?
set /?
setlocal /?
See Wikipedia article about Windows Environment Variables for a list of predefined environment variables with description like USERPROFILE.

How can I run a command with a variable? /// How can I produce a random number with with a variable?

I'm aware that set zeroThroughNine=%Random%*9/32768 followed by echo %zeroThroughNine% will produce a a random number between and including 0 and 9. But it seems the interpreter doesn't evaluate the contents of the variable every time it is called, and as such, echo %zeroThroughNine% produces, for example, 7 every time.
I looked up a method for running commands using variables so that I could try to force it to work. I liked the question because it was very basal in its approach; something along the lines of "How can I run commands using variables?", tagged appropriately. I didn't much care for the answer because it was very narrow. The highest voted and selected answer was:
Simple. Just run set commandVar=echo "Hello world.", followed by echo %commandVar%.
Of course the truth is that only works for the echo command. >: [
Anyway I'll stop complaining. This is what I've tried:
set zeroThroughNine=set /a number=%Random%*9/32768 & echo %number% followed by echo %zeroThroughNine%
Unfortunately the & echo %number% section of my SET command runs immediately, producing "%number%" as output --and using echo %zeroThroughNine% produces "set /a number=8436*9/32768", for example, as output.
So two questions: How can I universally achieve running commands with the use of variables (or some alternative method), and perhaps more pressing, how can I achieve producing a new random number at the command line with each new command calling?
You should set number before you set zeroThroughNine to the command, like so:
set /a number=%Random%*9/32768
set zeroThroughNine=echo %number%
%zeroThroughNine%
Also, since zeroThroughNine already is an echo command, you don't need to add the extra echo before it.
EDIT:
Taking into account your Random calculation is needlessly complicated, the final code should be something like this (1 - 10 exclusive):
set /a number=%Random% %% 10
set zeroThroughNine=echo %number%
%zeroThroughNine%
Important thing is, rather than trying to do it all on one line, it is much more readable by separating it into two.
The CALL SET syntax allows a variable substring to be evaluated, the CALL page has more detail on this technique, in most cases a better approach is to use Setlocal EnableDelayedExpansion.
Command line (note that all % percent signs are escaped as ^% and that > and & characters are escaped within a pair of " double quotes:
set "zeroThroughNine=call set /a number=^%Random^% ^% 10>nul & call echo number=^%number^%"
%zeroThroughNine%
for /L %G in (1, 1, 10) do #%zeroThroughNine%
Batch script, CALL method (note that all % percent signs are escaped as %%):
#echo OFF
SETLOCAL
set "_zeroThroughNine=call set /a _number=%%Random%% %%%% 10 & call echo number=%%_number%%"
echo check variables
set _
echo output
%_zeroThroughNine%
for /L %%G in (1,1,10) do %_zeroThroughNine%
echo check variables after evaluating
set _
ENDLOCAL
Batch script, EnableDelayedExpansion only for output:
#echo OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "_zeroThroughNine=set /a _number=!Random! %% 10 & echo Number=!_number!"
SETLOCAL EnableDelayedExpansion
echo check variables
set _
echo output
%_zeroThroughNine%
for /L %%G in (1,1,10) do %_zeroThroughNine%
echo check variables after evaluating
set _
ENDLOCAL
ENDLOCAL
Batch script, EnableDelayedExpansion script wide (note that ! exclamation sign is escaped as ^!):
#echo OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "_zeroThroughNine=set /a _number=^!Random^! %% 10 & echo NUMBER=^!_number^!"
echo check variables
set _
echo output
%_zeroThroughNine%
for /L %%G in (1,1,10) do %_zeroThroughNine%
echo check variables after evaluating
set _
ENDLOCAL
Check out this question. Basically, it's an issue with how the %random% environment variable works...
EDIT:
To elaborate, the reason your random value was always 7 is because of how cmd's pseudo-random number generator works, not because of how the variables are interpreted. The matter is explained very well in this answer.
Essentially, in repeated runs of a batch file, %RANDOM% will produce a value very close to the previous run. Thus, the expression %RANDOM%*9/32768 produces the same result in each separate run because of the random value.
If I understand correctly, the question you're asking is how to better generate a random value 0 - 9 inclusive, which would be by using the following expression:
set /a zeroThroughNine=%RANDOM% %% 10

Cannot set argument value to a variable resulting empty in Windows batch script

I'm looping through all command-line arguments using SHIFT. I'm getting result of ECHO is off.. It is likely printing the empty variable.
:argLoopStart
SET paramName=
SET arg=%1
IF -%arg%-==-- GOTO argLoopEnd
IF %arg:~0,2%==-- (
SET paramName=%arg%
ECHO %arg%
ECHO %paramName%
)
SHIFT
GOTO argLoopStart
:argLoopEnd
By running the command fake-command --dbs=mydbname, I got this:
--dbs
ECHO is off.
According to the code above, ECHO %arg% prints --dbs and ECHO %paramName% prints ECHO is off. The line of SET paramName=%arg% is not working as I expected. %parameName% should print --dbs as well. However, it seems printing an empty variable.
You need to enable delayed expansion with SETLOCAL EnableDelayedExpansion at the top of your script:
Delayed Expansion will cause variables to be expanded at execution
time rather than at parse time, this option is turned on with the
SETLOCAL command. When delayed expansion is in effect variables may be
referenced using !variable_name! (in addition to the normal
%variable_name% )
#echo off
setlocal enabledelayedexpansion
:argLoopStart
set paramName=
set arg=%1
if -!arg!-==-- goto argLoopEnd
if %arg:~0,2%==-- (
set paramName=!arg!
echo !arg!
echo !paramName!
)
shift
goto argLoopStart
:argLoopEnd

How to survive "delayed variable expansion" in a Windows batch script

This is my script:
#echo off
setlocal
for /f %%i in ('echo aaa/') do set REPO=%%i
if "%REPO%"=="" (
echo No input
) else (
echo %REPO:~-1%
echo %REPO:~0,-1%
if %REPO:~-1%==/ set REPO=%REPO:~0,-1%
echo %REPO%
)
endlocal
Please, observe:
c:\dev\shunra\GlobalLibrary\Server>c:\Utils\hgbackup.cmd
/
aaa
aaa/
c:\dev\shunra\GlobalLibrary\Server>
What is going on?
EDIT
Note, that I am assigning to REPO something that evaluates to "aaa", hence I expect it to print "aaa", not "aaa/". It drives me crazy.
EDIT2
Apparently, here is the culprit (from help on the set command):
Finally, support for delayed environment variable expansion has been
added. This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?
Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed. The following example
demonstrates the problem with immediate variable expansion:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "%VAR%" == "after" #echo If you see this, it worked
)
would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement. So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal. Similarly, the following example
will not work as expected:
set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%
in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:
for %i in (*) do set LIST= %i
which just keeps setting LIST to the last file found.
Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time. If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" #echo If you see this, it worked
)
set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%
But I tried using the ! sign, still it does not work for me. I use get ! printed on the screen or the wrong result again.
As has been discussed in the comments, and in your edited question, you need delayed expansion.
Delayed expansion must be enabled before you can use it. Within a batch script you can use setlocal enableDelayedExpansion
#echo off
setlocal enableDelayedExpansion
for /f %%i in ('echo aaa/') do set REPO=%%i
if "%REPO%"=="" (
echo No input
) else (
echo %REPO:~-1%
echo %REPO:~0,-1%
if %REPO:~-1%==/ set REPO=%REPO:~0,-1%
echo !REPO!
)
endlocal
EDIT
The above fails if the IN() clause is changed such that REPO is undefined. For example: in (echo.)
It fails because the entire IF/ELSE construct must have valid syntax, even it the ELSE clause will not be executed.
If REPO is undefined, then
if %REPO:~-1%==/ set REPO=%REPO:~0,-1%
expands to
if ~-1REPO:~0,-1
which is invalid syntax.
The problem again is solved by using delayed expansion.
#echo off
setlocal enableDelayedExpansion
for /f %%i in ('echo.') do set REPO=%%i
if "%REPO%"=="" (
echo No input
) else (
echo %REPO:~-1%
echo %REPO:~0,-1%
if !REPO:~-1!==/ set REPO=%REPO:~0,-1%
echo !REPO!
)
endlocal
Note, that I am assigning to REPO something that evaluates to "aaa"
Actually, you're conditionally assigning something. Have you testing whether the then-part is actually executing (for example, echo If Entered).
This works for me (just an extract from my whole script)
choice /C 1234567H /M "Select an option or ctrl+C to cancel"
set _dpi=%ERRORLEVEL%
if "%_dpi%" == "8" call :helpme && goto menu
for /F "tokens=%_dpi%,*" %%1 in ("032 060 064 096 0C8 0FA 12C") do set _dpi=%%1
echo _dpi:%_dpi%:

Random variable not changing in "for" loop in windows batch file

I'm trying to print out a Random number multiple times but in the for loop I use, it doesn't reset the variable. Here's my code.
#echo off
for %%i in (*.txt) do (
set checker=%Random%
echo %checker%
echo %%i% >> backupF
)
echo Complete
There are 5 text files and so I want it to print 5 different random numbers but it just prints the same random number 5 times. Any help would be greatly appreciated. Thanks!
I'm not sure how you've been able to have it print even one random number. In your case, %checker% should evaluate to an empty string, unless you run your script more than once from the same cmd session.
Basically, the reason your script doesn't work as intended is because the variables in the loop body are parsed and evaluated before the loop executes. When the body executes, the vars have already been evaluated and the same values are used in all iterations.
What you need, therefore, is a delayed evaluation, otherwise called delayed expansion. You need first to enable it, then use a special syntax for it.
Here's your script modified so as to use the delayed expansion:
#echo off
setlocal EnableDelayedExpansion
for %%i in (*.txt) do (
set checker=!Random!
echo !checker!
echo %%i% >> backupF
)
endlocal
echo Complete
As you can see, setlocal EnableDelayedExpansion enables special processing for the delayed expansion syntax, which is !s around the variable names instead of %s.
You can still use immediate expansion (using %) where it can work correctly (basically, outside the bracketed command blocks).
Try by calling a method.
#echo off
pause
for %%i in (*.txt) do (
call :makeRandom %%i
)
echo Complete
pause
:makeRandom
set /a y = %random%
echo %y%
echo %~1 >> backupF
on my system I have to write
set checker=Random
instead of
set checker=!Random!

Resources