batch programming - batch-file

I am new in batch. Trying for some days to make something in batch but have a problem I cannot solve. I read a lot of your comments but did not find answer. Maybe you can help me?
The point is:
I input string from keyboard( e.g. 10 characters ). name of it is"allinputstring"
Calculate of length is ok ( by redirect in txt file and expand its bytes ). name "length"
Parse string in 10 pieces (strings) is ok.
So here is a problem, I want to echo these pieces, so I use next code, I use a counter to find out is the counter give me good count as output variable, and echo it to see on screen if it is good. Counter seems good, end echo of pieces strings is good enough. But I want to put in line 5. Variable count instead of "%%m", and cannot find a syntax way how to do it.
setlocal enabledelayedexpansion
for /l %%m in (1,1,!lenght!) do (
set /a count=0
set /a count=count+%%m
echo !count!!allinputstring:~%%m,1!
)
endlocal
please help me.

Try this:
#echo off &setlocal enabledelayedexpansion
set /a lenght=9
set "allinputstring=ABCDEFGHIJ"
for /l %%m in (0,1,%lenght%) do (
set /a count=0
set /a count+=%%m
echo !count! !allinputstring:~%%m,1!
)
endlocal
Output is:
0 A
1 B
2 C
3 D
4 E
5 F
6 G
7 H
8 I
9 J

#ECHO off
setlocal ENABLEDELAYEDEXPANSION
SET allinputstring=abcdefghijk
SET lenght=10
for /l %%m in (1,1,!lenght!) do (
set /a count=0
set /a count=count+%%m
FOR %%z IN (!count!) DO echo !count! !allinputstring:~%%z,1!
)
GOTO :eof
Does this do what you require?
So... to make COUNT show (I've assigned it to KOWNT, but the syntax endlocal&set count=%count% would assign it to COUNT instead)
I've changed the starting value of the FOR/L because character counting starts from character#0 in the string.
#ECHO off
setlocal ENABLEDELAYEDEXPANSION
SET allinputstring=abcdefghij
SET lenght=9
for /l %%m in (0,1,!lenght!) do (
set /a count=0
set /a count=count+%%m
FOR %%z IN (!count!) DO echo !count! !allinputstring:~%%z,1!
)
endlocal&SET KOWNT=%count%
ECHO Now KOWNT=%KOWNT% but count=%count% because we have exited the SETLOCAL
GOTO :eof
When the ENDLOCAL is encountered, the parser substitutes the CURRENT value of the variables in the line and THEN executes the line.
Hence, the line is executed as
endlocal&set KOWNT=9
since the value of count at the time is 9.
When the SETLOCAL is executed, all changes to the environment since the matching SETLOCAL are thrown away. The environment variables are restored to their state when the SETLOCAL was executed and count becomes empty again (as it was before the routine.) THEN the SET instruction is executed, which sets KOWNT to 9.

Related

can I use a variable to extract a substring in batch?

I need to extract the characters of a string one by one in a loop. Ideally, I would've done something like this, but as you might have guessed, it doesn't work.
#echo off
setlocal EnableDelayedExpansion
set /a len=5
set var=abcde
for /l %%n in (1,1,%len%) do (
set /a num=%%n - 1
echo %var:~!num!,1%
)
it works seamlessly if I replace !num! with a plain number, but with the variable, behaves as if the percent signs aren't there and echoes:
var:~0,1
var:~1,1
var:~2,1
var:~3,1
var:~4,1
To directly fix your issue replace:
echo %var:~!num!,1%
with:
call echo %%var:~!num!,1%%`
But you can do it without set /a num=%%n - 1 because you are already counting using for /L but note we are counting from 0.
Also note, we start couting from 0.
#echo off
setlocal EnableDelayedExpansion
set /a len=4
set "var=abcde"
for /l %%n in (0,1,%len%) do (
echo(!var:~%%n,1!
)

Batch file - loop through first n lines of text file

I'm trying to determine the format of a text file by looping through the first 10 lines, perform some regex matching and then compare the results at the end. I can easily loop through the entire file, but I only want the first N lines (in this case 10)
I'm familiar with other languages, but the idiosyncrasies of this batch file is throwing me for a loop so to say.
Here is what I have so far:
#echo off
setlocal enableDelayedExpansion
set /A REGEXCOUNTER=0
set /A COUNTER=0
for /F %A in (%submitfile%) do (
set /A COUNTER=COUNTER+1
rem echo %A
setlocal enableDelayedExpansion
echo(%A|findstr /r /c:"[0-9].*" >nul && (
set /A REGEXCOUNTER=REGEXCOUNTER+1
echo %COUNTER% - %REGEXCOUNTER% - FOUND - %A
rem any commands can go here
) || (
echo NOT FOUND
rem any commands can go here
)
rem LOOP END
if %COUNTER% GEQ 10 do (goto loop_over)
)
)
:loop_over
echo "END HERE!"
I've got counters set up that incrementally tick up to count my matches and how many times it's looped. However here is some sample output of variable values:
110 - 0 - FOUND - 003
220 - 0 - FOUND - 2
330 - 0 - FOUND - 1
440 - 0 - FOUND - 029
The loop counter variable is increasing by ten for each loop and the regex match counter is not going up at all. I'm pretty sure this has something to do with variable scope but I'm not sure where to begin.
This should fix all the issues I talked about in my comments above.
#echo off
setlocal enableDelayedExpansion
set /A REGEXCOUNTER=0
set /A COUNTER=0
for /F %%A in (%submitfile%) do (
set /A COUNTER=COUNTER+1
rem echo %%A
echo(%%A|findstr /r /c:"[0-9].*" >nul && (
set /A REGEXCOUNTER=REGEXCOUNTER+1
echo !COUNTER! - !REGEXCOUNTER! - FOUND - %%A
rem any commands can go here
) || (
echo NOT FOUND
rem any commands can go here
)
rem LOOP END
if !COUNTER! GEQ 10 goto loop_over
)
)
:loop_over
echo "END HERE"
The extra setlocal within the loop should be removed. Once setlocal enabledelayedexpansion has been executed, it remains in effect until a setlocal disabledelayedexpansion is executed or until the batch terminates.
Each %A (the loop-control metavariable must be %%A (one % if run from the command-line, two if within a batch file)
If you change the value of a variable within the loop, then you need to refer to the changed value as !varname!, not %varname% which is the original value (search SO for delayed expansion). set /a always works on the current variable value.

Increment a variable on each cycle in a batch file?

I have a batch script which loops and I want to count how many cycles it has done.
This is how the script looks:
#echo off
title MyTitle
set cycles=0
:startpoint
echo %cycles%
(my command)
goto startpoint
I would like to be able to see the variable "cycles" increment by 1 each time it goes back to :startpoint, how do i do that?
To perform arithmetic operations in batch you need to use the /a switch with the set command:
#echo off
title MyTitle
set cycles=0
:startpoint
set /a cycles=cycles+1
echo %cycles%
...
(my command)
...
goto startpoint
Type set /? in cmd for more information.
Using this code
setlocal enableextensions enabledelayedexpansion
set /a count = 1
for /f "tokens=*" %%a in (config.properties) do (
set /a count += 1
echo !count!
)
endlocal
works for me, the reason because I was using %count% instead of !count! so I keep getting 1 instead of the expected output. So if using %% doesn't work for you, you can as well use !! to either display your output or do your calculations or comparisons.
well this worked for me.
#echo off
setlocal enabledelayedexpansion
SET /A i = 1
for /f "tokens=*" %%f in (temp.txt) do (
IF !i!==2 echo %%f
SET /a i+=1
)

How to make a variable plus a random number?

Here's the little bit of code I have:
if %a%==1 set /a count=%count%+%random% %%100
What it does (at the moment) is simply stay as %count% and not increase by a random number between 1 and 100.
What I want it to do is this:
If a is equal to 1 (which it always is in an earlier line of code) it will increase count by a random number between 0 and 99. If anyone could help, that would be great! (It's my first time on stackoverflow and I'm new at programming so make it simple!)
I'm not sure what your problem is? Have you posted your full script? The following script works for me.
#echo off
setlocal
set a=1
set count=0
echo count: %count%
if %a% == 1 set /a count=%count%+%random% %%100
echo count: %count%
As MC ND points out you may have problems if you're using count inside another block (e.g., inside a for-loop). The following does not work.
#echo off
setlocal
set a=1
set count=0
for /L %%x in (1,1,1) do (
if %a% == 1 set /a count=%count%+%random% %%100
rem This always prints 0!
echo count: %count%
)
To get it working use setlocal enableDelayedExpansion. The following is again working.
#echo off
setlocal enableDelayedExpansion
set a=1
set count=0
for /L %%x in (1,1,1) do (
if !a! == 1 set /a count=!count!+%random% %%100
rem Now this works!
echo count: !count!
)

Comparisons returning incorrect value in batch

I have the code:
#echo off
set /p dec="Path? "
set patha="C:\Users\%username%\%dec%"
set /a i=2
setlocal EnableDelayedExpansion
:import
if "%i%"=="12" goto loopend
if "!patha:~-%i%,1!"=="." set ext="!patha:~-%i%!"
set /a i=%i%+1
goto import
:loopend
echo %ext%
pause
It loops through the code 10 times, but when I have it echo "!patha:~-%i%,1!" and it echoes "." it doesn't set the ext variable. Am I doing comparisons wrong?
Works happily or me in W7, although
FOR /f %%i IN ("%patha%") DO SET ext="%%~xi"
echo %ext%
would seem to do the same thing.
That is, assuming you want to extract the last n characters starting "."... (that's not clear)

Resources