String Left & Mid Manipulation in a For Loop Batch File - batch-file

I am writing a batch file with a for loop and am having trouble getting the left and mid logic working. I have successfully got this logic working outside of the for loop and have attached the code here.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ZKR
set /a value = 1000
set /a range = "%value%"
set /a secrange = "%value%"
for /l %%x in (1, 1, 10) do (
echo %range%
set range=%range:~0,1%
set secrange=%secrange:~1,3%
echo !range!
echo !secrange!
set "newstr=!range!.!secrange!"
echo !newstr!
PAUSE
)
The output is shown here.
Split 1000
So the above cmd bat is able to split the string and then combine it with a period mark in the middle of the two split strings.
However, in my for loop I don't achieve this result. I know it may be a syntax issue but I am not sure how to fix it (I have tried a lot of different things already).
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ZKR
set /a range = "X"
set /a secrange = "X"
for /l %%x in (1, 1, 10000) do (
if %%x geq 1000 (
if %%x lss 10000 (
set "range=%%x"
set "secrange=%%x"
echo !range!
echo !secrange!
set "range=%range%:~0,1%"
echo !range!
echo %range%
set secrange=%secrange:~1,3%
echo !secrange!
echo %secrange%
set "newstr=!range!.!secrange!"
echo !newstr!
PAUSE
)
REM End x greater 1000
)
REM End for loop
)
I use secrange the same way as I did in the original code without the for loop. I tried to manipulate the range portion with many different syntax combos but have not been able to output the left character 1. I only am able to output 0 for both characters.
I attached the output here. Bad Output
I understand there is an error (Echo is Off). Please ignore that it is just based off my echo output type for my personal testing.
The important two outputs are the third and fourth line (whichever one is referring to the correct syntax). Does anyone know how to fix the syntax in order to get the left character of 1000 in the for loop and the remaining 3 zeros of the 1000?

Related

Sum in batch script

How can I make the sum of the imputed numbers given by the user from the keyboard ? I tried a for loop but is endless. I cannot figure out a break statement.
::#ECHO OFF
setlocal EnableDelayedExpansion
set /p X=
SET /A suma=0
:Loop
FOR %%G IN (%X%) DO (
IF %%G GTR 0 (
SET /A suma=%suma%+%%G
)
)
SHIFT
GOTO Loop
:completed
echo %suma%
What i want to do is to make the program more user friendyl like echo somthing for the user to know it is supposed to write somthing. I know the correct code for this question, but the numbers are given as parameters in the command (syntax: script.bat parameter1 parameter2 parameter3).
::#ECHO OFF
SET /A suma=0
:Loop
IF "%1"=="" GOTO completed
FOR %%F IN (%1) DO (
IF %1 GTR 0 (
SET /A suma=%suma%+%1
)
)
SHIFT
GOTO Loop
:completed
echo %suma%
Please give me any idea for the breaking statement in the first example or how can I modify the code to take the numbers from the users imput variable and not as paramaters.
Thank you.
SET /A suma=0
set "X=%*"
if not defined X set /p "X=No parameters detected "
if not defined X ECHO No user-input detected&goto completed
FOR %%G IN (%X%) DO (
IF %%G GTR 0 (
SET /A suma=suma+%%G
)
)
:completed
echo %suma%
%* means the command-line tail. If no tail, then ask user for input. If still no data, issue message and show result.
shift moves the command line tail one position, so %2 becomes %1, etc. You haven't provided a test to exit the routine - and the command-line tail is presumably empty, so you are shifting it (which does nothing) and looping.
The for...%%G.. processes the entire list %X%, so there's no point in the shift/loop - the data has already been processed.

If else issue with arrays in batch

I have created two sorted arrays of size n and m with numeric value in a batch file.
FOR /L %%a IN (0,1,!n!) DO ECHO !vector[%%a]!
FOR /L %%a IN (0,1,!m!) DO ECHO !vector2[%%a]!
This exactly shows the contents of my arrays.
Now I want to write a logic which will print merged sorted array.
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set i=0
set j=0
set /A totalElements =!n!+!m!
FOR /L %%A IN (1,1,!totalElements!) DO (
if !vector[!i!]! LSS !vector2[!j!]! (
echo "First list"
echo !%vector[!i!]%!
) else (
echo "Second List"
echo !%vector[!i!]%!
)
)
So, this if else logic is not working. Any idea where in the syntax I have gone wrong? I guess I am not extracting the value from the array correctly?
Incomplete analyse:
where are defined n and m variables in set /A totalElements =!n!+!m! ?
in !vector[!i!]!, batch parser is looking for variables !vector[! and !]! instead of nesting like in !vector[%i%]!;
!%vector[!i!]%! is totally unclear for me.
Your question seems to be too broad (a bit unclear aim) to give more positive and constructive notes.

Problems with for loops in batch file

I am trying to create a for loop in batch with some calculation using modulus from 1-100 then return an average for the entire set of numbers. I want to print this average on the screen and to a file called output This is what i have but it doesn't seem to be working correctly. Any suggestions? Thank you kindly.
#echo off
setlocal EnabledDelayedExpansion
for /l %%i in (1,1,100) do (
set /a EXPR = %%i %% 5
set /a EXPR2 = %EXPR+3
set /a TOTAL = TOTAL+%EXPR2
)
set /a AVG = %TOTAL/100
echo Your average is %AVG
echo Your average is %AVG >> output.txt
#echo off
setlocal EnableDelayedExpansion
for /l %%i in (1,1,100) do (
set /a EXPR=%%i %% 5
set /a EXPR2=EXPR+3
set /a TOTAL=TOTAL+EXPR2
)
set /a AVG=TOTAL/100
echo Your average is %AVG%
echo Your average is %AVG%
Is this what you want?
One additional d in EnableDelayedExpansion (btw. set /a works also without delayed expansion). Classical error is to let spaces surrounding variable assignment.In batch scripts result is that the space becomes part of the variable name.Except FOR tokens and command line arguments variables are two side enclosed with %

Echo Outputs When Writing to File

When I use this code to select a character from my list, it works just fine but when I write it to a file using:
echo %pwd%>>pwd.gen
It will some times put the word "ECHO" randomly in the middle of the strings generated. Here is an example:
jUrkunjcxC
ecRECHOsI5w0T
DmJfat13fT
UWXOysW7Gb
pPmS7138Ve
nFkh32ECHOJd1
You can see it appears in line 2 and 6. This only happens about 20% of the time.
Here is the code:
#echo off
title Password Generator
color f0
:firstRun
set /a cnt=0
cls
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set /p Len=What length should the password be?
set /a Len=%Len%-1
cls
set /p Amt=How many would you like to generate?
cls
goto start
:start
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set pwd=
FOR /L %%b IN (0, 1, %Len%) DO (
SET /A rnd_num=!RANDOM! * 62 / 32768 + 1
for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set pwd=!pwd!%%c
)
echo %pwd%>> pwd.gen
set /a cnt=%cnt%+1
if %cnt%==%Amt% goto end
goto start
:end
cls
echo Done!
echo Results have been saved to "pwd.gen"
echo.
choice /c YN /m "Restart?"
if %errorlevel%==1 goto firstRun
:start
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set pwd=
FOR /L %%b IN (0, 1, %Len%) DO (
SET /A rnd_num=!RANDOM! * 62 / 32768 + 1
for /F %%c in ('echo %%alfanum:~!rnd_num!^,1%%') do set pwd=!pwd!%%c
)
alfanum is 26+26+10 = 62 characters long.
RANDOM gives a random number from 0-32,767
When RANDOM is above 32240, rnd_num gets set to 62
string indexing starts at 0 not 1
the for /F %%c command indexes alfanum:~62,1~ which is an empty string
it calls echo with no parameter, which prints ECHO is on. instead of returning a single character
for /F defaults to splitting strings with a space delimiter, which separates out the first word
%%c becomes ECHO
you add ECHO into the password.
This is a combination of a couple of things. While I'm not totally clear about the inner workings of the whole thing, I know what's causing it and how to fix it.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 is 62 characters long. However, substrings in batch start with 0, so valid numbers go from 0 to 61. For whatever reason, an index-out-of-range combined with the ^ in 'echo %%alfanum:~!rnd_num!^,1%%' is causing the word ECHO to be displayed.
To get around this, simply don't add 1 when calculating rnd_num.
SET /A rnd_num=!RANDOM! * 62 / 32768

If Statement with numbers goes wrong

Ok there, I'm trying to make a batch that have to read some .txt files and to create some .icss that I should load on Google Calendar.
Year and month are set manually, and it have to go from 1 to 30 for the days.
What the batch do is this:
-Set the day (d);
-Update date (data), input name file (in) and output name file (out);
-Read all the lines and create an event for all the day long with the lines as name of the events.
And this work should have done from 20141101.txt to 20141130.txt, creating files from 20141101.ics to 20141130.ics.
I post the code and the problem just below.
Code:
#echo off
setLocal enableDelayedExpansion
set "y=2014"
set "m=11"
set "d=00"
set /A "prova=10"
set "data=!y!!m!!d!"
set "in=!data!.txt"
set "out=!data!.ics"
for /L %%i in (0,1,11) do (
if "%%i" LSS "!prova!" (
set "d=0%%i"
) else (
set "d=%%i"
)
echo !d!
set "data=!y!!m!!d!"
set "in=!data!.txt"
set "out=!data!.ics"
echo BEGIN:VCALENDAR>>!out!
for /F "tokens=*" %%l in (!in!) do (
echo BEGIN:VEVENT>>!out!
echo DTSTART;VALUE=DATE:!data!>>!out!
echo SUMMARY:%%l>>!out!
echo END:VEVENT>>!out!
)
echo END:VCALENDAR>>!out!
)
pause
goTo :exit
:exit
exit
Problem:
The if statement that have to control if the day is less than 10 works only for d equal to 0 and 1 and I just don't understand why.
echo !d! prints 00, 01, 3, 4, ..., 9, 10, 11 and only for d=0 and d=1 the if statement is respected...
I tried to set d with and without /A in the set command, but never changed...
What am I doing wrong?
Any tip?
I solved the problem.
The right statement should be written like this:
if %%i LSS !prova! (
set "d=0%%i"
) else (
set "d=%%i"
)
[The problem were the quotation marks that forced the statement to use the vars as they were strings]
For everybody who wants to use the same code and editing it I leave it here.

Resources