If Statement with numbers goes wrong - batch-file

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.

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.

String Left & Mid Manipulation in a For Loop 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?

What is the reason for command not recognized error message on executing my batch file?

I have written this batch code:
#echo on
set variable=z
set t=%time:~0,2%
IF %t% LSS 12 (
%z%=_1
) ELSE (
%z%=_2
)
And when run the batch file from command window, I get this error:
IF 14 LSS 12 (_1) ELSE (_2)
'_2' is not recognized as an internal or external command, operable program or batch file.
Where is the mistake in my code resulting in this error message?
I'm not sure what you want to achieve. I guess you want to check the condition %t%<12 and set your variable z to _1 if this is the case and to _2 otherwise.
Your error is that %z%=_1 is not a valide statement. To assign a value to a variable you always have to use SET:
SET z=_1 will result in %z% holding the value _1. So your code can be fixed like this:
#echo on
set variable=z
set t=%time:~0,2%
IF %t% LSS 12 (
SET z=_1
) ELSE (
SET z=_2
)
Based on the rest of your code you also might need to add the line SETLOCAL EnableDelayedExpansion at the beginning of your code and access your variable z as !z! instead of %z% below your IF-construct.
EDIT:
To fit your desctiprion from your comment you should do this:
#echo on
set t=%time:~0,2%
IF %t% LSS 12 (
SET z=_1
) ELSE (
SET z=_2
)
set folderName=%date%%z%
mkdir %folderName%
An easier way would be to set folderName inside the IF-counstruct:
#echo on
setlocal enabledelayedexpansion
set t=%time:~0,2%
IF %t% LSS 12 (
set folderName=%date%_1
) ELSE (
set folderName=%date%_2
)
mkdir !folderName!

Basic cmd number loop

I have created a simple program to print numbers 1 to 10 and store them in a text file:
#echo off
SET /A X=1
:START
IF %X% LEQ 10 (
ECHO %X%>>C:\TXT.TXT
SET /A X+=1
GOTO START
)
PAUSE
Output that I am getting is:
ECHO OFF
10
Where have I gone wrong?
You could use a for loop for this (not an if statement):
FOR /L %i IN (1,1,10) do echo %i
(this loops from 1 to 10 in command line)
See also: http://ss64.com/nt/for_l.html
EDIT (as I tried to put the code in my comment -> if you change your code to output in the console, you'll see that your code does work, but in your case the txt only has the last time your echo'd):
#echo off
SET /A X=1
:START
IF %X% LEQ 10 (
ECHO %X%
SET /A X+=1
GOTO START
)
pause

Batch-file programing to get specified line into variable by checking other key word

Im trying to write batch file code to check whether given line is present in text file or not. If it is present I want to get specific line(depending on line number) after that line into variable..
Can anyone help me?
For example
I have text file as...
EX1
EX2
Ex3
EX4
Ex5
now I want to search weather Ex3 is present in batch file or not.
If it is present I want Ex5(2nd line after that) into variable.
Thanks in advance
This works on my box:
#echo off
SET searchterm=Ex3
SET /a lineafter=2
SET filename=lst.txt
:: --------------
SETLOCAL ENABLEDELAYEDEXPANSION
SET /a c=0
FOR /F "delims=" %%i IN (%filename%) DO (
if !c! GTR 0 (
IF %lineafter% EQU !c! (
SET result=%%i
GOTO :linefound
)
SET /a c=!c! + 1
) ELSE (
IF "%%i"=="%searchterm%" (
SET /a c=1
)
)
)
echo No result
GOTO :EOF
:linefound
echo Result: %result%
Just enter your values in the lines 2-4.

Resources