0

I have to execute a program.exe that generate me reports for a range of dates, I call the program.exe in a command line but i have to set two argument (current date and current_date-6days) when I call it :

c:\program.exe current_date current_date-6 (date before 6 days)

i found that this command gives me the current date : 28-06-2013 %date:~0,2%-%date:~-7,2%-%date:~-4,4%

but I can't get the current_date-6 (means the date 6 days before)

i have also to let the user choose the range of date he want ...

how I can proceed to execute the program 1 )automatically (windows schedular) by giving him the two parametres (current date , and date of 6 days before for example) 2) and manually in the same time, by giving the users the possibility of choosing a range of dates

@echo off

set /a currd=%date:~0,2%
set /a currm=%date:~3,2%
set /a curry=%date:~6,4%

set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%

FOR %%A IN (1 2 3 4 5 6) DO (
    :loop
      set /a d-=1

      if %d% LSS 1 (
        set d=1
        set /a m-=1

        if %m% LSS 1 (
          set m=12
          set /a y-=1
        )
      )
    xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto loop
)

start /WAIT /B "" "C:\MyPGM.exe" "%d%-%m%-%y%" "%currd%-%currm%-%curry%"

for the 28/06/2013 I have no problem but for the 01/07/2013 it generate me an error "String was not recognized as a valid DateTime"

4

3 回答 3

3

这个 VBS 脚本有能力给你日期 - 6 和今天。

启动它以查看帮助。

:: Date foward & backward
@echo off
if "%~2"=="" (
echo to get todays date use         call "%~n0" today 0
echo to get yesterdays date use     call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today 1250
echo.
echo Add a third parameter if you want a separator in the date string
echo EG: to use - as in YYYY-MM-DD for today's date
echo     call "%~n0" today 0 -
echo.
pause
goto :EOF)

set date1=%1
set qty=%2
set separator=%~3
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%%separator%%result:~4,2%%separator%%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)
pause
于 2013-06-28T16:22:22.807 回答
3

这是一个纯批处理解决方案,它在第一个参数中获取要减去的天数:

@echo off
setlocal
for /F "tokens=1-3 delims=/" %%a in ("%date%") do (
   set Current_date=%%a-%%b-%%c
   set /A dd=1%%a-100, mm=1%%b-100, yyyy=%%c
)
set /A a=(mm-14)/12, jdn=(1461*(yyyy+4800+a))/4+(367*(mm-2-12*a))/12-(3*((yyyy+4900+a)/100))/4+dd-32075-%1
set /A l=jdn+68569,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447
set /A dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yyyy=100*(n-49)+i+l
if %dd% lss 10 set dd=0%dd%
if %mm% lss 10 set mm=0%mm%
set Previous_date=%dd%-%mm%-%yyyy%
echo Current date:               %Current_date%
echo Current date minus %1 days: %Previous_date%

例如:

>test 6
Current date:               29-06-2013
Current date minus 6 days: 23-06-2013

>test 60
Current date:               29-06-2013
Current date minus 60 days: 30-04-2013

>test 600
Current date:               29-06-2013
Current date minus 600 days: 07-11-2011

参考:http ://www.hermetic.ch/cal_stud/jdn.htm#comp

于 2013-06-29T08:29:01.437 回答
0

您可以使用其他语言非常轻松地执行此操作(日期操作)。以下是 Perl、Python 和 Powershell 中的示例。

@ECHO OFF
SETLOCAL

SET PERL_CMD=perl -MPOSIX -e "print(POSIX::strftime(q(%%d-%%m-%%Y),localtime(time()-6*86400)));"
FOR /F %%d IN ('%PERL_CMD%') DO SET PREV_DATE=%%d
ECHO %PREV_DATE%

SET PYTHON_CMD=py -3 -c "from datetime import *; print((datetime.now()+timedelta(days=-6)).strftime('%%d-%%m-%%Y'));" 
FOR /F %%d IN ('%PYTHON_CMD%') DO SET PREV_DATE=%%d
ECHO %PREV_DATE%

SET POWERSHELL_CMD=powershell -NoProfile -Command "Get-Date ((Get-Date).AddDays(-6)) -format dd.MM.yyyy"
FOR /F %%d IN ('%POWERSHELL_CMD%') DO SET PREV_DATE=%%d
ECHO %PREV_DATE%

如果您绝对必须在批处理/CMD 中执行此操作,则可以(大部分)通过解析日期字符串并对日、月和年值进行数学运算来完成。

于 2013-06-28T14:09:49.463 回答