Script to copy a subset of files to a new drive/directory

KingL
KingL Posts: 1,713 Forumite
I have a .txt file I made long ago which contains the filenames/paths of "special" files, (across several folders and subfolders). Is there a way to use that list to copy (only) these files to a new path/directory?

If so, would someone be so kind as to write me a Windows script/batchfile to do this?!

The folders are nested and have spaces, but no special characters.


In more detail:

If the SUPERSET of files (including the ones I _don't_ want to copy) is :

f:\Source\Folder1\File1.jpg
f:\Source\Folder1\File2.jpg
f:\Source\Folder1\File3.jpg
f:\Source\Folder1\File4.jpg
f:\Source\Folder1\File5.jpg
f:\Source\Folder1\File6.jpg
f:\Source\Folder1\File7.jpg
f:\Source\Folder1\File8.mov
f:\Source\Folder1\File9.jpg
f:\Source\Folder1\File10.jpg
f:\Source\Folder2\File1.jpg
f:\Source\Folder2\File2.jpg
f:\Source\Folder2\File3.jpg
f:\Source\Folder2\File4.jpg
f:\Source\Folder2\File5.jpg
f:\Source\Folder2\File6.jpg
f:\Source\Folder2\File7.jpg
f:\Source\Folder2\File8.jpg
f:\Source\Folder2\File9.jpg
f:\Source\Folder2\File10.jpg
f:\Source\Folder3\File1.jpg
f:\Source\Folder3\File2.jpg
f:\Source\Folder3\File3.jpg
f:\Source\Folder3\File4.jpg
f:\Source\Folder3\File5.jpg
f:\Source\Folder3\File6.jpg
f:\Source\Folder3\File7.jpg
f:\Source\Folder3\File8.jpg
f:\Source\Folder3\File9.mov
f:\Source\Folder3\File10.jpg


My text file contains only the paths of the special (manually selected) subset of files:
f:\Source\Folder1\File3.jpg
f:\Source\Folder1\File8.mov
f:\Source\Folder3\File3.jpg
f:\Source\Folder3\File8.jpg
f:\Source\Folder3\File9.mov


... and I want to copy (only) those files to new drive/folders:
h:\Destination\Folder1\File3.jpg
h:\Destination\Folder1\File8.mov
h:\Destination\Folder3\File3.jpg
h:\Destination\Folder3\File8.jpg
h:\Destination\Folder3\File9.mov


More:
-Currently, nothing whatsoever exists in "h:\Destination\", so the script would first need to create the corresponding empty folders in "h:\Destination\".

If that's a problem, I could first make a new set of empty folders with
robocopy "f:\Source" "h:\Destination" /xf *
to make a superset of empty folders then later (after the copying), delete the unused folders with Remove Empty Directories app.

-There are spaces in the foldernames, but no special characters

- F: and H: are external drives

- Windows 10 Home

My coding/script skills are nada. Could anyone kindly write me a batch file which will do this?

thanks everso
«1

Comments

  • Cornucopia
    Cornucopia Posts: 16,154 Forumite
    First Anniversary Name Dropper First Post Photogenic
    edited 15 July 2018 at 8:23PM
    The easiest way (without coding) to do this kind of thing is to use Excel (or the excellent, free Google Spreadsheet) to create a batch file from your file list. You use formulas to create each line of the script from your list.

    e.g.

    Column A: holds your list
    Column B: holds your destination folder for each entry (could be derived from [A] using a formula
    Column C: holds a formula that takes [A] and and combines them into a copy command. There's a forum limitation on adding script elements to posts, so I've put an image of the example below.

    Then copy Column C into a text file, add a ".BAT" suffix and double click it in File Manager.

    jrrwo1.png
  • KingL
    KingL Posts: 1,713 Forumite
    thanks - I can do the exel bit - it's the (windows) syntax for creating the selected empty folders and copying files (your "Column C: holds a formula... etc") that I don't know.

    .
  • Cornucopia
    Cornucopia Posts: 16,154 Forumite
    First Anniversary Name Dropper First Post Photogenic
    Does my edited example help? The required formula is shown next to where it says "fx".
  • KingL
    KingL Posts: 1,713 Forumite
    if that that statement also creates folders where they don't already exist in the target path, then yes your edited example does help! I'll give it a go :)
  • Cornucopia
    Cornucopia Posts: 16,154 Forumite
    First Anniversary Name Dropper First Post Photogenic
    edited 15 July 2018 at 9:45PM
    No it won't do that as it stands.

    You'd need to either use XCOPY instead of COPY or create a separate script using MD to make the new directories first.

    You can break your pathnames into component parts using REGEX.

    e.g. if B35 contains one of your pathnames "H:\Destination\Folder1\File3.jpg", then the filename is this:
    =REGEXEXTRACT(B35,"\\([^\\]+)$")

    and the folder name is this:
    =REGEXEXTRACT(B35,"^.*\\")

    edit: Apparently, these are for Google Spreadsheet only.

    For Excel, these are the (less elegant) alternatives:

    Filename:
    =mid(A35,find("|",substitute(A35,"\","|",len(A35)-len(SUBSTITUTE(A35,"\",""))))+1,9999)

    Folder:
    =mid(A35,1,find("|",substitute(A35,"\","|",len(A35)-len(SUBSTITUTE(A35,"\","")))))
  • KingL
    KingL Posts: 1,713 Forumite
    edited 15 July 2018 at 11:14PM
    Cornucopia wrote: »
    No it won't do that as it stands.

    You'd need to either use XCOPY instead of COPY


    thanks, executing :
    xcopy "SourceDrive:\Path\filename.jpg" "DestinationDrive:\Path\filename.jpg"
    (etc)

    returns :
    'Does DestinationDrive:\Path\filename.jpg specify a file name or directory name on the target (f/d)'
    for each line.

    I can lean on the f key as it is running (they are all files) as a workaround, and it works. But is there a way of specifying, upfront, that they are all files ?

    thanks

    .
  • Cornucopia
    Cornucopia Posts: 16,154 Forumite
    First Anniversary Name Dropper First Post Photogenic
    Unfortunately not.

    The syntax you are looking for with XCOPY is this:-
    XCOPY "d:\source\folder1\abc.jpg" "e:\destination\folder2\"

    This will copy the file, and create the folder, without prompting.

    So you'll need the formula above to extract the folder part of the path from the full pathname.
  • KingL
    KingL Posts: 1,713 Forumite
    Cornucopia wrote: »
    You can break your pathnames into component parts using REGEX.


    actually, I lied about this part (I thought it would make it easier to explain). Actually, I just have the filenames and I know which folders each batch of files is in, so I am reconstructing the path statements (in excel, using "&") , no need to deconstruct them. I've got this part covered, no worries!
  • KingL
    KingL Posts: 1,713 Forumite
    Cornucopia wrote: »
    This will copy the file, and create the folder, without prompting.

    that's the ticket - works perfectly, thanks a lot :)
  • John_Gray
    John_Gray Posts: 5,821 Forumite
    Name Dropper First Post Photogenic First Anniversary
    It is fairly easy to write a BATch file which reads a text file containing the file paths of the files you want to copy, and copies them to where you want.

    Any file paths which you don't want to copy can be omitted from the text file, or have a semicolon at the front of each path.

    You've already found a solution, so I won't bother with my suggestion.

    I use this mechanism to copy a set of desktop shortcuts to the desktops of a number of user accounts on a number of PCs in a workgroup (for example).
This discussion has been closed.
Meet your Ambassadors

Categories

  • All Categories
  • 343.1K Banking & Borrowing
  • 250.1K Reduce Debt & Boost Income
  • 449.7K Spending & Discounts
  • 235.2K Work, Benefits & Business
  • 607.9K Mortgages, Homes & Bills
  • 173K Life & Family
  • 247.8K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 15.9K Discuss & Feedback
  • 15.1K Coronavirus Support Boards