• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

copy script to search for source file

I'm trying to copy a Windows backup file (VHD) to a Samba share that is backed up off site weekly.  However- I need to only backup the VHD (not the XML files, directories, etc.) and place the VHD in the root of the share.  I can of course do this manually, but would like to instead schedule a script to run weekly.

The hard part is getting my script to search the WindowsImageBackup\...\ directories for the VHD files before copying.  Is there a copy tool, such as xcopy, that will search subdirectories for the source file, or does anyone have a handy dandy script?
0
Allen
Asked:
Allen
  • 2
2 Solutions
 
Santosh GuptaCommented:
Try this

forfiles /p  "D:\" /s /m *.vhd  /c "cmd /c copy @file E:\backup"
0
 
AllenAuthor Commented:
Thanks for the response.  

I received- ERROR: Invalid argument/option - '@file'

D:\ is the source & E:\backup is the destination, correct?
0
 
Santosh GuptaCommented:
Note: do not use '\' after source and destination name. like C:\temp NOT C:\temp\

yes, D:\ is the source & E:\backup is the destination.
0
 
Bill PrewCommented:
Here's a small BAT script that should do the job, it uses the DIR command to find all VHD files under the base folder you specify, including subfolders, and then processes that list copying each file to the destination backup location.

Save as a BAT file and adjust the SET lines near the top as needed.

@echo off
setlocal

set BaseDir=C:\WindowsImageBackup
set DestDir=Z:\Backup

for /f "tokens=*" %%A in ('dir /b /s /a-d "%BaseDir%\*.vhd"') do (
  copy "%%~A" "%DestDir%"
)

Open in new window

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now