How to create a yesterday date in a batch

46,916

Solution 1

Save yourself a world of pain, use a better language!

Here is the script you want in PowerShell:

$yesterday = [DateTime]::Today.AddDays(-1).ToString("yyMMdd")
copy \\server01\E$\LogFiles\IVR\bcR\??${yesterday}.* \\LBC\workgroup\cs\ftp\Team\bcR\
copy \\server02\E$\LogFiles\IVR\bcR\??${yesterday}.* \\LBC\workgroup\cs\ftp\Team\bcR\
copy \\server03\E$\LogFiles\IVR\bcR\??${yesterday}.* \\LBC\workgroup\cs\ftp\Team\bcR\
copy \\server04\E$\LogFiles\IVR\bcR\??${yesterday}.* \\LBC\workgroup\cs\ftp\Team\bcR\

Use these instructions to help you schedule the copy

Solution 2

If you can use powershell, why not do it with this simple one line:

powershell -Command [DateTime]::Now.ToString(\"yyyyMMdd_hhmmss\")

It is an example that outputs actual Year, Month, Day, Hour, Minute and Second.

See last part, it is the format you want, so you can get it with the format you want.

Explanation of what it does:

  • With .ToString(...) it formats the output of [DateTime]::Now in the format yyyyMMdd_hhmmss

And if you want it in a bacth file you can use the trick of for /f "delims=", see an example:

for /f "delims=" %a in ('powershell -Command [DateTime]::Now.ToString(\"yyyyMMdd_hhmmss\"^)') do @echo DateTime is: %a

If you want to use it inside a .BAT or .CMD file you must double the % as this:

for /f "delims=" %%a in ('powershell -Command [DateTime]::Now.ToString(\"yyyyMMdd_hhmmss\"^)') do @echo DateTime is: %%a

Importat note: The ^ character is t avoid for see the ) as final, same as \ does this " inside powershell, etc. It is a meta-charater that tells next character must not be interpreted.

And if you need datetime inside a variable on a batch, see this:

@ECHO OFF
for /f "delims=" %%a in ('powershell -Command [DateTime]::Now.ToString(\"yyyyMMdd_hhmmss\"^)') do @Set MyVariable=%%a
ECHO %MyVariable%

P.D.: There are side effects on using powershell, it changes CMD window size and font (and let them changed after exit), also sometimes it takes some seconds to run such simple command.

Solution 3

As retrieving yesterdays date involves various boundaries (month, years, leap years) it quickly becomes slightly more complex than one thinks. The below script is a slight modification of this post and depends on your machines language settings (run DATE /T on your command line).

@echo off

for /f "tokens=1" %%i in ('date /t') do set thedate=%%i

set yy=%thedate:~2,2%
set mm=%thedate:~5,2%
set dd=%thedate:~8,2%
echo Today    : %yy%%mm%%dd%

if %dd%==08 (
set dd=8 ) else (
if %dd%==09 (
set dd=9 ) )

if %mm%==08 (
set mm=8 ) else (
if %mm%==09 (
set mm=9 ) )

set /A dd=%dd% - 1
set /A mm=%mm% + 0

if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yy=%yy% - 1

:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31

goto ERROR

:SET31
set /A dd=31 + %dd%
goto DONE

:SET30
set /A dd=30 + %dd%
goto DONE

:LEAPCHK
set /A tt=%yy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto DONE

:SET29
set /A dd=29 + %dd%

:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
set YESTERDAY=%yy%%mm%%dd%
echo Yesterday: %YESTERDAY%

Solution 4

Doing this sort of thing in cmd batch files is purely masochistic.

But in any other language it is easy (bash under cygwin, probably powershell; even vbscript might make this reasonably easy).

So just use any other scripting language to either do it all, or to write out a temporary batch file and execute it.

An example in perl:

#!/usr/bin/perl

use strict;
use Date::Format;

my $date = time2str("%y%m%d", time-86400);

for ( qw(01 02 03 04) ) {
    print "copy \\\\server$_\\E\$\\LogFiles\\IVR\\bcR\\??$date.* \\\\LBC\\workgroup\\cs\\ftp\\Team\\bcR\\", "\n";
}

Say it's called writebatch.pl. Then your script could be something like

perl writebatch.pl > "%TEMP%\dailycopies.cmd"
call "%TEMP%\dailycopies.cmd"

Or you could do the copies directly from the script. In Perl, you would use the File::Copy module. If you chose any other scripting tool, it will also have some file copy function.

Share:
46,916

Related videos on Youtube

John Gardeniers
Author by

John Gardeniers

A system administrator migrating from Windows to Linux (but who prefers to use a Mac).

Updated on September 18, 2022

Comments

  • John Gardeniers
    John Gardeniers over 1 year
    copy \\server01\E$\LogFiles\IVR\bcR\??120428.* \\LBC\workgroup\cs\ftp\Team\bcR\
    copy \\server02\E$\LogFiles\IVR\bcR\??120428.* \\LBC\workgroup\cs\ftp\Team\bcR\
    copy \\server03\E$\LogFiles\IVR\bcR\??120428.* \\LBC\workgroup\cs\ftp\Team\bcR\
    copy \\server04\E$\LogFiles\IVR\bcR\??120428.* \\LBC\workgroup\cs\ftp\Team\bcR\
    

    This is my current script and I have to change the date weekly. I am looking for a script that will look at today's current date minus one day and can modify the 12 04 28. (Year Month Day sample 120408). I need yesterday's date. This script is in DOS

    • Admin
      Admin about 12 years
      Batch files are a poor choice when it comes to any sort of arithmetic or string manipulation. What version of DOS are you using or, as I suspect, is this from a command prompt in Windows? If so, which version? Knowing the version will help people decide what solutions might be best for your case. (I'd personally use Perl but of course that's not a native solution.)
  • SmallClanger
    SmallClanger almost 12 years
    +1 for sheer bloody-mindedness :D (I started down this road myself but gave up...)
  • IT Bear
    IT Bear about 6 years
    +1 I do not understand the downvote on this answer. All commands worked exactly as described. Especially since the current top answer also suggests using powershell instead of batch as the solution, yet this answer describes in much more detail how to interface with powershell.exe from cmd.exe