I'm working with a nested if statement to delete directories if they meet a certain criteria. This is part of a for loop to check all directories in a given location. Variable %JDcalc% represents the difference between today's julian day and the directory's julian day. Variable %fdate% is the directory name. The errorlevel is deteremined by the following:
echo.%fdate% | findstr /C:"OE"
This nested if statement works:
if %errorlevel% EQU 0 (if %JDcalc% GEQ 31 (echo DELETE) else (echo KEEP)) else (if %JDcalc% GEQ 14 echo DELETE)
As soon as I switch echo DELETE to rd %fdate% the batch script outputs, "The system cannot find the specified file."
if %errorlevel% EQU 0 (if %JDcalc% GEQ 31 (rd %fdate%) else (echo KEEP)) else (if %JDcalc% GEQ 14 rd %fdate%)
If I use a simpler if statement like if %JDcalc% GEQ 14 (rd %fdate%) it works fine.
EDIT: Below is the script in question.
#echo off
cd /d C:\Batch\Maximus\Dailies
:TODAYS_JULIAN_DAY
set /a JD=2458939
::a calculation that determines this number is usually here
::above is the julian day for 3.30.2020
:FOR_LOOP_FOLDER_NAME
for /f "delims=" %%a in ('dir /a:d /b') do (
SETLOCAL EnableDelayedExpansion
set fdate=%%a
::fdate is set to the date in the directory name
::with a naming convention of yymmdd
call :SUBROUTINE
ENDLOCAL
)
GOTO END_BATCH
:SUBROUTINE
set folyy=%fdate:~0,2%
set folmm=%fdate:~2,2%
set foldd=%fdate:~4,2%
set /a "folyy=1000020%folyy% %%10000,folmm=100%folmm% %% 100,foldd=100%foldd% %% 100"
set /a folJD=foldd-32075+1461*(folyy+4800+(folmm-14)/12)/4+367*(folmm-2-(folmm-14)/12*12)/12-3*((folyy+4900+(folmm-14)/12)/100)/4
set /a JDcalc=JD-folJD
echo.%fdate% | findstr /C:"OE"
if %errorlevel% EQU 0 (if %JDcalc% GEQ 31 (rd %fdate%) else (echo KEEP)) else (if %JDcalc% GEQ 14 rd %fdate%)
GOTO :eof
:END_BATCH
ENDLOCAL
Related
This question already has an answer here:
How to generate succesive dates? (add N days to date)
(1 answer)
Closed last month.
I have bat script for loop function
#echo off set /p a="Start Date(yyyymmdd):"%=% set /p b="Finish Date
(yyyymmdd):"%=%
echo %a% echo %b%
echo start loop
FOR /L %%c IN (%a%,1,%b%) DO ( echo %%c
set inputan=%%c
call test_loopong.bat %inputan%
)
I need to run according to the range of start and end date input.
But from the script above, for example the start date is 20221231 and the end date is 20230103
will loop all numbers from 20221231, 20221232, 20221233......20230103
Please someone can help me.
Thanks
#ECHO Off
SETLOCAL
set /p "startdate=Start Date(yyyymmdd):"
set /p "finishdate=Finish Date (yyyymmdd):"
echo %startdate%
echo %finishdate%
IF %finishdate% lss %startdate% ECHO Invalid DATE range&GOTO :eof
echo start loop
FOR /L %%c IN (%startdate%,1,%finishdate%) DO (
FOR /F %%y IN ('set /a inputan^=%%c %% 100') DO IF %%y lss 32 IF %%y gtr 0 (
echo %%c
set /a inputan=%%c
call :test_loopong.bat %%c
)
)
GOTO :EOF
:test_loopong.bat
ECHO IN test_loopong : %%1=%1 inputan=%inputan%
GOTO :eof
I changed the variable names so that they are maintainable, 'though I've no idea what inputan means.
Added invalid date-range check.
Note syntax of set /p
Forget for the moment what for...%%y... does.
I used a set /a since %%c must be numeric.
Use set "var1=value" for setting STRING values - this avoids problems caused by trailing spaces.
I converted test_loopong.bat to an internal subroutine (call :name) for demonstration purposes to avoid having to generate another file.
I'm having that subroutine simply display the value of the variable inputan and the parameter %1. %%c can be delivered as a parameter to the subroutine (be it internal or external) but your syntax would appear to omit the parameter because of the delayed expansion trap - %var% will be replaced by the value of var at the time the outer loop (for...%%c) is encountered.
Now the for...%%y jiggery-pokery.
The command set /a inputan=%%c % 100 would set inputan to %%c mod 100. There's no importance about the variable name - that one's about to be assigned a different value again in a couple of lines.
When used in a for /f, batch will execute the set/a command and echo the result to the metavariable %%y.
However, = and % are special characters and need to be "escaped" (interpreted without their special meaning). Th escape character for most specials is caret (^) but for % is % itself.
The result of the calculation is assigned to %%y and we can then test that %%y is lss 32 - Less than 32, and also it is gtr 0 - Greater than 0. Only then do we call test_loopong.bat. This eliminates most non-dates
If you don't want to skip the non-dates, then remove the for...%%y line and delete one of the ) lines.
--- further thoughts ----
Suppressing the days 32..99 & 00 really only does half the job. Much better if we suppress months 13..99 & 00. The revision then would be (presenting just the main loop, minus the frippery)
FOR /L %%c IN (%startdate%,1,%finishdate%) DO (
FOR /F %%e IN ('set /a inputan^=%%c %% 10000') DO IF %%e lss 1232 IF %%e gtr 100 (
FOR /F %%y IN ('set /a inputan^=%%c %% 100') DO IF %%y lss 32 IF %%y gtr 0 (
echo %%c
set /a inputan=%%c
call :test_loopong.bat %%c
)
)
)
Same principle, just dealing with months in place of days.
One slight problem with this method is that it's as slow as a wet week.
So - a different approach
SET /a yyyy=%startdate:~0,4%
SET /a mm1=1%startdate:~4,2%
SET /a dd1=1%startdate:~-2%
:loop2
SET /a inputan=%yyyy%0000+%mm1%00+dd1-10100
IF %inputan% gtr %finishdate% GOTO :eof
CALL :test_loopong.bat %inputan%
SET /a dd1+=1
IF %dd1% leq 131 GOTO loop2
SET /a dd1=101&SET /a mm1+=1
IF %mm1% leq 112 GOTO loop2
SET /a mm1=101
SET /a yyyy+=1
GOTO loop2
This sets yyyy to the year and mm1/dd1 to 100+(month/day). 100+ needs to be done since batch regards a numeric string that starts 0 as
octal hence August and September cause problems.
So - calculate inputan by tringing 4 0s to yyyy, adding 100*mm1 and dd1, then subtracting 10100 since mm1 is mm+100 and dd1 is dd+100.
If the resultant inputan is greater than the finish date, end the routine.
Test using inputan.
Next day - add 1 to dd1.
If the result is less than or equal to 131, we're fine.
Otherwise set dd1 to 101 and increment the month
Same recipe for mm1, limit is 112 and bump the year if required.
---- Further revision to deal with non-dates 31st Apr, Jun, Sep, Nov and Feb (include leap years) ---
#ECHO Off
SETLOCAL
set /p "startdate=Start Date(yyyymmdd):"
set /p "finishdate=Finish Date (yyyymmdd):"
echo %startdate%
echo %finishdate%
IF %finishdate% lss %startdate% ECHO Invalid DATE range&GOTO :eof
echo start loop
GOTO ver2
FOR /L %%c IN (%startdate%,1,%finishdate%) DO (
FOR /F %%e IN ('set /a inputan^=%%c %% 10000') DO IF %%e lss 1232 IF %%e gtr 100 (
FOR /F %%y IN ('set /a inputan^=%%c %% 100') DO IF %%y lss 32 IF %%y gtr 0 (
echo %%c
set /a inputan=%%c
call :test_loopong.bat %%c
)
)
)
GOTO :EOF
:ver2
SET /a yyyy=%startdate:~0,4%
SET /a mm1=1%startdate:~4,2%
SET /a dd1=1%startdate:~-2%
:loop2
SET /a inputan=%yyyy%0000+%mm1%00+dd1-10100
IF %inputan% gtr %finishdate% GOTO :eof
CALL :test_loopong.bat %inputan%
SET /a dd1+=1
SET /a inputan=yyyy %% 4
IF %mm1%==102 IF %dd1% gtr 129 (GOTO nextmonth) ELSE IF %inputan% neq 0 IF %dd1%==129 GOTO nextmonth
FOR %%e IN (104 106 109 111) DO IF %%e131==%mm1%%dd1% GOTO nextmonth
IF %dd1% leq 131 GOTO loop2
:nextmonth
SET /a dd1=101&SET /a mm1+=1
IF %mm1% leq 112 GOTO loop2
SET /a mm1=101
SET /a yyyy+=1
GOTO loop2
GOTO :eof
:test_loopong.bat
ECHO IN test_loopong : %%1=%1 inputan=%inputan%
GOTO :eof
I am currently making a game that has a persuasion system in it. I had all the code for one of the systems set up, but then I set up 2 more, and it started give me an error that said '(number) was not expected at this time'. when I put in 2 for the second choice, and 3 for the 3rd choice.
The code is like this.
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
set name=Quantum
cls
color 0a
Echo King Redwood: So 2000?
pause >nul
echo.
call :colorText 09 "1. 2500"
echo.
call :colorText 0e "2. 3000"
echo.
call :colorText 0c "3. 4000"
echo.
echo 4. 2000
echo.
set /p "purs=Enter:"
if /i %purs% == 1 (
goto CheckB )
if /i %purs% == 2 (
goto CheckY )
if /i %purs% == 3 (
goto CheckR )
if /i %purs% == 4 (
goto Convo )
:CheckB
set bleu=%random:~-2,1%
if %bleu% GTR 10 (
goto CheckB )
if %bleu% LSS 0 (
goto CheckB )
set /a num = 3
set /a reward = 2500
goto Res
:CheckY
set Yel=%random:~-2,1%
if %Yel% GTR 10 (
goto CheckY )
if %Yel% LSS 0 (
goto CheckY )
set /a num = 5
set reward = 3000
goto Res
:CheckR
set red=%random:~-2,1%
if %red% GTR 10 (
goto CheckB )
if %red% LSS 0 (
goto CheckB )
set /a num = 7
set /a reward = 4000
goto Res
:Convo
set /a reward = 2000
Echo %name%: I think that is a reasonable price.
Echo King Redwood: Very well.
Echo King Redwood: We will now take you to make sure you are
echo ready.
pause >nul
:Res
if %bleu% GEQ %num% goto Succeed
if NOT %bleu% GEQ %num% goto Fail
:Succeed
Echo %name%: I think that the struggles for such a long trip will be more then that
Echo %name%: How about %reward%?
Echo King Redwod: OK %reward% will work.
pause >nul
goto end
:Fail
Echo %name%: I think that you can give me %reward%.
Echo %name%: You know, for the struggles that there will be along the way.
echo If 2000 isn't good enough for you, I'll just have someone else do it.
pause >nul
:end
exit
:colorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
First, make sure to close the FOR loop by putting a ) before :CheckB.
For the 'was not expected at this time' error, you're sometimes comparing an empty variable to something. For example, by following CheckY, you set Yel, then proceed to Res and check Bleu, which is empty because it hasn't been set. You're putting nothing next to the binary GEQ operator, and that's why it's complaining.
Tip: to debug, try inserting ECHO statements like this:
:Res
echo bleu=%bleu%,num=%num%
Another problem: when using SET, do not surround the = by spaces. SET /a will work with spaces around =, just because of the nature of /a, but plain SET will not. Well, it will append your variable name with a space and prepend your value with a space, which is not what you want.
Another tip: you can constrain what RANDOM returns through SET /a and the modulus operator, like this.
SET /a red=%random% %% 11
This will set red to a number between 0 and 10, so there is no need for the substrings and goto routines you're using after picking your random number.
Also, consider using EXIT /b to exit the batch file and not the whole CMD environment.
I have a log file like below.
[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1019025)
Reading Rules From Rule Object For Database [PL]
[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1013157)
Received Command [Import] from user [giuraja#MSAD] using [AIF0142.rul] with data file [SQL]
.
.
.
.
.
Clear Active on User [giuraja#MSAD] Instance [1]
.
.
I want to extract the line starting with "[Tue Aug 19 10:" until the line that starts with "Clear Active on User" and output to a file using windows batch script. I tried the below code. It only outputs the last line.
#echo off & setlocal enabledelayedexpansion
set Month_Num=%date:~4,2%
if %Month_Num%==08 set Month_Name=Aug
set Day=%date:~0,3%
set Today_Date=%date:~7,2%
set Search_String=[%Day% %Month_Name% %Today_Date% 10:
for /f "tokens=1 delims=[]" %%a in ('find /n "%Search_String%"^
#(
more +%%a D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN.LOG)>D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Temp.txt
(for /f "tokens=*" %%a in (D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Temp.txt) do (
set test=%%a
if "!test:~0,20!" equ "Clear Active on User" goto :eof
echo %%a
))>D:\Hyperion\ERPI_Actuals_Load\Logs\PLPLAN_Formatted.txt
Regards,
Ragav.
The Batch file below was designed to process as fast as possible a big file; however, it deletes empty lines from result:
#echo off
setlocal EnableDelayedExpansion
set "start=[Tue Aug 19 10:"
set "end=Clear Active on User"
for /F %%a in ("%start%") do set startWord=%%a
for /F %%a in ("%end%") do set endWord=%%a
set "startLine="
set "endLine="
for /F "tokens=1,2 delims=: " %%a in ('findstr /I /N /B /C:"%start%" /C:"%end%" logFile.txt') do (
if not defined startLine if "%%b" equ "%startWord%" set startLine=%%a
if not defined endLine if "%%b" equ "%endWord%" set "endLine=%%a" & goto continue0
)
:continue0
set /A skipLines=startLine-1, numLines=endLine-startLine+1
set "skip="
if %skipLines% gtr 0 set skip=skip=%skipLines%
(for /F "%skip% delims=" %%a in (logFile.txt) do (
echo %%a
set /A numLines-=1
if !numLines! equ 0 goto continue1
)) > outFile1.txt
:continue1
rem Previous outFile1.txt contain as many extra lines as empty lines removed, so we need to eliminate they
for /F "delims=:" %%a in ('findstr /I /N /B /C:"%end%" outFile1.txt') do set numLines=%%a
(for /F "delims=" %%a in (outFile1.txt) do (
echo %%a
set /A numLines-=1
if !numLines! equ 0 goto continue2
)) > outFile2.txt
:continue2
del outFile1.txt
TYPE outFile2.txt
If you want to preserve empty lines, the process would be much slower.
This should work (tested)
#echo off
set "st_line=Tue Aug 19"
set "end_line=Clear Active on User"
for /f "delims=:" %%i in ('findstr /inc:"%st_line%" logfile.txt') do (set st_line_ln=%%i)
for /f "delims=:" %%j in ('findstr /inc:"%end_line%" logfile.txt') do (set end_line_ln=%%j)
findstr /in /c:[a-z] /c:[0-9] /rc:"^$" logfile.txt >logfile_ln.txt
set /a "st_line_ln_temp=%st_line_ln-1"
for /f "skip=%st_line_ln_temp% tokens=1* delims=:" %%a in ('type logfile_ln.txt') do (
if %%a leq %end_line_ln% (echo.%%b)
)
del logfile_ln.txt
Sample output -
C:\test>type logfile.txt
Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1019025)
Reading Rules From Rule Object For Database [PL]
[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1013157)
Received Command [Import] from user [giuraja#MSAD] using [AIF0142.rul] with data file [SQL]
test1
test2
test4
test546
Clear Active on User [giuraja#MSAD] Instance [1
test1212
test232
test67
dj
C:\test>draft.bat
[Tue Aug 19 10:45:28 2014]Local/PLPLAN/PL/giuraja#MSAD/2172/Info(1013157)
Received Command [Import] from user [giuraja#MSAD] using [AIF0142.rul] with data file [SQL]
test1
test2
test4
test546
Clear Active on User [giuraja#MSAD] Instance [1
C:\test>
Cheers, G
#ECHO OFF
SETLOCAL
:: If you don't want to preserve empty lines
SET "select="
(
FOR /f "delims=" %%a IN (q25390541.txt) DO (
ECHO %%a|FINDSTR /b /L /c:"[Tue Aug 19 10:" >NUL
IF NOT ERRORLEVEL 1 SET select=y
IF DEFINED select ECHO(%%a
ECHO %%a|FINDSTR /b /L /c:"Clear Active on User" >NUL
IF NOT ERRORLEVEL 1 GOTO done1
)
)>newfile.txt
:done1
:: If you want to preserve empty lines
SET "select="
(
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r ".*" q25390541.txt') DO (
IF "%%b"=="" (
IF DEFINED select ECHO(
) ELSE (
ECHO %%b|FINDSTR /b /L /c:"[Tue Aug 19 10:" >NUL
IF NOT ERRORLEVEL 1 SET select=Y
IF DEFINED select ECHO(%%b
ECHO %%b|FINDSTR /b /L /c:"Clear Active on User" >NUL
IF NOT ERRORLEVEL 1 GOTO done2
)
)
)>newfile2.txt
:done2
GOTO :EOF
I used a file named q25390541.txt containing your data for my testing.
Produces newfile.txt and newfile2.txt depending on which is preferred.
This is an inclusive (start and end line are shown) script that should do the job. You can modify it to exclude the lines:
#echo off
setlocal
set filename=text_file.txt
for /f "tokens=1 delims=:" %%a in ('findstr /n /b /c:"[Tue Aug " %filename%') do (
set /a f_line=%%a-1
)
for /f "tokens=1 delims=:" %%a in ('findstr /n /b /c:"Clear Active on User" %filename%') do (
set /a l_line=%%a
)
echo %l_line% -- %f_line%
call :tail_head2 -file=%filename% -begin=%f_line% -end=%l_line%
exit /b 0
#echo off
:tail_head2
setlocal
rem ---------------------------
rem ------ arg parsing --------
rem ---------------------------
if "%~1" equ "" goto :help
for %%H in (/h -h /help -help) do (
if /I "%~1" equ "%%H" goto :help
)
setlocal enableDelayedExpansion
set "prev="
for %%A in (%*) do (
if /I "!prev!" equ "-file" set file=%%~fsA
if /I "!prev!" equ "-begin" set begin=%%~A
if /I "!prev!" equ "-end" set end=%%A
set prev=%%~A
)
endlocal & (
if "%file%" neq "" (set file=%file%)
if "%begin%" neq "" (set /a begin=%begin%)
if "%end%" neq "" (set /a end=%end%)
)
rem -----------------------------
rem --- invalid cases check -----
rem -----------------------------
if "%file%" EQU "" echo file not defined && exit /b 1
if not exist "%file%" echo file not exists && exit /b 2
if not defined begin if not defined end echo neither BEGIN line nor END line are defined && exit /b 3
rem --------------------------
rem -- function selection ----
rem --------------------------
if defined begin if %begin%0 LSS 0 for /F %%C in ('find /c /v "" ^<"%file%"') do set /a lines_count=%%C
if defined end if %end%0 LSS 0 if not defined lines_count for /F %%C in ('find /c /v "" ^<"%file%"') do set lines_count=%%C
rem -- begin only
if not defined begin if defined end if %end%0 GEQ 0 goto :end_only
if not defined begin if defined end if %end%0 LSS 0 (
set /a end=%lines_count%%end%+1
goto :end_only
)
rem -- end only
if not defined end if defined begin if %begin%0 GEQ 0 goto :begin_only
if not defined end if defined begin if %begin%0 LSS 0 (
set /a begin=%lines_count%%begin%+1
goto :begin_only
)
rem -- begin and end
if %begin%0 LSS 0 if %end%0 LSS 0 (
set /a begin=%lines_count%%begin%+1
set /a end=%lines_count%%end%+1
goto :begin_end
)
if %begin%0 LSS 0 if %end%0 GEQ 0 (
set /a begin=%lines_count%%begin%+1
goto :begin_end
)
if %begin%0 GEQ 0 if %end%0 LSS 0 (
set /a end=%lines_count%%end%+1
goto :begin_end
)
if %begin%0 GEQ 0 if %end%0 GEQ 0 (
goto :begin_end
)
goto :eof
rem -------------------------
rem ------ functions --------
rem -------------------------
rem ----- single cases -----
:begin_only
setlocal DisableDelayedExpansion
for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
set "line=%%L"
for /F "delims=:" %%n in ("%%L") do (
if %%n GEQ %begin% (
setlocal EnableDelayedExpansion
set "text=!line:*:=!"
(echo(!text!)
endlocal
)
)
)
endlocal
endlocal
goto :eof
:end_only
setlocal disableDelayedExpansion
for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
set "line=%%L"
for /F "delims=:" %%n in ("%%L") do (
IF %%n LEQ %end% (
setlocal EnableDelayedExpansion
set "text=!line:*:=!"
(echo(!text!)
endlocal
) ELSE goto :break_eo
)
)
:break_eo
endlocal
endlocal
goto :eof
rem --- end and begin case -----
:begin_end
setlocal disableDelayedExpansion
if %begin% GTR %end% goto :break_be
for /F "delims=" %%L in ('findstr /R /N "^" "%file%"') do (
set "line=%%L"
for /F "delims=:" %%n in ("%%L") do (
IF %%n GEQ %begin% IF %%n LEQ %end% (
setlocal EnableDelayedExpansion
set "text=!line:*:=!"
(echo(!text!)
endlocal
) ELSE goto :break_be
)
)
:break_be
endlocal
endlocal
goto :eof
rem ------------------
rem --- HELP ---------
rem ------------------
:help
echo(
echo %~n0 - dipsplays a lines of a file defined by -BEGIN and -END arguments passed to it
echo(
echo( USAGE:
echo(
echo %~n0 -file=file_to_process {-begin=begin_line ^| -end=end_line }
echo or
echo %~n0 -file file_to_process {-begin begin_line ^| -end end_line }
echo(
echo( if some of arguments BEGIN or END has a negative number it will start to count from the end of file
echo(
echo( http://ss64.org/viewtopic.php^?id^=1707
echo(
goto :eof
EDIT - last 100 lines:
#echo off
setlocal
set filename=text_file.txt
for /F %%C in ('find /c /v "" ^<"%filename%"') do set /a lines_count=%%C
set /a last_100_lines=lines_count-100
type %filename% | more /e +%last_100_lines%
I have a batch file and within that batch file I need to run one of two commands depending on time of my server.
If the time is between 22:00:00 and 03:30:00 -- xcopy /Y a\1.txt c\1.txt
If the time is before or after this range -- -- xcopy /Y b\1.txt c\1.txt
This will use xcopy to switch a file back and forth depending on the time.
I know this is easy but my brain just won't work atm lol
Edit:
Went with 22:00 and 4:00... this is what I came up with but it doesn't seem like the best way...
set current_time=%time:~0,5%
if "%current_time%" lss "22:00" goto daycycle
if "%current_time%" gtr " 4:00" goto daycycle
echo Do this between 10pm and 4am
goto continue
:daycycle
echo Do this before 10pm and after 4am
:continue
#echo off
setlocal enableextensions disabledelayedexpansion
set "now=%time: =0%"
set "task=day"
if "%now%" lss "03:30:00,00" ( set "task=night" )
if "%now%" geq "22:00:00,00" ( set "task=night" )
call :task_%task%
endlocal
exit /b
:task_day
:: do daily task
goto :eof
:task_night
:: do nightly task
goto :eof
EDITED - The previous code should work under the conditions in the original question. But will fail in different time configurations. This should solve the usual problems
#echo off
setlocal enableextensions disabledelayedexpansion
call :getTime now
set "task=day"
if "%now%" lss "03:30:00,00" ( set "task=night" )
if "%now%" geq "22:00:00,00" ( set "task=night" )
call :task_%task%
echo %now%
endlocal
exit /b
:task_day
:: do daily task
goto :eof
:task_night
:: do nightly task
goto :eof
:: getTime
:: This routine returns the current (or passed as argument) time
:: in the form hh:mm:ss,cc in 24h format, with two digits in each
:: of the segments, 0 prefixed where needed.
:getTime returnVar [time]
setlocal enableextensions disabledelayedexpansion
:: Retrieve parameters if present. Else take current time
if "%~2"=="" ( set "t=%time%" ) else ( set "t=%~2" )
:: Test if time contains "correct" (usual) data. Else try something else
echo(%t%|findstr /i /r /x /c:"[0-9:,.apm -]*" >nul || (
set "t="
for /f "tokens=2" %%a in ('2^>nul robocopy "|" . /njh') do (
if not defined t set "t=%%a,00"
)
rem If we do not have a valid time string, leave
if not defined t exit /b
)
:: Check if 24h time adjust is needed
if not "%t:pm=%"=="%t%" (set "p=12" ) else (set "p=0")
:: Separate the elements of the time string
for /f "tokens=1-5 delims=:.,-PpAaMm " %%a in ("%t%") do (
set "h=%%a" & set "m=00%%b" & set "s=00%%c" & set "c=00%%d"
)
:: Adjust the hour part of the time string
set /a "h=100%h%+%p%"
:: Clean up and return the new time string
endlocal & if not "%~1"=="" set "%~1=%h:~-2%:%m:~-2%:%s:~-2%,%c:~-2%" & exit /b
Try this:
#echo off
set hour=%time:~0,2%
set min=%time:~3,2%
if %hour% GEQ 22 (
xcopy /Y a\1.txt c\1.txt
) ELSE (
if %hour% LEQ 3 (
if %hour% EQU 3 if %min% GTR 30 (
xcopy /Y b\1.txt c\1.txt
goto :END
)
xcopy /Y a\1.txt c\1.txt
) ELSE (
xcopy /Y b\1.txt c\1.txt
goto :END
)
)
:END
And I'm rather sure that will do what you want. Manual if statements for the win! Note it would be a lot easier if it was 22:00 to 4:00 or 3:00. I had to incorperate the :30 minute checker.
But yea, it might not work, so just check it before you put it up on your server.
Monacraft.
What should work:
#echo off
:loop
set hour=%time:~0,2%
set min=%time:~3,2%
cls
ECHO %hour%
ECHO %min%
ECHO This %I%
IF %hour% == 14 GOTO Test2
goto loop
:Test2
IF %min% == 58 GOTO YUP
IF %min% == 59 GOTO LATE
Goto loop
:YUP
SET I=0
GOTO loop
:LATE
SET I=NOPE
GOTO loop
i have writing a window batch to del the files order than 3 months in some directory, however, there are some problem on :Loop_Folder_Del_Old_Files. I don't know why i cannot assign the last modified date of the files, please help to find out the problem. Below are the source code and program result, thanks!
Source Code
:program_start
#ECHO ON
echo program_start
:Parameter_Settings
set filePath=c:\New Folder
set delPeriod=3
echo %date%
set curYYYY=%date:~10,4%
set curMM=%date:~7,2%
set curDD=%date:~4,2%
:Set_The_Date_Of_3_Months_Ago
set /A curMM=curMM - %delPeriod%
if "%curMM%" LEQ "0" (
set /A curMM="(curMM + 12 - %delPeriod%)%%12"
if %curMM% == 0 (set curMM=12) ELSE (set curMM=%curMM%)
set /A curYYYY=curYYYY - 1
)
set curMM=00%curMM%
set curMM=%curMM:~-2%
set curDate=%curYYYY%%curMM%%curDD%
:Loop_Folder_Del_Old_Files
for %%a IN ("%filePath%\*.*") DO (
set ltdate=%%~ta
set fileDate=%ltdate:~6,4%%ltdate:~3,2%%ltdate:~0,2%
if "%fileDate%" LSS "%curDate%" Del /Q "%%a"
)
:end
echo program end
pause
Result
C:\>echo program_start
program_start
C:\>set filePath=c:\New Folder
C:\>set delPeriod=3
C:\>echo Fri 22/11/2013
Fri 22/11/2013
C:\>set curYYYY=2013
C:\>set curMM=11
C:\>set curDD=22
C:\>set /A curMM=curMM - 3
C:\>if "8" LEQ "0" (
set /A curMM="(curMM + 12 - 3)%12"
if 8 == 0 (set curMM=12 ) ELSE (set curMM=8 )
set /A curYYYY=curYYYY - 1
)
C:\>set curMM=008
C:\>set curMM=08
C:\>set curDate=20130822
C:\>for %a IN ("c:\New Folder*.*") DO (
set ltdate=%~ta
set fileDate=~6,4%ltdate:~3,2%ltdate:~0,2
if "" LSS "20130822" Del /Q "%a"
)
C:\>(
set ltdate=22/11/2013 05:36 PM
set fileDate=~6,4%ltdate:~3,2%ltdate:~0,2
if "" LSS "20130822" Del /Q "c:\New Folder\New Text Document (2).txt"
)
C:\>(
set ltdate=22/11/2013 05:36 PM
set fileDate=~6,4%ltdate:~3,2%ltdate:~0,2
if "" LSS "20130822" Del /Q "c:\New Folder\New Text Document.txt"
)
C:\>echo program end
program end
C:\>pause
Press any key to continue . . .
maybe the use of forfiles to delete old files could be better in handle?
You need to enable delayed expansion to make your variables work inside the for loop.
Add
setlocal enabledelayedexpansion
at the top of your bat file
and then, refer to the variable using the !var! syntax (instead of %var%)
if "!fileDate!" LSS "!curDate!" Del /Q "%%a"
Further to PA's correct response,
set /A curMM=curMM - %delPeriod%
if "%curMM%" LEQ "0" (
set /A curMM="(curMM + 12 - %delPeriod%)%%12"
if %curMM% == 0 (set curMM=12) ELSE (set curMM=%curMM%)
set /A curYYYY=curYYYY - 1
)
Is unreliable as currMM may be 08 or 09 which would generate a syntax-error as batch assumes that any numeric starting with 0 is OCTAL.
set /A curMM=1%curMM% - 100 - %delPeriod%
if %curMM% LEQ 0 (
set /A curMM=curMM + 12
set /A curYYYY=curYYYY - 1
)
is better. The comparison is executed against the NUMERIC value, not a string value. The value of curMM would be 0 for Dec, -1 for Nov, etc - so simply adding 12 and subtracting 1 from the year is sufficient and clearer.