+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, Script to Copy Files (of a particular type) from 1 location to another? in Coding and Web Development; Can someone help me write a quick script to copy files from 1 location to another? I have files in ...
  1. #1
    Zoom7000's Avatar
    Join Date
    Feb 2006
    Location
    London
    Posts
    992
    Thank Post
    348
    Thanked 93 Times in 62 Posts
    Rep Power
    34

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

    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\<Load of individual folders with different names>\<Mixture of different file types>

    I want to copy all the .doc files from the <Mixture of different file types> 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!

  2. #2
    noxigen's Avatar
    Join Date
    May 2013
    Location
    Nashville, TN USA
    Posts
    5
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    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.

  3. #3
    FishCustard's Avatar
    Join Date
    Feb 2013
    Location
    Croydon
    Posts
    426
    Thank Post
    74
    Thanked 43 Times in 39 Posts
    Rep Power
    19
    Something like...

    Code:
    robocopy <SOURCE> <DEST> *.doc /e
    ...should do it.

  4. #4
    Zoom7000's Avatar
    Join Date
    Feb 2006
    Location
    London
    Posts
    992
    Thank Post
    348
    Thanked 93 Times in 62 Posts
    Rep Power
    34
    Quote Originally Posted by noxigen View Post
    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\<Folders with Random Names>\*.doc

    How do I declare the <Folders with Random Names> in the script?

  5. #5
    old_n07's Avatar
    Join Date
    Jun 2012
    Location
    North Staffordshire
    Posts
    99
    Thank Post
    10
    Thanked 17 Times in 15 Posts
    Rep Power
    8
    This PS script should do it

    Code:
    $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}

  6. #6

    Dos_Box's Avatar
    Join Date
    Jun 2005
    Location
    Preston, Lancashire
    Posts
    9,435
    Thank Post
    701
    Thanked 2,302 Times in 1,063 Posts
    Blog Entries
    23
    Rep Power
    678
    Moving to scripts forum.

  7. #7
    old_n07's Avatar
    Join Date
    Jun 2012
    Location
    North Staffordshire
    Posts
    99
    Thank Post
    10
    Thanked 17 Times in 15 Posts
    Rep Power
    8
    I've edited the code above included a line to check for the destination folder and create if it doesn't exist

    Code:
    $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}

  8. #8

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,249
    Thank Post
    1,061
    Thanked 1,070 Times in 626 Posts
    Rep Power
    741
    Quote Originally Posted by Zoom7000 View Post
    No, that won't work, you've missed out a level in the folder structure:

    C:\Other Files\Misc\<Folders with Random Names>\*.doc

    How do I declare the <Folders with Random Names> 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.



SHARE:
+ Post New Thread

Similar Threads

  1. Scripting help needed to copy files
    By Stuart_C in forum Scripts
    Replies: 8
    Last Post: 3rd August 2010, 03:40 PM
  2. Request for script to copy files
    By TechSupp in forum Scripts
    Replies: 5
    Last Post: 15th July 2010, 02:34 PM
  3. Replies: 5
    Last Post: 17th September 2009, 11:52 PM
  4. Replies: 19
    Last Post: 20th June 2006, 04:53 PM
  5. script to copy files to machines
    By browolf in forum Scripts
    Replies: 1
    Last Post: 28th November 2005, 11:28 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •