Batch File Counter in a for loop - batch-file

I have a for loop that I'm trying to count each loop however this loop echo's zeros. How do I get it to increment?
#echo off
setlocal enableextensions
set /a count = 0
for /f "Delims=" %%a in (content\docs.html) do (
set /a count+=1
echo %count%
)

#echo off
setlocal enabledelayedexpansion
set /a count = 0
for /f "Delims=" %%a in (content\docs.html) do (
set /a count+=1
echo !count!
)
notice the ! instead of %. that's the consequence of using setlocal enabledelayedexpansion.

Related

Setting random variable value in for loop in batch file gives errors

Here is my code:
for %%i in ("joined/*.mp4") do (
set /a result=(%random%*2/32768)+1
echo %result%
)
It gives me errors about +1 was unexpected at this time.
I tried another variant:
for %%i in ("joined/*.mp4") do (
set /a result=(%random%*2/32768)
echo %result%
)
It gives me an error about unbalanced parenthesis.
How can I echo the random variable correctly?
Thanks. :)
Trying the following code gives me the same value of random every time. How can I change it with each itertion of the loop?
setlocal EnableDelayedExpansion
for %%i in ("joined/*.mp4") do (
set /a result= %random%*20/32768 + 1
echo !result!
)
Is there a resource that I can read to learn in detail how batch files work and their language like loops, arrays etc.? I tried searching on Google but nothing useful came up.
Using brackets, (parentheses), is perfectly fine, and I would say more correct, so there's no reason why you cannot continue to do so.
Hare some options for you, which maintain their use:
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
Set /A result = (!random! * 20 / 32768^) + 1
Echo !result!
)
Pause
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
Set /A "result = (!random! * 20 / 32768) + 1"
Echo !result!
)
Pause
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
Set /A result = (!random! %% 20^) + 1
Echo !result!
)
Pause
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
For %%G In ("joined\*.mp4") Do (
Set /A "result = (!random! %% 20) + 1"
Echo !result!
)
Pause
As explained in the link provided by #Stephan, you'll need the ! on random too.
You can do it like this (for some reason, (!random!*20)/32768 triggers "/32768" was unexepected, so you have to split it on two lines :
setlocal EnableDelayedExpansion
for %%i in ("joined/*.mp4") do (
set /a result = !random!*20
set /a result= !result!/32768 +1
echo !result!
)

print exact all characters from lines from one file to one file in batch file

Here i am printing line no 1 to 5 from a file to another file.its working fine but one small issue that the lines are trimming from left side,i do not want to trim,it should be same as the input file.
infile.txt:
<RCO-XXX-AGENT>
<CREATED>2018-06-28 10:19:09</CREATED>
<FORMAT>
<VARIABLE>
<EOR>/010</EOR>
<EOC>/009</EOC>
<CTR>5</CTR>
code:
echo off
setlocal enabledelayedexpansion
call :Print_Lines > outfile.txt
endlocal
:Print_Lines
setlocal enabledelayedexpansion
set cur=0
for /f "delims==" %%i in (infile.txt) do (
set /a cur=cur+1
if !cur! geq 1 (
if !cur! leq 5 (
for /f "tokens=*" %%j in ( "%%i") do (
echo %%j
)
)
)
)
endlocal
exit /b 0
goto :eof
outfile.txt:
<RCO-XXX-AGENT>
<CREATED>2018-06-28 10:19:09</CREATED>
<FORMAT>
<VARIABLE>
<EOR>/010</EOR>
The second loop is useless
It should work :
echo off
setlocal enabledelayedexpansion
call :Print_Lines > outfile.txt
endlocal
:Print_Lines
setlocal enabledelayedexpansion
set cur=0
for /f "delims==" %%i in (infile.txt) do (
set /a cur=cur+1
if !cur! geq 1 (
if !cur! leq 5 (
echo %%i
)
)
)
endlocal
exit /b 0
goto :eof
In your exemple, the spaces at left are not printed because they are considered as delimiter character
I would suggest this method:
#Echo Off
Set "Num=5"
<"infile.txt" (For /L %%A In (1,1,%Num%) Do (Set "_="
Set /P "_="
SetLocal EnableDelayedExpansion
Echo=!_!
EndLocal))>"outfile.txt"
Where you can adjust the value to Num as necessary.

Variable name stored in delayed variable - Batch

I have:
setlocal EnableDelayedExpansion
var inLoopVar=hey
var first=!inLoopVar!
echo %!first!%
This outputs ECHO OFF because %!first!% is returned as empty.
How can I print "Hey"
Updated and Clarified:
Here is my full code with comment of what i am trying to do
#ECHO OFF
setlocal EnableDelayedExpansion
set RESULT=TRUE
set INPUT[0]=+NAME=RESULT; VARB=Second; VARC=Second; VARD=Second; VARE=Second; VARF=Second; VARG=Second;
set length=1
set i=0
:loop
if %i% equ %length% goto :eof
for /f "usebackq delims=+ tokens=2" %%j in (`set INPUT[%i%]`) do (
set y=%%j
FOR /f "tokens=1-7 delims=; " %%a IN ("!y!") DO (
set aaa=%%a
set testVar=!aaa:~5!
REM basically testVar resolves to RESULT
echo !!testVar!!
REM Above echo prints "RESULT"
echo %!!testVar!!%
REM Above echo prints "ECHO is off."
)
)
set /a i=%i%+1
goto loop
Instead of ECHO is off. i am trying to output TRUE
#ECHO OFF
setlocal EnableDelayedExpansion
set RESULT=TRUE
set INPUT[0]=+NAME=RESULT; VARB=Second; VARC=Second; VARD=Second; VARE=Second; VARF=Second; VARG=Second;
set length=1
set i=0
:loop
if %i% equ %length% goto :eof
for /f "usebackq delims=+ tokens=2" %%j in (`set INPUT[%i%]`) do (
CALL :sub %%j
)
set /a i=%i%+1
goto loop
GOTO :EOF
:sub
SET "namepart="
FOR %%a IN (%*) DO IF DEFINED namepart (
SET "Valuepart=!%%a!"
ECHO !namepart! is !valuepart!
SET "namepart="
) ELSE (
SET "namepart=%%a"
)
GOTO :eof
It would help if you would explain what you intend to do rather than set up an inquest to determine what you want to do by examining how you have failed to do whatever it is.

Splitting txt using batch containing "=" on each line

I'm splitting the file every 6000 lines with the script below but I'm having a problem with it....
Every line of my txt file has the character '=' so when the batch is treating the file, where I have the '=' the line stops and goes to the next one (I don't have the end of each line, anything that follows the = is deleted).
FOR %%X IN (*.TXT) do (
setlocal ENABLEDELAYEDEXPANSION
SET BFN=%%X
SET LPF=6000
SET SFN=%%X_6000_
)
REM ==============================
SET SFX=%BFN:~-3%
SET /A LineNum=0
SET /A FileNum=1
For /F "delims==" %%l in (%BFN%) Do (
SET /A LineNum+=1
echo %%l >> %SFN%!FileNum!.%SFX%
if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)
)
endlocal
Can Someone help me out?
Sample of file:
A88A0A1891BAA9=B088A00000003800001==00000038000
A88A0A1B13A0BB=B088A00000089000002==00000890000
A88A0A1A13B830=B088A000000B3800003==00000B38000
A88A0A00331831=B088A00000010A00004==0000010A000
A88A0A10B31B39=B088A00000090A00005==0000090A000
A88A0A19A3AA89=B088A00000089800006==00000898000
A88A0A19AA0318=B088A000000A0100007==00000A01000
A88A0A08911913=B088A00000008800008==00000088000
A88A0A1089A139=B088A00000098300009==00000983000
A88A0A1BB8BBA8=B088A000000AA100010==00000AA1000
A88A0A0A8B9199=B088A00000098100011==00000981000
A88A0A19AA8A9B=B088A00000088900012==00000889000
A88A0A0B380A13=B088A00000099A00013==0000099A000
A88A0A13899A18=B088A00000088A00014==0000088A000
A88A0A1A188910=B088A0000008A800015==000008A8000
A88A0A10930AA0=B088A0000009B900016==000009B9000
A88A0A09338A88=B088A000000A0A00017==00000A0A000
A88A0A11A98930=B088A000000AAB00018==00000AAB000
Setting the LPF (lines Per File) to 6, here is the result (first of 3 files):
A88A0A1891BAA9
A88A0A1B13A0BB
A88A0A1A13B830
A88A0A00331831
A88A0A10B31B39
A88A0A19A3AA89
Thanks!
Remove the second = in the delims :
For /F "delims=" %%l in (%BFN%) Do (
delims== means the command tries to split each line with =
What you need is
For /F "tokens=*" %%l in (%BFN%) Do
This means "give me the whole line without splitting it into tokens"
Also in your code above the nesting of the do ( ) blocks seems wrong.
It looks like the first for loop simply sets environment variables and does nothing else. Did you actually run this code?
It should be
#echo off
FOR %%X IN (*.TXT) do (
setlocal ENABLEDELAYEDEXPANSION
SET BFN=%%X
SET LPF=6000
SET SFN=%%X_6000_
REM ==============================
SET SFX=%BFN:~-3%
SET /A LineNum=0
SET /A FileNum=1
For /F "tokens=*" %%l in (%BFN%) Do
SET /A LineNum+=1
echo %%l >> %SFN%!FileNum!.%SFX%
if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)
)
endlocal
)

( was unexpected at this time in a FOR loop

So Im having this problem where my batch for loop doesnt work, I havent found any solutions for this. It keeps saying "( was unexpected at this time". Could anyone help please?
for /F %%G IN ('TYPE info.txt') DO (
set /a cnt+=1
set /a div=%cnt% %% 2
if %div% == 0 {
set ord=%%G
echo %ord%
}
)
This only works if you use delayed expansion, otherwise all your variables will only be evaluated once.
Setlocal EnableDelayedExpansion
set cnt=0
for /F %%G IN ('TYPE info.txt') DO (
set /a cnt+=1
set /a div=!cnt! %% 2
if !div!==0 (
set ord=%%G
echo !ord!
)
)
Or you could use labels instead.
for /F %%G IN ('TYPE info.txt') DO call :line %%G
goto :EOF
:line
set /a cnt+=1
set /a div=%cnt% %% 2
if "%div%"=="0" call :div0 %1
goto :EOF
:div0
set ord=%1
echo %ord%
goto :EOF

Resources