Monday, June 13, 2011

Passing the Current Date to XCOPY

XCOPY is a really useful console command that allows you to make copies of files in a fast and easy way. One of the parameters of this command is '/d':

/d[:mm-dd-yyyy] : Copies source files changed on or after the specified date only. If you do not include a mm-dd-yyyy value, xcopy copies all Source files that are newer than existing Destination files. This command-line option allows you to update files that have changed.

If you need to pass the current date to this command, you'll be surprised: on some configurations of the operating system %date% returns the current date in dd.mm.yyy format, while XCOPY requires mm-dd-yyyy. What to do? Here is some batch code that helps you to solve this problem:

@echo off
setlocal
set "dd=%date:~0,2%"
set "mm=%date:~3,2%"
set "yy=%date:~-2%"
xcopy c:\*.txt c:\temp /d:%mm%-%dd%-%yy%

You can use this code in your batch files that can be written with the help of our award-winning batch files editor. Hope you'll find this information useful.

2 comments:

  1. Thanks for the post mental-works. An excellent starting place indeed!! I needed to go back a few days before todays date to backup files with XCopy. So I added some more code to cover that as well as check for january and if true go back to December 26 of the prior year. Hope it helps the community.

    echo off
    rem
    rem my system date format = Fri 11/30/2012
    rem
    rem start code....
    rem
    set "mm=%date:~4,2%"
    set "dd=%date:~7,2%"
    set "yy=%date:~-2%"
    If %dd% GTR 2 (set /A dd=%dd%-2)
    If %dd% EQU 2 (set /A dd=%dd%-1)
    If %dd% EQU 1 goto FirstDayOfMonth
    rem
    REM less=LSS,LEQ <=,GEQ >=,NEQ Not equal
    REM Could use an else statement eg. Else (set dd=28)
    rem
    rem
    rem test drive
    rem
    xcopy c:\music\test e:\backups /d:%mm%-%dd%-%yy% /f /s /y /z
    if errorlevel 4 goto lowmemory
    if errorlevel 2 goto abort
    rem
    rem
    echo date completed %date% %time%
    echo Backup from date %mm%-%dd%-%yy%
    rem
    if errorlevel 4 goto lowmemory
    if errorlevel 2 goto abort
    if errorlevel 0 goto exit
    rem
    rem
    :FirstDayOfMonth
    IF %mm% EQU 1 (set /A yy=%yy%-1) Else (set /A mm=%mm%-1)
    IF %mm% EQU 1 (set dd=26)
    IF %mm% EQU 1 (set mm=12)
    xcopy c:\music\test e:\backups /d:%mm%-%dd%-%yy% /f /s /y /z
    echo January code %mm%-%dd%-%yy%
    rem
    rem end jan code...
    rem
    if errorlevel 4 goto lowmemory
    if errorlevel 2 goto abort
    rem
    rem
    :lowmemory
    echo Insufficient memory to copy files or
    echo invalid drive or command-line syntax.
    goto exit
    :abort
    echo You pressed CTRL+C to end the copy operation.
    goto exit
    :exit

    ReplyDelete
  2. Thanks for the post mental-works. An excellent starting place indeed!! I needed to go back a few days before todays date to backup files with XCopy. So I added some more code to cover that as well as check for january and if true go back to December 26 of the prior year. Hope it helps the community.

    echo off
    rem
    rem my system date format = Fri 11/30/2012
    rem
    rem start code....
    rem
    set "mm=%date:~4,2%"
    set "dd=%date:~7,2%"
    set "yy=%date:~-2%"
    If %dd% GTR 2 (set /A dd=%dd%-2)
    If %dd% EQU 2 (set /A dd=%dd%-1)
    If %dd% EQU 1 goto FirstDayOfMonth
    rem
    REM less=LSS,LEQ <=,GEQ >=,NEQ Not equal
    REM Could use an else statement eg. Else (set dd=28)
    rem
    rem
    rem test drive
    rem
    xcopy c:\music\test e:\backups /d:%mm%-%dd%-%yy% /f /s /y /z
    if errorlevel 4 goto lowmemory
    if errorlevel 2 goto abort
    rem
    rem
    echo date completed %date% %time%
    echo Backup from date %mm%-%dd%-%yy%
    rem
    if errorlevel 4 goto lowmemory
    if errorlevel 2 goto abort
    if errorlevel 0 goto exit
    rem
    rem
    :FirstDayOfMonth
    IF %mm% EQU 1 (set /A yy=%yy%-1) Else (set /A mm=%mm%-1)
    IF %mm% EQU 1 (set dd=26)
    IF %mm% EQU 1 (set mm=12)
    xcopy c:\music\test e:\backups /d:%mm%-%dd%-%yy% /f /s /y /z
    echo January code %mm%-%dd%-%yy%
    rem
    rem end jan code...
    rem
    if errorlevel 4 goto lowmemory
    if errorlevel 2 goto abort
    rem
    rem
    :lowmemory
    echo Insufficient memory to copy files or
    echo invalid drive or command-line syntax.
    goto exit
    :abort
    echo You pressed CTRL+C to end the copy operation.
    goto exit
    :exit

    ReplyDelete

Translate