Batch for loop prints the same number multiple times - loops

I'd like to write a batch file which will display numbers from 1 to 5.
Here is my code:
#ECHO OFF
FOR /l %%A in (1,1,5) do (
SET number=%%A
ECHO %number%
)
And here is an output:
5
5
5
5
5
What is wrong with my code?

You are missing ENABLEDELAYEDEXPANSION and !number! instead of %number%.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /l %%A in (1,1,5) DO (
SET number=%%A
ECHO !number!
)
When variables are changed inside loop or if structures you always have to work with ENABLEDELAYEDEXPANSION and access the variables as !var! instead of %var%.

Related

Why FOR loop with "do (looped content)" not working?

I really don't understand why the first batch script is working and second not. I need to use the second batch script as I need to add more conditional inside the loop.
The script counts the number of lines in .txt file and make a separate variable for each of the lines
this version is working
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=2 delims='" %%B in ('type "output-mp3.txt"') do set /a "nlines+=1+0" && <con: set "_line!nlines!=%%~nB"
echo line 1 !_line1!
echo line 2 !_line2!
echo total number of lines !nlines! and name of the song !_line2!
pause
I need the add more things on the for loop so I need do (...) but is not working ..
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=2 delims='" %%B in ('type "output-mp3.txt"') do (
set /a "nlines+=1+0" && <con: set "_line!nlines!=%%~nB"
echo line 1 !_line1!
echo line 2 !_line2!
echo total number of lines !nlines! and name of the song !_line2!
pause
)
output-mp3.txt contain
file '...\intro\Canal-1\Videos-10.mp3'
file 'F...Canal-1\Videos-10\0HDW0N3Q.mp3'
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=2 delims='" %%B in ('type "output-mp3.txt"') do (
set /a "nlines+=1+0"
<con: set "_line!nlines!=%%~nB"
)
set _line
works for me - regardless of the number of lines in the file.

Count Directory Files then Test Numeric values

Using the code below I am able to count the number of files in a directory and call another batch file if the number of files in the directory equals (EQU or ==) say 20
When i use LSS i run into a few problems. The results are problematic and i am getting unexpected results depending on how many files are in the directory or the value of LSS
If for example i have 9 files in my directory and LSS set for say 15 the call command doesn't work.
Is there a way to fix this possible number Vs string issue. I have also tried using "" around the numbers but still no luck. Any help would be appreciated
#ECHO OFF
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=0
for %%o IN (C:\test1\*.*) DO (
echo %%o
SET /A count=count + 1
)
echo %count%
IF %count% LSS 20 call RunAll.bat
ENDLOCAL ENABLEDELAYEDEXPANSION
ENDLOCAL
you setlocal enabledelayedexpansion but you do not use its functionality. It makes more sense to wrap your if in double quotes or square brackets.
Lastly, no need to call RunAll.bat just can it without call.
#echo off
setlocal enabledelayedexpansion
set count=0
for %%o in (F:\CSV\*) do (
echo %%o
set /A count=count + 1
)
echo !count!
if "!count!" LSS "20" RunAll.bat
endlocal enabledelayedexpansion

Windows Batch:Nested for loop counter not working

I'm a novice at batch and been trying to get this to work properly but can't figure out what I'm doing wrong. The counter doesn't increment for some reason?
#echo off
set local enabledelayedexpansion
set /a counter=0
for /F "delims=" %%a in ('dir /b/ad/o "C:\Sources"') do (
for /F "delims=" %%i in ('dir /b/ad/o "C:\Sources\%%a"') do (
set a[%counter]=%%i
set /a counter=counter+1
echo value of counter is: %counter%
)
)
echo array 0 is: %a[0]%
echo array 1 is: %a[1]%
The SET LOCAL should really be SETLOCAL. It's a single command. Also the nested variables should be refernced with !var! and not %var%. If you use %var% it will use the outer scope (and not work correctly).
https://ss64.com/nt/delayedexpansion.html
#ECHO OFF
REM "SETLOCAL" not "SET LOCAL"
SETLOCAL enabledelayedexpansion
SET counter=0
FOR /L %%a IN (1,1,10) DO (
FOR /L %%i IN (1,1,10) DO (
REM USE "!" instead of "%"
SET X[!counter!]=%%i
SET /a counter=!counter! +1
echo value of counter is: !counter!
)
)
ECHO array 0 is: %X[0]%
ECHO array 1 is: %X[1]%
BTW, if you want to have your variables just scoped to your batchfile you should end your script with ENDLOCAL

Perform arithmetic in loop with variables in Batch

I would like to print the following:
0
1
2
3
4
I have tried this:
ECHO OFF
FOR /L %%A in (1,1,5) DO (
SET /a "B=%%A-1"
ECHO %B%
)
However, this gives me:
4
4
4
4
4
How can I achieve the desired output while using both A and B in my code?
ECHO OFF
setlocal
FOR /L %%A in (1,1,5) DO (
SET /a "B=%%A-1"
call ECHO %%B%%
)
Since you are not using setlocal, B will be set to the value from the previous run. %B% will be replaced by 4 since B was set to 4 by the previous run. the call echo trick uses a parsing quirk to retrieve the current (run-time) value of the variable.
Here's "the official" way:
ECHO OFF
setlocal enabledelayedexpansion
FOR /L %%A in (1,1,5) DO (
SET /a "B=%%A-1"
ECHO !B!
)
In delayedexpansion mode, !var! retrieves the value of var as it changes at run-time. This is not without its drawbacks, but you'd need to read up on delayedexpansion for a guide on that matter.

Manipulating strings in arrays in a batch file?

I currently have a batch file that is going through a text file and assigning each line into an array. I want to iterate through the loop and remove a certain number of characters from every value in the array. Is this possible to do?
#ECHO off
findstr /C:"number" /C:"type" testFile.txt > oneresult.txt
set "file=oneresult.txt"
set /A i=0
timeout /t 1
echo ---------------Results--------------- > results.txt
for /f "tokens=*" %%x in (oneresult.txt) do (
call echo %%x >> results.txt
call set array[%i%]=%%x
set /A i+=1
)
call echo %i% files received >> results.txt
del "oneresult.txt"
So right now it just prints the retrieved strings from testFile.txt and then they are eventually placed into result.txt. I would like all the strings that come from the testFile.txt to have the first 10 characters removed. If there is an easier way please let me know. So far this is what I have found but I am also a bit of a batch noob.
Just figured it out without the array and posting the answer for anyone else that may be searching in the future:
#ECHO off
findstr /C:"number" /C:"type" testFile.txt > oneresult.txt
set /A i=0
timeout /t 1
echo ---------------Results--------------- > results.txt
for /f "tokens=*" %%x in (oneresult.txt) do (
setlocal enabledelayedexpansion
call set print=%%x
call set newprint=!print:~32!
call echo !newprint! >>results.txt
endlocal
set /A i+=1
)
call echo %i% files received >> results.txt
del "oneresult.txt"
You use several call's in your code without having understood that these pseudo calls are usually used for a different type of delayed expansion not requiring setlocal enabledelayedexpansion, but to double the percent signs.
the intermediate file oneresult isn't necessary, one for /f to parse the output of the findstr is sufficient.
One set of parentheses enclosing all output lines can redirect to results.txt
#ECHO off
set /A i=0
(
echo ---------------Results---------------
for /f "tokens=*" %%x in (
'findstr /C:"number" /C:"type" testFile.txt'
) do (
set print=%%x
call echo:%%print:~32%%
set /A i+=1
)
call echo %%i%% files received
) > results.txt
The following code with setlocal enabledelayedexpansion is functional identical
#ECHO off&Setlocal EnabledelayedExpansion
set /A i=0
(
echo ---------------Results---------------
for /f "tokens=*" %%x in (
'findstr /C:"number" /C:"type" testFile.txt'
) do (
set print=%%x
echo:!print:~32!
set /A i+=1
)
echo !i! files received
) > results.txt

Resources