Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.
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
2  
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

4 Answers 4

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

: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%
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:

#!/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|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

Your Answer

 
discard

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.