locked
using batch to copy files in folders that change their name RRS feed

  • Question

  • We have Backup Exec backing up our VHD's to a backup host once a week.  Each time BE backs up, it changes one folder name in a path.  What can I do to have the new folder name reflected each week in a batch that I use to copy the VHD's to a launching location?  Backup Exec copies to a D: drive, and I use xcopy to copy the VHD's to an E: drive, where they would be launched if needed, and we still have another copy.
    Here is the command for one of the VHD's.  I have 7 of them in the batch file.
    xcopy "D:\Server_Backup\COMM1\IMG000101\c\Virtual Machines\COMM1\Virtual Hard Disks\*.vhd" "E:\vhds\COMM1\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1
    The folder "IMG000101" changes on each backup.
    Thanks for any assistance.
    Friday, October 21, 2011 11:33 PM

Answers

  • Thanks to Pegasus (and AbqBill), here is the answer to my question, "What can I do to have the new folder name reflected each week in a batch that I use to copy the VHD's to a launching location?":

    @echo off
    for /F %%a in ('dir /b /od /ad d:\Server_Backup\Comm1') do set Folder=%%a

     

    Replace the folder that changes its name each week (IMG000101) with %Folder%

    xcopy "D:\Server_Backup\COMM1\%Folder%\c\Virtual Machines\COMM1\Virtual Hard Disks\*.vhd" "E:\vhds\COMM1\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

    A modified version for multiple commands can be found here as suggested by Pegasus also.

     

    • Marked as answer by TerryR. _ Tuesday, October 25, 2011 4:23 PM
    Tuesday, October 25, 2011 4:23 PM

All replies

  • I suspect you wanted to ask the question like so:

    BackupExec creates new folders of the form

    d:\Server_Backup\Comm1\Img000101
    d:\Server_Backup\Comm1\Img000102
    d:\Server_Backup\Comm1\Img000103

    How can I pick up the most recent folder?

    If so then you could use this code:

    @echo off
    for /F %%a in ('dir /b /od /ad d:\Server_Backup\Comm1') do set Folder=%%a

    If not then please rephrase your question.

    Saturday, October 22, 2011 6:06 AM
  • Thank you for the reply.  They are not in sequential order, but yes, the numeric values change.

    How do I implement your for statement into the string I listed?

     

    Saturday, October 22, 2011 6:15 AM
  • Thank you for the reply.  They are not in sequential order, but yes, the numeric values change.

    How do I implement your for statement into the string I listed?

    You replace

    D:\Server_Backup\COMM1\IMG000101
    with
    %Folder%

    About your "not in sequential order" observation: Have a closer look at what the /od switch does in the command I gave you:

    dir /b /od /ad d:\Server_Backup\Comm1


    Saturday, October 22, 2011 8:02 AM
  • Great, thanks.  I have 7 servers to backup, could I do this using one FOR statement, or do I need to have one before each xcopy command?

     

    :: COMM1  C and Exchange

    echo Backing up COMM1 C:

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\Comm1') do set Folder=%%a

    xcopy "%Folder%\c\Virtual Machines\COMM1\Virtual Hard Disks\*.vhd" "E:\vhds\COMM1\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

    echo Backing up Exchange on COMM1 D:

    xcopy "%Folder%\d\Virtual Machines\comm1\Virtual Hard Disks\Exchange*.vhd" "E:\vhds\Exchange\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

     

    The FOR above works for both since the path is the same.  Below are other paths.  It's not any deal to use one for each server, I was just wondering if there was a way for a single statement.

     

    :: DC1

    echo Backing up DC1

    xcopy "D:\Server_Backup\DC1\IMG000097\c\Virtual Machines\DC1\*.vhd" "E:\vhds\DC1\" /O/Y/J 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

     

    :: FILE-01 C and D drives

    echo Backing up FILE-01 C: and D:

    xcopy "D:\Server_Backup\FILE-01\IMG000098\D\Virtual Machines\file-01\Virtual Hard Disks\*.vhd" "E:\vhds\FILE-01\" /O/Y/J 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

     

    :: FPS1

    echo Backing up FPS1 C:

    xcopy "D:\Server_Backup\FPS1\IMG000096\D\Virtual Machines\comm1\Virtual Hard Disks\*.vhd" "E:\vhds\FPS1\" /O/Y/J 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

     

    :: RDS1

    echo Backing up RDS1 C:

    xcopy "D:\Server_Backup\RDS1\IMG000099\c\Virtual Machines\RDS1\*.vhd" "E:\vhds\RDS1\" /O/Y/J 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

     

    :: SQL1

    echo Backing up SQL1 C:

    xcopy "D:\Server_Backup\SQL1\IMG000100\c\Virtual Machines\SQL1\*.vhd" "E:\vhds\SQL1\" /O/Y/J 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

     

     

    echo Date: %date%   End Time: %time%   User: %UserName% 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

     


    • Edited by TerryR. _ Monday, October 24, 2011 7:51 PM
    Monday, October 24, 2011 7:49 PM
  • Your batch file is badly in need of getting a good structure for these reasons:
    - Make its flow visible.
    - Make it easy to debug
    - Make it easy to maintain in the long run.

    This version shows you how you can do it:

    @echo off
    set Source1=d:\Server_Backup\Comm1
    set Source2=D:\Server_Backup\DC1
    etc.
    set Target1=E:\vhds\COMM1\
    set Target2=E:\vhds\DC1\
    etc.
    set Log=E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log
    echo Backup run on %date% at %time% > %Log%

    call :Backup %Source1% %Target1%
    call :Backup %Source2% %Target2%
    goto :eof

    :Backup
    echo Backing up %1 >> %Log%
    for /F %%a in ('dir /b /od /ad %1') do set Folder=%%a
    xcopy "%Folder%\c\Virtual Machines\COMM1\Virtual Hard Disks\*.vhd" %2 /O/Y 1>>%Log% 2>>&1

    Monday, October 24, 2011 9:09 PM
  • Thanks for the suggestion.  Seems I will need multiple :Backup versions (because of the paths) .
    Monday, October 24, 2011 10:37 PM
  • I just tried the below for one server and received a file not found error.

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\DC1') do set Folder=%%a

    xcopy "%Folder%\c\Virtual Machines\DC1\*.vhd" "E:\vhds\DC1\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

     

    Current source of VHD: D:\Server_Backup\DC1\IMG000103\c\Virtual Machines\DC1\

    I tried your version above also and received the same error.

    Monday, October 24, 2011 11:23 PM
  • Hi,

    One good debugging tip for shell scripts (batch files) is to put "echo" before the xcopy command so you can see the exact command line. This way you can determine if the command that's executing is what you think is executing. You need to experiment on your own and do some troubleshooting. We can't see your screen.

    Bill

    Tuesday, October 25, 2011 12:07 AM
  • I just tried the below for one server and received a file not found error.

     

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\DC1') do set Folder=%%a

    xcopy "%Folder%\c\Virtual Machines\DC1\*.vhd" "E:\vhds\DC1\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1


    Expanding a little on AbqBill's suggestion:

    echo xcopy "%Folder%\c\Virtual Machines\DC1\*.vhd" "E:\vhds\DC1\" /O/Y 1 *** E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log
    pause

    Note the *** and the omitted 2>>&1

    Tuesday, October 25, 2011 7:07 AM
  • It's failing as the path begins with the sequential folder.

    xcopy "IMG000103\c\Virtual Machines\DC1\*.vhd" ...

    So I've modified it and it's working:

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\DC1') do set Folder=%%a

    xcopy "d:\Server_Backup\DC1\%Folder%\c\Virtual Machines\DC1\*.vhd" "E:\vhds\DC1\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

     

    • Edited by TerryR. _ Tuesday, October 25, 2011 3:39 PM
    Tuesday, October 25, 2011 3:25 PM
  • I just tried the below for one server and received a file not found error.

     

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\DC1') do set Folder=%%a

    xcopy "%Folder%\c\Virtual Machines\DC1\*.vhd" "E:\vhds\DC1\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1


    Expanding a little on AbqBill's suggestion:

    echo xcopy "%Folder%\c\Virtual Machines\DC1\*.vhd" "E:\vhds\DC1\" /O/Y 1 *** E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log
    pause

    Note the *** and the omitted 2>>&1

    This is the existing batch with the mods:

    echo off

    :: Backup VHD data from D to E on VHOST2

    set Log=E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log

    echo Date: %date%   Start Time: %time%   User: %UserName% >%Log%

    :: COMM1  C and Exchange

    echo Backing up COMM1 C: >> %Log%

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\comm1') do set Folder=%%a

    xcopy "d:\Server_Backup\comm1\%Folder%\c\Virtual Machines\COMM1\Virtual Hard Disks\*.vhd" "E:\vhds\COMM1\" /O/Y 1>>%Log% 2>>&1

     

    echo Backing up Exchange on COMM1 D: >> %Log%

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\comm1') do set Folder=%%a

    xcopy "d:\Server_Backup\comm1\%Folder%\d\Virtual Machines\comm1\Virtual Hard Disks\Exchange*.vhd" "E:\vhds\Exchange\" /O/Y 1>>%Log% 2>>&1

     

    :: DC1

    echo Backing up DC1 >> %Log%

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\dc1') do set Folder=%%a

    xcopy "d:\Server_Backup\dc1\%Folder%\c\Virtual Machines\DC1\*.vhd" "E:\vhds\DC1\" /O/Y/J 1>> %Log% 2>>&1

     

    :: FILE-01 C and D drives

    echo Backing up FILE-01 C: and D: >> %Log%

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\file-01') do set Folder=%%a

    xcopy "d:\Server_Backup\file-01\%Folder%\D\Virtual Machines\file-01\Virtual Hard Disks\*.vhd" "E:\vhds\FILE-01\" /O/Y/J 1>>%Log% 2>>&1

     

    :: FPS1  

    echo Backing up FPS1 C: >> %Log%

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\fps1') do set Folder=%%a

    xcopy "d:\Server_Backup\fps1\%Folder%\D\Virtual Machines\comm1\Virtual Hard Disks\*.vhd" "E:\vhds\FPS1\" /O/Y/J 1>>%Log% 2>>&1

     

    :: RDS1

    echo Backing up RDS1 C: >> %Log%

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\rds1') do set Folder=%%a

    xcopy "d:\Server_Backup\rds1\%Folder%\c\Virtual Machines\RDS1\*.vhd" "E:\vhds\RDS1\" /O/Y/J 1>>%Log% 2>>&1

     

    :: SQL1

    echo Backing up SQL1 C: >> %Log%

    for /F %%a in ('dir /b /od /ad d:\Server_Backup\sql1') do set Folder=%%a

    xcopy "d:\Server_Backup\sql1\%Folder%\c\Virtual Machines\SQL1\*.vhd" "E:\vhds\SQL1\" /O/Y/J 1>>%Log% 2>>&1

     

    echo Date: %date%   End Time: %time%   User: %UserName% 1>>%Log% 2>>&1

    start "" notepad %Log%

     

     

    Here is your suggestion with the change:

    echo off

    :: Backup VHD data from D to E on VHOST2

    set Source1=d:\Server_Backup\comm1

    set Source2=d:\Server_Backup\dc1

    set Source3=d:\Server_Backup\file-01

    set Source4=d:\Server_Backup\fps1

    set Source5=d:\Server_Backup\rds1

    set Source6=d:\Server_Backup\sql1

     

    set Target1=e:\vhds\comm1\

    set Target2=e:\vhds\dc1\

    set Target3=e:\vhds\file-01\

    set Target4=e:\vhds\fps1\

    set Target5=e:\vhds\rds1\

    set Target6=e:\vhds\sql1\

    set Target7=e:\vhds\exchange\

     

    set Log=E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%-2.log

    echo Date: %date%   Start Time: %time%   User: %UserName% > %Log%

    call :COMM1    %Source1% %Target1%

    call :DC1      %Source2% %Target2%

    call :FILE-01  %Source3% %Target3%

    call :FPS1     %Source4% %Target4%

    call :RDS1     %Source5% %Target5%

    call :SQL1     %Source6% %Target6%

    call :EXCHANGE %Source1% %Target7%

    goto :eof

     

    :COMM1

    echo Backing up %1 >> %Log%

    for /F %%a in ('dir /b /od /ad %1') do set Folder=%%a

    xcopy "%1\%Folder%\c\Virtual Machines\COMM1\Virtual Hard Disks\*.vhd" %2 /O/Y 1>>%Log% 2>>&1

     

    :EXCHANGE

    echo Backing up EXCHANGE >> %Log%

    for /F %%a in ('dir /b /od /ad %1') do set Folder=%%a

    xcopy "%1\%Folder%\d\Virtual Machines\comm1\Virtual Hard Disks\Exchange*.vhd" %2 /O/Y 1>>%Log% 2>>&1

     

    :DC1

    echo Backing up %1 >> %Log%

    for /F %%a in ('dir /b /od /ad %1') do set Folder=%%a

    xcopy "%1\%Folder%\c\Virtual Machines\DC1\*.vhd" %2 /O/Y/J 1>>%Log% 2>>&1

     

    :FILE-01

    echo Backing up %1 >> %Log%

    for /F %%a in ('dir /b /od /ad %1') do set Folder=%%a

    xcopy "%1\%Folder%\D\Virtual Machines\file-01\Virtual Hard Disks\*.vhd"\*.vhd" %2 /O/Y/J 1>>%Log% 2>>&1

     

    :FPS1

    echo Backing up %1 >> %Log%

    for /F %%a in ('dir /b /od /ad %1') do set Folder=%%a

    xcopy "%1\%Folder%\D\Virtual Machines\comm1\Virtual Hard Disks\*.vhd" %2 /O/Y/J 1>>%Log% 2>>&1

     

    :RDS1

    echo Backing up %1 >> %Log%

    for /F %%a in ('dir /b /od /ad %1') do set Folder=%%a

    xcopy "%1\%Folder%\c\Virtual Machines\RDS1\*.vhd" %2 /O/Y/J 1>>%Log% 2>>&1

     

    :SQL1

    echo Backing up %1 >> %Log%

    for /F %%a in ('dir /b /od /ad %1') do set Folder=%%a

    xcopy "%1\%Folder%\c\Virtual Machines\SQL1\*.vhd" %2 /O/Y/J 1>>%Log% 2>>&1

     

    echo Date: %date%   End Time: %time%   User: %UserName% 1>>%Log% 2>>&1

    start "" notepad %Log%

     

    • Edited by TerryR. _ Tuesday, October 25, 2011 3:56 PM
    Tuesday, October 25, 2011 3:50 PM
  • Glad you've got it sorted out, finding out at the same time how to debug batch files. You should now mark Bill's reply and mine as "Answer".
    Tuesday, October 25, 2011 3:50 PM
  • Thanks to Pegasus (and AbqBill), here is the answer to my question, "What can I do to have the new folder name reflected each week in a batch that I use to copy the VHD's to a launching location?":

    @echo off
    for /F %%a in ('dir /b /od /ad d:\Server_Backup\Comm1') do set Folder=%%a

     

    Replace the folder that changes its name each week (IMG000101) with %Folder%

    xcopy "D:\Server_Backup\COMM1\%Folder%\c\Virtual Machines\COMM1\Virtual Hard Disks\*.vhd" "E:\vhds\COMM1\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

    A modified version for multiple commands can be found here as suggested by Pegasus also.

     

    • Marked as answer by TerryR. _ Tuesday, October 25, 2011 4:23 PM
    Tuesday, October 25, 2011 4:23 PM
  • Replace the folder that changes its name each week (IMG000101) with %Folder%

    xcopy "D:\Server_Backup\COMM1\%Folder%\c\Virtual Machines\COMM1\Virtual Hard Disks\*.vhd" "E:\vhds\COMM1\" /O/Y 1>>E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log 2>>&1

    A modified version for multiple commands can be found here as suggested by Pegasus also.

    Hmm . . . not quite. The idea is to mark the respondents' posts that answered your question, not your own summary post at the end.
    Tuesday, October 25, 2011 4:53 PM
  • Marking the posts you suggest would not provide the answer in entirety. Your post stated to replace:

    D:\Server_Backup\COMM1\IMG000101 with %Folder% , which did not work.

    AbqBill said, "put "echo" before the xcopy command so you can see the exact command line".  Again, this is not an answer, but a troubleshooting step.  These suggestions led me to the answer.

    So by combining everything together and giving the credit where due, the answer is complete.  I'm not taking any credit for it, but if the "system" only gives points in that manner, it's flawed.

    Tuesday, October 25, 2011 5:54 PM

  • Your batch file is badly in need of getting a good structure for these reasons:
    - Make its flow visible.
    - Make it easy to debug
    - Make it easy to maintain in the long run.

    This version shows you how you can do it:

    @echo off
    set Source1=d:\Server_Backup\Comm1
    set Source2=D:\Server_Backup\DC1
    etc.
    set Target1=E:\vhds\COMM1\
    set Target2=E:\vhds\DC1\
    etc.
    set Log=E:\backup-logs\%date:~4,2%-%date:~7,2%-%date:~12,2%.log
    echo Backup run on %date% at %time% > %Log%

    call :Backup %Source1% %Target1%
    call :Backup %Source2% %Target2%
    goto :eof

    :Backup
    echo Backing up %1 >> %Log%
    for /F %%a in ('dir /b /od /ad %1') do set Folder=%%a
    xcopy "%Folder%\c\Virtual Machines\COMM1\Virtual Hard Disks\*.vhd" %2 /O/Y 1>>%Log% 2>>&1

    I tried this suggestion yesterday and it failed.  I found that without a "goto" returning the script each time to the next "call", the first call statement continued on using the same variables, which caused all other xcopy commands to fail.
    Wednesday, October 26, 2011 2:06 PM
  • I tried this suggestion yesterday and it failed.  I found that without a "goto" returning the script each time to the next "call", the first call statement continued on using the same variables, which caused all other xcopy commands to fail.

    You need to apply Bill's debugging method to find out what's going on. Hint: The line

    xcopy "%Folder%...
    should probably read
    xcopy "%1...

    Wednesday, October 26, 2011 5:02 PM
  • I was referring to what I posted on the 25th, regarding your suggestion and how I used it.  Those are correct.  

    I found out what the problem was.  There was no "goto :eof" at the end of each subroutine.  

    Thanks for your time and your assistance.

    Friday, October 28, 2011 5:25 PM
  • I found out what the problem was.  There was no "goto :eof" at the end of each subroutine.  
    Since you did not post the code that misbehaved, we would have required a crystal ball to figure out the cause of this particular problem. I often use it but right it's getting a cut & polish, hence I need to rely on posters including their code.
    Friday, October 28, 2011 6:49 PM
  • I found out what the problem was.  There was no "goto :eof" at the end of each subroutine.  
    Since you did not post the code that misbehaved, we would have required a crystal ball to figure out the cause of this particular problem. I often use it but right it's getting a cut & polish, hence I need to rely on posters including their code.
    If you read my first sentence (what you snipped), you will see that the code was posted, on the 25th.
    Friday, October 28, 2011 8:39 PM
  • If you read my first sentence (what you snipped), you will see that the code was posted, on the 25th.
    You assume that I live in the same time zone as you do. I don't and the calendar dates I see are different from the ones you see, which prevents me from checking your code. Anyway, what I tried to suggest is this: Always post the code you have a problem with. I have seen countless posts in which the OP claimed that the replies he received failed to work. When asked to post the problem code it turned out . . . well, you know the end of the story.
    Friday, October 28, 2011 9:27 PM