Jump to content

Script to Copy Files (of a particular type) from 1 location to another?


Recommended Posts

Posted

Can someone help me write a quick script to copy files from 1 location to another?

 

I have files in this folder structure:

 

C:\Other Files\Misc\\

 

I want to copy all the .doc files from the folder to the following location:

 

E:\Other Files\Misc

Can someone help me with a script that will do it for me? Thanks in advance!

Posted

You could create a simple batch file to do this. Just save the following command to CopyFiles.bat (or whatever you want to call it):

xcopy /s "C:\Other Files\Misc\*.doc " "E:\Other Files\Misc\*.*"

 

This will copy all .doc files and retain the original folder structure as well.

Posted
You could create a simple batch file to do this. Just save the following command to CopyFiles.bat (or whatever you want to call it):

xcopy /s "C:\Other Files\Misc\*.doc " "E:\Other Files\Misc\*.*"

 

This will copy all .doc files and retain the original folder structure as well.

 

No, that won't work, you've missed out a level in the folder structure:

 

C:\Other Files\Misc\\*.doc

 

How do I declare the in the script?

Posted

This PS script should do it

 


$source = "C:\Other Files\Misc" #source directory
$dest = "E:\Other Files\Misc" #destination directory (must exist)

#search source directory and subfolders for .doc files
$files = get-childitem $source -recurse -Include *.doc | select-object DirectoryName,Name


#copy files to destination
foreach ($file in $files) { $item = $file.Directoryname + "\" + $file.name
                           Copy-Item $item -Destination $dest}

Posted

I've edited the code above included a line to check for the destination folder and create if it doesn't exist

 


$source = "C:\Other Files\Misc" #source directory
$dest = "E:\Other Files\Misc" #destination directory

#search source directory and subfolders for .doc files
$files = get-childitem $source -recurse -Include *.doc | select-object DirectoryName,Name

#check for destination folder and create if doesn't exist
If(!(Test-Path $dest)) {new-item $dest -type directory}

#copy files to destination
foreach ($file in $files) { $item = $file.Directoryname + "\" + $file.name
                           Copy-Item $item -Destination $dest}

Posted
No, that won't work, you've missed out a level in the folder structure:

 

C:\Other Files\Misc\\*.doc

 

How do I declare the in the script?

 

Errrrrrrr *.* /s /e /h ?

If it's just a one off, back it up using some backup software and then restore to the new location.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...