4
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

John Gardeniers
  • 27,262
  • 12
  • 53
  • 108
Joe
  • 41
  • 1
  • 1
  • 2
  • 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

7 Answers7

5

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

dwurf
  • 910
  • 8
  • 15
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.

anonymous
  • 31
  • 1
  • +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 – IT Bear Apr 10 '18 at 08:42
1

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%
erikxiv
  • 147
  • 2
  • 12
1

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.

mivk
  • 3,457
  • 1
  • 34
  • 29
0

Date Time

for /f "delims=" %%a in ('powershell -Command [DateTime]::Now.AddDays^(-1^).ToString^(\"dd-MMM-yyyy\"^)') do @Set MyVariable=%%a

Only Date

for /f "delims=" %%a in ('powershell -Command [DateTime]::Today.AddDays^(-1^).ToString^(\"dd-MMM-yyyy\"^)') do @Set MyVariable2=%%a
Tommiie
  • 5,547
  • 2
  • 11
  • 45
0

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.

Simon Catlin
  • 5,222
  • 3
  • 16
  • 20
-1

for /f "delims=" %%a in ('powershell -Command [DateTime]::Today.AddDays^(-1^).ToString^(\"yyyyMMdd\"^)') do ( set onedayold=%%a )

sze
  • 1