Reputation: 21
I want a variable which increments from 00
to 23
on its every run.
I have to download hourly files every hour, that is why i need this pattern. 0
to 9
should be two digits. Ex. 00, 01, 02, 03, 04 .. 10, 11, 12 .. 23, 00, 01 ..
To achieve above, i have return a simple code as below:
@echo off
set /p VAR=<Hourly_capture.txt
echo %var%
if /I %myvar% LSS 23 (SET /a myvar=%var%+1) else (SET myvar=00)
echo %myvar%
if /I %myvar% EQU 0 (echo 0 >Hourly_capture.txt) else echo %myvar% >Hourly_capture.txt
if /I %var% LSS 10 (if /I %var% NEQ 0 set Hourly_v=0%var%)
if /I %var% EQU 0 (SET Hourly_v=00) else (if /I %var% GTR 9 (SET /a Hourly_v=%var%))
echo %Hourly_v%
Where I enter 0
manually for the very first time into the Hourly_capture.txt
.
Now all this is set. I am able to get this run on terminal (cmd) or also run this as a batch script and take Hourly_v
into some other variable.
But my problem is that I have a main script which needs this variable. When I put this piece of code in my script or call a bat file which hold this data, its exiting. Not getting the reason.
Ex: Considering my above logic to be in a batch script Hourly_logic.bat
. If i run another script that holds
Echo " Iam running Horly_logic script to get the hour variable incremented"
Hourly_logic.bat
Echo "done"
The above main script is coming out immediately when it runs Hourly_logic.bat
.
Even when i try put the above lines directly into the main script i see the script exiting at line 4, i.e. if /I %myvar% LSS 23 (SET /a myvar=%var%+1) else (SET myvar=00)
.
Please help me understand what is going wrong here.
Upvotes: 0
Views: 189
Reputation: 338158
Your main problem is that you need to use call
to execute one batch file from within another.
@echo off
echo I am running Horly_logic script to get the hour variable incremented
call hourly_logic.bat
echo %hourly_v% - done
May I also suggest a nicer approach to incrementing your hourly_capture.txt
file / hourly_v
variable:
@echo off
REM read from file and cast to int
if exist hourly_capture.txt (set /p h=<hourly_capture.txt) else (set h=0)
set /a h=%h%
REM prefix with 0 and take last 2 characters
set hh=0%h%
set hourly_v=%hh:~-2%
REM increment by 1 and save to file
if %h% LSS 23 (set /a h=%h% + 1) else (set h=0)
echo %h% >hourly_capture.txt
Give http://www.dostips.com/DtTipsStringManipulation.php a read, too.
Upvotes: 1