MS DOS command ...

Discussion in 'Programmer's Corner' started by Anthony Quah, Oct 17, 2009.

  1. Anthony Quah

    Thread Starter Active Member

    Dec 1, 2007
    80
    0
    hi guy,

    I am working on some dos command to get some folder copy to other location at specific time on daily basic...

    My DOS source code
    start at 21:44 /every:M,T,W,Th,F cmd /c xcopy C:\123 C:\DeakinU /E


    My problem:
    I need to get it copy over to new location in a new files name without overwrite the current folder. If tuesday it copy at 21:44, then it will create new folden without overwrite the monday exist folder. How can I do that? If batch files can be use can i get some example for further trial out ?


    I thank in advance for any help given...:)
     
  2. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,694
    904
    Can you just capture the date and copy to a file of the same name? Use yymmdd format.

    John
     
  3. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    How many versions do you want to keep?
     
  4. Anthony Quah

    Thread Starter Active Member

    Dec 1, 2007
    80
    0
    jp,
    if i put ddmmyy would me I have to set the date ...rather than let it copy by itself .. you see my DOS already set copy at specific time daily ..my problem is i dont want it to overwrite the previous folder..

    SgtWookie,
    I just need it to have 3 version of folder without overwrite each other..
     
  5. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Well, this is overkill ... 10 versions. I suggest having less than 2 weeks' worth of backups is not sufficient. Even just two weeks worth is minimal.

    Backup copies SHOULD be stored on a separate drive, but the following is just copying from/to the local C: drive.

    Your scheduler command would look something like this:
    at 21:44 /every:M,T,W,Th,F cmd /c c:\123back.bat

    Here's an example .BAT file:
    @ filename: C:\123back.bat
    @-----------------code begins---------------------
    @echo off
    c:
    cd \
    if exist 123backup goto :backexist
    md 123backup
    :backexist
    cd 123backup

    if not exist 10 goto :9
    del 10 /q
    rd 10

    :9
    if not exist 9 goto :8
    ren 9 10

    :8
    if not exist 8 goto :7
    ren 8 9

    :7
    if not exist 7 goto :6
    ren 7 8

    :6
    if not exist 6 goto :5
    ren 6 7

    :5
    if not exist 5 goto :4
    ren 5 6

    :4
    if not exist 4 goto :3
    ren 4 5

    :3
    if not exist 3 goto :2
    ren 3 4

    :2
    if not exist 2 goto :1
    ren 2 3

    :1
    if not exist 1 goto :0
    ren 1 2

    :0
    md 1
    xcopy c:\123 1 /e /i

    :exit
    cd \
    EXIT
    @-----------------code ends---------------------

    You'll wind up with a subdirectory called "123backup" on the C: drive.
    It will have 10 subdirectories, numbered 1 through 10.
    directory 1 will always have the most current backup; directory 10 the oldest.
    Determine the backup copy date & time by the directory creation date & time.
     
    Last edited: Oct 17, 2009
  6. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    A more generalized approach
    AT command:
    AT 21:44 /every:M,T,W,Th,F cmd /c C:\backit.bat C: 123 C: DeakinU
    All files in c:\123 will be copied to C:\backup\DeakinU\01 on the days specified.
    There will eventually be 10 subdirectories under C:\backup\DeakinU.

    @rem ---file: backit.bat --------------------------------------------------
    @echo off
    if \%1\==\\ goto :err
    if \%2\==\\ goto :err
    if \%3\==\\ goto :err
    if \%4\==\\ goto :err
    goto :noerr
    :err
    echo "%0 %1 %2 %3 %4" is not correct. Exactly 4 parameters are required.
    echo Syntax:
    echo "%0 source_drive: source_dir dest_drive: dest_dir"
    echo Copies will be placed in the dest_drive:\backup\dest_dir directory
    goto :exit
    :noerr
    %1
    cd \
    %3
    if exist backup goto :backexist
    md backup
    :backexist
    cd backup
    if exist %4 goto :back2exist
    md %4
    :back2exist
    cd %4

    if not exist 10 goto :09
    rd 10 /s /q

    :09
    ren 09 10
    ren 08 09
    ren 07 08
    ren 06 07
    ren 05 06
    ren 04 05
    ren 03 04
    ren 02 03
    ren 01 02
    md 01
    xcopy %1\%2 01 /c /e /i /q /h

    :exit
    %1
    cd \
    exit
    @rem --- end --------------------------------------------------
     
    Last edited: Oct 18, 2009
  7. Anthony Quah

    Thread Starter Active Member

    Dec 1, 2007
    80
    0
    oh man, thank for the help..i going to try it out...
     
  8. Anthony Quah

    Thread Starter Active Member

    Dec 1, 2007
    80
    0
    Sgt Wookie,

    I just got the program work, but I need advice how to delete the oldest folder after it reach folder "test3" because i don't want it to populate our the disk space. Need some help on this...




    @ filename: C:\123back.bat
    @-----------------code begins---------------------
    @echo off
    c:
    cd \
    at 15:45 /every:M,T,W,Th,F cmd/c c:\123back.bat
    if exist 123backup goto :backexist
    md 123backup
    :backexist
    cd 123backup


    if not exist c:\123backup\original goto :1
    ren c:\123backup\original test1

    :1
    if not exist c:\123backup\test1 goto :2
    ren c:\123backup\test1 test2


    :2
    if not exist c:\123backup\test2 goto :3
    ren c:\123backup\test2 test3

    :3
    if not exist c:\123backup\test2 goto :0
    ren c:\123backup\test2 test3


    :0
    xcopy c:\EC_logs c:\123backup\original /e /i /y

    :exit
    cd \
    EXIT
    @-----------------code ends---------------------
     
  9. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    You're doing it backwards.
    You're renaming original to test1, and then right away renaming test1 to test2, and then renaming test2 to test3.

    The first time you run it, you'd wind up with just an "original" subdirectory.
    The next time you ran it, original would be renamed to test1, then test2, then test3, and a new "original" directory would be created.
    The next time you ran it, "original would be renamed to test1, then test2, but the rename to test3 would fail because test3 already exists.

    You need to deal with the oldest backup directory, test3, first - by deleting it.
    Then rename test2 to test3, if test2 exists.
    Then rename test1 to test2, if test1 exists. Etc.

    AT 15:45 /every:M,T,W,Th,F cmd /c c:\123back.bat
    The AT command should not be in the batch file.
    Here's a shorter version:

    @REM filename: C:\123back.bat
    @REM -----------------code begins---------------------
    @echo off
    c:
    cd \
    if not exist 123backup md 123backup
    cd 123backup
    if exist test3 rmdir test3 /s /q
    if exist test2 ren test2 test3
    if exist test1 ren test1 test2
    if exist original ren original test1
    xcopy c:\EC_logs c:\123backup\original /e /i /y
    cd \
    EXIT
    @REM-----------------code ends---------------------

    This way, the oldest directory, test3, is removed first each time the batch file is executed, and the remaining testn directories get renamed, along with "original" to test1.

    Then the "original" is created from the current C:\EC_logs directory.
     
    Last edited: Oct 26, 2009
Loading...