1.

Solve : DATE AS PART OF FILE NAME?

Answer»

I am building a string for a file name that has the date or part of it as the file name. Like (file1mmdd) where mmdd is the month and day. Most of the files I am dealing with are from the current day so there is no problem BUT some may be yesterdays date. Is there a way to SUBTRACT 1 from the day to build this string?

Mike
@echo off
setlocal
call :get_date
:: Strip leading zeros from possible octals and decrement the day
set /a mm=1%mm%-100, dd=1%dd%-101
if %dd% NEQ 0 goto :add_zeros
:: Today is the 1st of the month - decrement the month
:: and set leap year check (ignoring centuries)
set /a mm-=1,ly=YY%%4
:: If today is 1 Jan, set date to 31st Dec
if %mm% EQU 0 (set /a dd=31, mm=12, yy-=1) else (
rem Calculate days in last month (by Frank Westlake)
set /a "dd=5546>>mm&1,dd+=30"
rem Special case for February
if %mm% EQU 2 if %ly% EQU 0 (set dd=29) else (set dd=28)
)
:add_zeros
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%

goto end


:: ------------------------------------------------------------------
:Get_Date
:: ------------------------------------------------------------------
:: Generic date parser
:: Sets %dd% (01-31), %mm% (01-12) & %yy% (4 digit)

if "%date%A" LSS "A" (set toks=1-3) else (set toks=2-4)
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
for /f "tokens=%toks% delims=.-/ " %%d in ('date/t') do (
set %%a=%%d
set %%b=%%e
set %%C=%%f
set toks=
)
)
if %yy% LSS 100 set yy=20%yy%
goto :eof

:end


echo yesterday was %yy%%mm%%dd%
REM next code here
here's a vbscript that is not dependent on your system's date/time settings
Code: [Select]N=Now
THEDAY=Day(N-1)
theMonth=Month(N-1)
If Len(theDay) < 2 Then
theDay = "0"&theDay
End If
If Len(theMonth) < 2 Then
theMonth="0"&theMonth
End If
WScript.Echo theMonth&theDay

to use, save as myscript.vbs:
Code: [Select]C:\test>for /F %i in ('cscript /nologo myscript.vbs') do echo %i
C:\test>echo 0920
0920




Discussion

No Comment Found