I have checked several resources on conditional if statement syntax for batch scripts, and despite this, I'm unable to find my mistake. I typically avoid asking questions here that should be this simple, but it's the last task on a huge project so I'm hoping someone can help me see what I'm doing wrong. This is a script for a university project, and is merely for an exploration of concepts.
#Echo OFF
REM Check time remaining
for /f "tokens=3 delims=: " %%a in (
'cscript //nologo "%systemroot%\system32\slmgr.vbs" /dli ^| find "Time remaining: "'
) do set "timeRemainingStatus=%%a"
Echo %timeRemainingStatus%
pause
REM Check SkipRearm dword value
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform"
set VALUE_NAME=SkipRearm
FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
Echo Value Name: %ValueName%
Echo Value Value: %ValueValue%
pause
IF %timeRemainingStatus% EQU 20160 (
Echo First level
pause
IF defined ValueName (
Echo Second Level
pause
IF %ValueValue% == "0x0" (
Echo Third Level
pause
regedit.exe /s "C:\Windows\SR.reg"
)
) ELSE (
Echo Fourth Level
pause
SLMGR /REARM
)
)
pause
I have the echos and pauses in there so I can see what code runs and to verify that the variables are retaining their values. It crashes for me after the second pause (after the Echo Value Value: %ValueValue% command). I assume it's something wrong with syntax, but I've been unable to see any errors because the command prompt closes, despite all the pauses I have.
There were a few things wrong. This is the solution I went with:
#Echo OFF
REM Check time remaining
for /f "tokens=3 delims=: " %%a in (
'cscript //nologo "%systemroot%\system32\slmgr.vbs" /dli ^| find "Time remaining: "'
) do set /a "timeRemainingStatus=%%a"
setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform"
set VALUE_NAME=SkipRearm
FOR /F "usebackq skip=2 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
set ValueName=%%A
set ValueType=%%B
set ValueValue=%%C
)
IF %timeRemainingStatus% EQU 0 (
IF defined ValueName (
IF "%ValueValue%" == "0x0" (
regedit.exe /s "C:\Windows\SR.reg"
)
IF "%ValueValue%" == "0x1" (
SLMGR /REARM
)
)
)
I needed to add /a to set (which I thought I had before, but I guess not). Additionally, I forgot quotes when comparing %ValueValue% to "0x0". While it may not be the cleanest way of doing things, I also changed the if-else into two if statements.
I've tested this at startup on a VM and it appears to be working properly. Much appreciated to the help given.
When setting variables within for loops or if statements, you'll need to take advantage of Setlocal EnableDelayedExpansion. This also applies when referencing variables set in this context. Any variables that use EnableDelayedExpansion will need to have exclamation points around the variable instead of percentages (!Value! instead of %Value%). You'll need to change your code to take advantage of this.
Related
I have a similiar question like I did ask here: Nested loop
However, I still did not solve the "problem" which seems to be little. I already implemented the solution mentioned using [FindStr][2], however the runtime is much much longer than without FindStr. So I would like to keep the method using
IF not "!stringToTest:!searchValue!=!"=="!stringToTest!" echo Something
Below I post some runable code. In the beginning I just set my array with the values I would like to search for and then some exmaple "texts" in which I like to search my beforehand set searchvalues.
My strategy:
Loop through my file with the text lines. For each line test each SearchValue for presence.
After all Searchvalues tested, go to ne next line and check also for the presence of each searchvalue.
So I have two nested Loops. At every step inside the loop I output the current vaule of the variables and those seems to be correct. I really do not know how my fault in this line is as the searchfunction is not working correctly:
IF not "!stringToTest:!searchValue!=!"=="!stringToTest!" echo Searchword is in Textline
There may be just a little mistake in here? I am very thankful for every tipp in this issue.
#echo off
setlocal enabledelayedexpansion
set /a counter=0
set "searchValues=abc,xyz"
FOR %%G in (%searchValues%) do (
set arraySearchVal[!counter!]=%%G
set /a counter+=1
)
REM set arraySearchVal
set /a counter-=1
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
FOR %%G in (%TextWhichNeedToBeSearched%) do (
set "stringToTest=%%G"
echo Textline: !stringToTest!
FOR /l %%I in (0,1,%counter%) do (
set "searchValue=!arraySearchVal[%%I]!"
echo Searchword: !searchValue!
REM echo !stringToTest! found in Textline | findstr "!searchValue!"
IF not "!stringToTest:!searchValue!=!"=="!stringToTest!" echo Searchword is in Textline
)
echo/
)
endlocal
pause
This falls under the K.I.S.S. methodology. By making your search arguments array variables you have over complicated your code. You can simplify it like so.
#echo off
setlocal enabledelayedexpansion
set "searchValues=abc,xyz"
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
FOR %%G in (%TextWhichNeedToBeSearched%) do (
set "stringToTest=%%G"
echo Textline: !stringToTest!
FOR %%I in (%searchValues%) do (
echo Searchword: %%I
IF not "!stringToTest:%%I=!"=="!stringToTest!" echo Searchword is in Textline
)
echo/
)
endlocal
pause
If you really want to use your original code you can get the double variable expansion using a trick with the CALL command to set another variable. Then use that variable with the IF command.
#echo off
setlocal enabledelayedexpansion
set /a counter=0
set "searchValues=abc,xyz"
FOR %%G in (%searchValues%) do (
set arraySearchVal[!counter!]=%%G
set /a counter+=1
)
REM set arraySearchVal
set /a counter-=1
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
FOR %%G in (%TextWhichNeedToBeSearched%) do (
set "stringToTest=%%G"
echo Textline: !stringToTest!
FOR /l %%I in (0,1,%counter%) do (
set "searchValue=!arraySearchVal[%%I]!"
echo Searchword: !searchValue!
REM echo !stringToTest! found in Textline | findstr "!searchValue!"
call set "temptest=%%stringToTest:!searchValue!=%%"
IF not "!temptest!"=="!stringToTest!" echo Searchword is in Textline
)
echo/
)
endlocal
pause
Before getring into this too much, why are you creating a list of counter values if you don't need to? Why bot loop the 1st set and 2nd set wirhoutbhaving special variables for a for L loop, as you don't then assign the positional value.
Another bit, are all of the terms you are looking to see if exist in the text strings partials? Or are you always going to match the full string? If you are matching full strings you only need one loop.
IE if possible strings to match are the Full value, or "whole word" you want to find in the set to look through.
EG
REM this sees if any of the arguments match the list of options using only one loop.
Set "_OptionsToMatch= -D -Delete -R -Remove "
FOR %%_ in (%*) DO (
Echo=%_OptionsToMatch% | FIND /I " %%~_ " )
Assuming the above doesn't work for you in this scenario for some reason
Well lets address the other bits like why are you crwating a valued array at all?
#(echo off
setlocal enabledelayedexpansion
set "searchValues=abc,xyz"
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
)
CALL :Main
( Endlocal
Pause
Exit /B
)
:Main
FOR %%G in (%TextWhichNeedToBeSearched%) do (
SET "stringToTest=%%G"
FOR %%g in (%searchValues%) do (
IF /I "!stringToTest:%%g!" NEQ "%%G" (
Echo The string "%%G" contains "%%g"
) ELSE (
Echo The string "%%G" does not contain "%%g"
)
)
)
Goto :EOF
If all that were needed you could amend the above to make it work to have numbered arrays as it goes along I suppose.
Alternatively if you just want to address what is going on:
Delayed expansion isn't a complete replacement for calling a command or using a function.
This is one of the reasons Inused to only write scripts wirhout delayed expansion as even wirh it on mor complex work still requires calling a function to do the steps as part of the loop.
This is generally when doing lots of value man up such as here.
The alternative, which works as well is to create loops to do the next level of variable manipulation, which is faster? YMMV
Which is simpler - generally calling the sub function.
Which is easier to follow - YMMV.
Here is the sub function method:
#(echo off
setlocal enabledelayedexpansion
set /a "counter=0"
set "searchValues=abc,xyz"
set "TextWhichNeedToBeSearched=tzu,abc,qsd"
)
CALL :Main
( Endlocal
Pause
Exit /B
)
:Main
FOR %%G in (%searchValues%) do (
set /a "counter+=1"
set "arraySearchVal[!counter!]=%%G"
)
FOR %%G in (
%TextWhichNeedToBeSearched%
) do (
set "stringToTest=%%G"
echo Textline: !stringToTest!
FOR /l %%I in (1,1,%counter%) do (
Call set "searchValue=!arraySearchVal[%%I]!"
echo Searchword: !searchValue!
CALL :Fn_Compare
If !_CmpResult! == 0 (
Echo=the string did not exist.
Echo=Even in your loop you can use this result now.
) ELSE (
Echo=the string does exist.
Echo=Even in your loop you can use this result now.
)
)
echo/
)
Goto :EOF
:Fn_Compare
REM echo !stringToTest! found in Textline | findstr "!searchValue!"
IF /I "!stringToTest:%searchValue%=!" NEQ "!stringToTest!" (
echo Searchword is in Textline
Set "_CmpResult=1"
) ELSE (
Set "_CmpResult=0"
)
Goto :EOF
Is this not all you need?
#Echo Off
Set "TextWhichNeedToBeSearched=tzu,abc,qsd"
Set "SearchValues=abc,xyz"
Set "ResultString=Searchword is in Textline"
For %%# In (%SearchValues%)Do (
Echo Textline: %TextWhichNeedToBeSearched%
Echo Searchword: %%#
Echo("%TextWhichNeedToBeSearched%"|FindStr "\<%%#\>">NUL 2>&1&&(
Echo(%ResultString%)||Echo(%ResultString:is=is not%
Echo(
)
Pause
i am trying to make an inventory of installed software in domain PCs by following reg command
Reg Query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall /S ^| Find /I "DisplayName"
I am using Find /I "DisplayName" to get software-name. I also need the version number (and may be few more fields later), but its in another line as DisplayVersion.
Since I am running this on multiple computers, my script looks like this:
for /f %%i in (computers_ALL.txt) do (
for /f "tokens=1,2,*" %%j in ('psexec \\%%i Reg Query HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall /S ^| Find /I "DisplayName"') do (
echo x64 %%i %%l >>%OUTPUT_FILE%
)
for /f "tokens=1,2,*" %%j in ('psexec \\%%i Reg Query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /S ^| Find /I "DisplayName"') do (
echo x86 %%i %%l >>%OUTPUT_FILE%
)
)
Now I can only find DisplayName. How can I find DisplayVersion which is on another line and add it to second column? My output will be like,
ComputerName, Platform (32-64 bit), Software Name, Software Version
I can take care upto software-name, but having difficulty to get version and put it on second column. Appreciate your help. Thanks.
Following script works against a local Windows machine. Adapting it for a list computers_ALL.txt instead of "%computername%" and adding psexec \\%_comp% should be a simple task.
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "OUTPUT_FILE=%temp%\41887529.txt"
for /F %%i in ("%computername%") do (
set "_comp=%%i"
call :proHive "x64" "\Software\Microsoft\Windows\CurrentVersion\Uninstall"
call :proHive "x86" "\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
)
rem debugging output
type "%OUTPUT_FILE%"
ENDLOCAL
goto :eof
:proHive
rem %1 platform
rem %2 registry key
>>"%OUTPUT_FILE%" (
for /f "tokens=*" %%G in ('
Reg Query "HKLM%~2" /S /V DisplayName ^| Find /I "HKEY_LOCAL_MACHINE%~2"
') do (
set "_DisplayName="
set "_DisplayVersion="
for /f "tokens=1,2,*" %%j in ('
Reg Query "%%G" ^| Findstr /I "DisplayName\> DisplayVersion\>"') do set "_%%j=%%l"
SETLOCAL EnableDelayedExpansion
echo %_comp%,%~1,!_DisplayVersion!,!_DisplayName!
ENDLOCAL
)
)
goto :eof
#ECHO OFF
SETLOCAL
SET "output_file=u:\report.txt"
DEL "%output_file%" /F /Q
for /f %%i in (q41887529.txt) do (
SET "compname=%%i"
CALL :clrsoft
FOR %%z IN (\ \Wow6432Node\) DO (
for /f "tokens=1,2,*" %%j in (
'Reg Query HKLM\Software%%zMicrosoft\Windows\CurrentVersion\Uninstall /S ^| Findstr /I "DisplayName DisplayVersion HKEY_LOCAL_MACHINE\\"'
) do (
IF /i "%%j"=="Displayname" (
SET "softname=%%l"
) ELSE (
IF /i "%%j"=="Displayversion" (
SET "softver=%%l"
) ELSE (
CALL :report %%z
)
)
)
REM processed all data - final report
CALL :report %%z
)
)
GOTO :EOF
:: Report routine parameters :
:: %1 indicates x64 ("\") or x86 (\Wow6432Node\)
:report
IF NOT DEFINED softname GOTO clrsoft
IF "%1"=="\" (
ECHO "%compname%",x64,"%softname%","%softver%">>"%output_file%"
) ELSE (
ECHO "%compname%",x86,"%softname%","%softver%">>"%output_file%"
)
:clrsoft
SET "softname="
SET "softver="
GOTO :EOF
Naturally, you'd need to set up your own output filename. I've also removed the psexec \\%%i and set up my machine name in q41887529.txt for testing on my machine.
Since the only difference between the queries is the node, I combined the steps and controlled them using %%z.
Clear the tempvars, then process each return line for one of the three target strings.
If it's a name, then call :report to report any accumulated data, then record the name
If it's a version, record the version and since this should now give us a pair, report it.
If it's neither, then it must be a new key, so report what we have so far.
The wart is that the version need not exist - and the final entry may be versionless, so we try reporting again to ensure any accumulated data is output.
I've revised this now and replaced the original.
It will report the machine name extracted from the file and I believe that the often-missing version number has been fixed - the order of name and version are unpredictable, so the report is triggered with the data accumulated between instances of HKEY_ and at the very end.
I've been trying to make a batch file that makes folders named after the people on a .txt file list, and then gives them full access to modifying their own personal folder.
The problem is that I keep getting a ' delims= " was unexpected at this time' error.
Here's my code here, I was wondering if you guys would be able to find out what I did wrong, thanks ^-^
(Btw I haven't added the permissions part yet, I just need to get this part sorted out first)
CODE: http://pastebin.com/XLi11nZa
NAMES LIST: http://pastebin.com/xbh3WTSv
#echo off
color A
echo What is the name of list file? (Do not include format)
SET /P list=
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" %list%.txt | find /C ":""
for /f %%a in ('!cmd!') do set m=%%a
SET c=0
echo !m! folders to be created. Continue? (Y/N)
SET /P ANSWER=
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)
exit
:yes
echo Now creating %m% folders.....
for /f "eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
SET /a c = !c! + 1
mkdir "%%i"
echo !c!/%m% folders created [%%i]
)
endlocal
echo Now adding permissions to %m% folders.....
pause
setlocal enabledelayedexpansion
SET c1=0
for /f "eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
SET /a c1 = !c1! + 1
SET word=1
SET /a showme=c1-1
SET showme=skip=%showme%
IF !c1! equ 1 set "showme= "
FOR /F "tokens=%word% %showme% delims= " %%F IN ("%list%") DO if defined
showme set showme=%%F
SET first=%showme:~0,1%
SET word=2
SET /a showme1=c1-1
SET showme1=skip=%showme1%
IF %c1% equ 1 set "showme1= "
FOR /F "tokens=%word% %showme1% delims= " %%L IN ("%list%") DO if
defined showme1 set showme1=%%L
set B=%showme1%%first%
set _STRING=%B%
set "_UCASE=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "_LCASE=abcdefghijklmnopqrstuvwxyz"
for /l %%a in (0,1,25) do (
call set "_FROM=%%_UCASE:~%%a,1%%
call set "_TO=%%_LCASE:~%%a,1%%
call set "_STRING=%%_STRING:!_FROM!=!_TO!%%
)
set _STRING
echo %_STRING%
echo %_STRING%>>testing.txt
endlocal
pause
)
names list
Loralee Stucky
Tomas Silberman
Marleen Rosell
Phyllis Steier
Elmo Jetter
Kristyn Spruell
Willetta Vandermeer
Hazel Alsobrook
Naida Nixon
Nadia Godfrey
Lavonna Antunez
Mac Castile
Tamela Stover
Piedad Heidrick
Hien Welsh
Carolin Gularte
Mariko Tolentino
Alia Graddy
Deadra Rehkop
Donella Pittman
Replace
for /f "eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
by
for /f "usebackq eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
twice.
Without the usebackq option, a double-quoted set of for /F is interpreted as a literal string rather than a file. Removing the double-quotes could work but could also lead to problems with files that have spaces in their names.
Another thing: you are dynamically creating the skip=# option for for /F, where # stands for a number. you have to make sure that this number is always positive, so 0 is not understood by for /F which could also lead to your error message.
So you could add an if check whether the number is greater than zero and do not add the skip option otherwise (by clearing your showme variables).
And last but not least: the delayed expansion is not always used properly: sometimes in the block startung at the for command in line #26 of your code and reaching to the end, you are not consistently using !! for expansion of variables showme, showme1 and c1, which are the ones that are modified within that code block.
The folders will get created proper if you remove the double quotes from the filename variable for /f "eol=; tokens=1 delims=," %%i in (%list%.txt) do ( change that line and you'll get past this part.
You may want to do the same in the permissions section.
I'm using Batch to create a kind of 'AI' program. It's pretty basic and i'm fine with that, but is there anyway to make it so if I open up the program and tell it to save something (text)? Then have it load up when i type in a certain thing next time i start the program?
Well not sure if this is what you want... 'cause you don't specify if want "Human AI" or a AI for other kind of bot-Task.
This is only a examples that I wrote in few minutes but maybe this can help you to start:
#Echo off
:Ask
Echo+
SET /P "Question=>>[YOU] "
Call :Answer "%Question%"
GOTO:ASK
:Answer
Echo+
For /F "Usebackq Tokens=1,* Delims=;" %%A in ("AI.txt") Do (
For %%# in (%Question%) DO (
Echo "%%A"| FINDSTR /I "\"%%#\"" 1>NUL && (
Echo: [AI] %%B
GOTO:ASK
)
)
)
Echo: [AI] I don't understand you.
GOTO:EOF
AI.txt content:
matrix;Don't talk about it if you don't awake...
pennys;You said pennys? That remembered me I'm so hungry... ;)
dog;I like it!
name;My name is Bachitia, yes it's cool...
PART 2
This is another kind of AI...
#Echo off
Setlocal enabledelayedexpansion
Set /A "INDEX=1"
REM :Check_Question {INDEX} {Response if answer is not saved} {Response if answer is saved}
Call :Check_Question "_%INDEX%" "Hello, I'm glad to meet you, What's your name? " "Welcome again {Value}"
Call :Check_Question "_%INDEX%" "What are your hobbies? " "Are you {Value} again? "
Pause&Exit
:Check_Question
If not exist "AI.txt" (Echo _0;DUMMY>"AI.txt")
For /F "Usebackq Tokens=1,* Delims=;" %%A in ("AI.txt") Do (
If "%%A" EQU "%~1" (
Set "Answer=%~3"
Set "Answer=!Answer:{Value}=%%B!"
Echo !Answer!
Set /A "INDEX+=1"
Exit /B 0
)
)
Set /P "Question=%~2"
Echo _%INDEX%;%Question%>>"AI.txt"
Set /A "INDEX+=1"
Exit /B 0
First run:
Ai.txt auto generated content:
_0;DUMMY
_1;Elektro
_2;programming
Second run:
I made this code
dir /B /S %RepToRead% > %FileName%
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo %z%
echo %%a
)
echo %%a is working fine but echo %z% returns "echo disabled".
I need to set a %z% because I want to split the variable like %z:~7%
Any ideas?
There are two methods to setting and using variables within for loops and parentheses scope.
setlocal enabledelayedexpansion see setlocal /? for help. This only works on XP/2000 or newer versions of Windows.
then use !variable! instead of %variable% inside the loop...
Create a batch function using batch goto labels :Label.
Example:
for /F "tokens=*" %%a in ('type %FileName%') do call :Foo %%a
goto End
:Foo
set z=%1
echo %z%
echo %1
goto :eof
:End
Batch functions are very useful mechanism.
You probably want SETLOCAL ENABLEDELAYEDEXPANSION. See https://devblogs.microsoft.com/oldnewthing/20060823-00/?p=29993 for details.
Basically: Normal %variables% are expanded right aftercmd.exe reads the command. In your case the "command" is the whole
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo %z%
echo %%a
)
loop. At that point z has no value yet, so echo %z% turns into echo. Then the loop is executed and z is set, but its value isn't used anymore.
SETLOCAL ENABLEDELAYEDEXPANSION enables an additional syntax, !variable!. This also expands variables but it only does so right before each (sub-)command is executed.
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo !z!
echo %%a
)
This gives you the current value of z each time the echo runs.
I struggeld for many hours on this.
This is my loop to register command line vars.
Example : Register.bat /param1:value1 /param2:value2
What is does, is loop all the commandline params,
and that set the variable with the proper name to the value.
After that, you can just use
set value=!param1!
set value2=!param2!
regardless the sequence the params are given. (so called named parameters).
Note the !<>!, instead of the %<>%.
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%P IN (%*) DO (
call :processParam %%P
)
goto:End
:processParam [%1 - param]
#echo "processparam : %1"
FOR /F "tokens=1,2 delims=:" %%G IN ("%1") DO (
#echo a,b %%G %%H
set nameWithSlash=%%G
set name=!nameWithSlash:~1!
#echo n=!name!
set value=%%H
set !name!=!value!
)
goto :eof
:End
Simple example of batch code using %var%, !var!, and %%.
In this example code, focus here is that we want to capture a start time using the built in variable TIME (using time because it always changes automatically):
Code:
#echo off
setlocal enabledelayedexpansion
SET "SERVICES_LIST=MMS ARSM MMS2"
SET START=%TIME%
SET "LAST_SERVICE="
for %%A in (%SERVICES_LIST%) do (
SET START=!TIME!
CALL :SOME_FUNCTION %%A
SET "LAST_SERVICE=%%A"
ping -n 5 127.0.0.1 > NUL
SET OTHER=!START!
if !OTHER! EQU !START! (
echo !OTHER! is equal to !START! as expected
) ELSE (
echo NOTHING
)
)
ECHO Last service run was %LAST_SERVICE%
:: Function declared like this
:SOME_FUNCTION
echo Running: %1
EXIT /B 0
Comments on code:
Use enabledelayedexpansion
The first three SET lines are typical
uses of the SET command, use this most of the time.
The next line is a for loop, must use %%A for iteration, then %%B if a loop inside it
etc.. You can not use long variable names.
To access a changed variable such as the time variable, you must use !! or set with !! (have enableddelayexpansion enabled).
When looping in for loop each iteration is accessed as the %%A variable.
The code in the for loop is point out the various ways to set a variable. Looking at 'SET OTHER=!START!', if you were to change to SET OTHER=%START% you will see why !! is needed. (hint: you will see NOTHING) output.
In short !! is more likely needed inside of loops, %var% in general, %% always a for loop.
Further reading
Use the following links to determine why in more detail:
Difference between %variable% and !variable! in batch file
Variable usage in batch file
To expand on the answer I came here to get a better understanding so I wrote this that can explain it and helped me too.
It has the setlocal DisableDelayedExpansion in there so you can locally set this as you wish between the setlocal EnableDelayedExpansion and it.
#echo off
title %~nx0
for /f "tokens=*" %%A in ("Some Thing") do (
setlocal EnableDelayedExpansion
set z=%%A
echo !z! Echoing the assigned variable in setlocal scope.
echo %%A Echoing the variable in local scope.
setlocal DisableDelayedExpansion
echo !z! &rem !z! Neither of these now work, which makes sense.
echo %z% &rem ECHO is off. Neither of these now work, which makes sense.
echo %%A Echoing the variable in its local scope, will always work.
)
set list = a1-2019 a3-2018 a4-2017
setlocal enabledelayedexpansion
set backup=
set bb1=
for /d %%d in (%list%) do (
set td=%%d
set x=!td!
set y=!td!
set y=!y:~-4!
if !y! gtr !bb1! (
set bb1=!y!
set backup=!x!
)
)
rem: backup will be 2019
echo %backup%
Try this:
setlocal EnableDelayedExpansion
...
for /F "tokens=*" %%a in ('type %FileName%') do (
set z=%%a
echo !z!
echo %%a
)
You can use a macro if you access a variable outside the scope
#echo off
::Define macro
set "sset=set"
for /l %%a in (1,1,4) do (
::set in loop
%sset% /a "x[%%a]=%%a*%%a"
if %%a equ 4 (
:: set in condition
%sset% "x[%%a]=x Condition"
%sset% "y=y Condition"
)
)
echo x1=%x[1]% x2=%x[2]% x3=%x[3]% x4=%x[4]% y=%y%
:: Bonus. enableDelayedExpansion used to access massive from the loop
setlocal enableDelayedExpansion
echo Echo from the loop
for /l %%a in (1,1,4) do (
::echo in one line - echo|set /p =
echo|set /p "=x%%a=!x[%%a]! "
if %%a equ 4 echo y=%y%
)
pause
I know this isn't what's asked but I benefited from this method, when trying to set a variable within a "loop". Uses an array. Alternative implementation option.
SETLOCAL ENABLEDELAYEDEXPANSION
...
set Services[0]=SERVICE1
set Services[1]=SERVICE2
set Services[2]=SERVICE3
set "i=0"
:ServicesLoop
if defined Services[%i%] (
set SERVICE=!Services[%i%]!
echo CurrentService: !SERVICE!
set /a "i+=1"
GOTO :ServicesLoop
)
The following should work:
setlocal EnableDelayedExpansion
for /F "tokens=*" %%a in ('type %FileName%') do (
set "z=%%a"
echo %z%
echo %%a
)