Comparisons returning incorrect value in batch - batch-file

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)

Related

Formatting generated numbers in a .bat file

Forgive me if there is a simple answer to this, I'm new to all of this.
The below .bat script generates a list of numbers depending on how many numbers you want.
However what I would like is to format the numbers it generate.
For example, if I input 20, instead of it coming out 1, 2, 3 etc. I would like it to come out as 001, 002... 020.
Is this possible? Am I missing something obvious?
Many Thanks.
#echo off
setlocal EnableExtensions
:start
cls
set /p loopcount= How Many Users?:
set "x="0"
:loop
set /a "x+=1"
echo %x%
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
pause
goto start
just implementing SomethingDark's suggestion (and a minor change in the logic of the loop):
set /p "loopcount= How Many Users?: "
set "x=1000"
:loop
set /a x+=1
echo %x:~-3%
set /a loopcount-=1
if %loopcount% gtr 0 goto :loop
echo loop finished.
(btw: your set "x="0" has a quote too much (probably a typo)
Here's a quick example batch file which uses powershell for your leading zeroes.
I have used a for loop as the looping mechanism.
#Echo Off
SetLocal EnableExtensions
:AskNum
Set "num=1"
Set /P "num=how many users?"
Set num | findstr.exe /RX "^num=[123456789][0123456789]*$" 1>NUL || GoTo AskNum
For /F %%G In ('powershell.exe "1..%num% | %% ToString User000"') Do Echo %%G
Pause
This script will not continue unless the end user inputs an integer, without a leading 0, and which is a minimum value of 1. Here you can modify the Echo command in Do to an alternative, for example net.exe User %%G /Add, or if you wish, to a parenthesized sequence of commands. In each case %%G will contain the returned string with the incremented three digits.This version prepends each three digit number sequence with an additional string, (User), but that can simply be deleted or replaced if/as required.
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET /p "lastnum= How many numbers? "
SET /a lastnum+=1000
FOR /L %%e IN (1001,1,%lastnum%) DO SET /a num=%%e&ECHO !num:~-3!
GOTO :EOF
No need for complication

Split string into array, convert last number to int and subtract, replace original string with new int in Batch file

I have a batch file that takes an argument that looks like this: 7.0.5 or maybe 10.34.7.2
I want to take the last digit of the string, subtract 1 from it, then re-save the original string replacing the last number with the new one. Here is what I have so far:
#echo off
setlocal enabledelayedexpansion
set tag=%1
echo %tag%
for %%a in ("%tag:.=" "%") do set "output=%%~a"
echo last number: %output%
set /a count=0
for /f "tokens=1-3 delims=." %%a in ("%tag%") do (
set /a count+=1
set "numbers[!count!]=%%a"
echo numbers[a]: %%a
)
for /l %%a in (1,1,3) do echo %numbers[%%a]%
set /a lastNum=%output%
echo lastNum: %lastNum%
set /a prevNum=lastNum-1
echo prevNum: %prevNum%
This doesn't work, obviously. The second for loop will only print the first digit and when I get to the third for loop, it only prints ECHO is off. And I haven't even gotten to replacing the string. But if I can get the array populated, then it should be simple.
Treat it like a file name and it becomes trivial. Your last token would then be the extension, available as %%~xa, the part before would be the "file name", available as %%~na:
#echo off
setlocal
set tag=%~1
echo input: %tag%
set "last=%~x1" &REM last token ("extension")
set /a last=%last:~1%-1 &REM remove the dot and subtract one
set "prevnum=%~n1.%last%" &REM reassemble "filename"."newExtension"
echo lastNum: %~1 &REM original parameter
echo prevNum: %prevNum% &REM new calcualted value
This works:
#echo off
setlocal EnableDelayedExpansion
set "tag=%1"
echo %tag%
set "last=%tag:.=" & set "out=!out!!last!." & set /A "last=%-1" & set "out=!out!!last!"
echo %out%
If you want to comprehend where the magic is, remove the #echo off line and carefully review the executed code...

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
)

"for" loop not working as per need

I have following code:
set b=Hello
set c=1
set d=5
for /l %%x in (1,1,%d%) do (set /a c=c+1 & set "a=%a%%b%%c%")
echo %a%
And for this, I wanted an output as:
Hello1Hello2Hello3Hello4Hello5
But instead, I get result as:
Hello1Hello1Hello1Hello1Hello1
Can anyone give me any idea on how can I do so?
You need:
setlocal enableextensions enabledelayedexpansion
set b=Hello
set c=0
set d=5
for /l %%x in (1,1,%d%) do (set /a c=c+1 & set "a=!a!!b!!c!")
echo %a%
endlocal
The ! variant of % will expand the variables at the time the code is executed whereas % expands when the code is parsed. And, since parsing happens on the entire statement (from for to the closing parenthesis), you'll get the original value only.
Note that you don't need delayed expansion for the invariant b but, once you've decided you need them, you may as well use them everywhere - they act more as you'd expect in most cases.
You'll notice I've also changed the initial value of c to get the output you stated that you wanted. As it was, you would get 2,3,4,5,6 rather than 1,2,3,4,5.
I think this is what you want:
#Echo Off
Set "a="
Set "b=Hello"
Set "c=1"
Set "d=5"
For /L %%A In (%c%,1,%d%) Do Call Set "a=%%a%%%b%%%A"
If Defined a Echo %a%
Pause
Or alternatively:
#Echo Off
Set "a="
Set "b=Hello"
Set "c=1"
Set "d=5"
For /L %%A In (1,%c%,%d%) Do Call Set "a=%%a%%%b%%%A"
If Defined a Echo %a%
Pause

batch programming

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.

Resources