Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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

share|improve this question
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.) – John Gardeniers Apr 30 '12 at 21:25

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

share|improve this answer

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

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

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

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

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

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

set /A dd=29 + %dd%

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%
share|improve this answer
+1 for sheer bloody-mindedness :D (I started down this road myself but gave up...) – SmallClanger May 26 '12 at 8:16

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:


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 Then your script could be something like

perl > "%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|improve this answer

If your main script has to be in Windows Shell Script, my solution for this would be to get the .CMD file to create a [small] .VBS file in %TEMP%, then launch it using a call to cscript.exe, wrapped in a for /f command. The actual VBScript can then use DateDiff. I've used this technique on our Windows Server 2003 boxes, where we haven't deployed PowerShell by default. If you're on a Windows NT6 platform, use PowerShell.

share|improve this answer

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:

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.