Is there a way to select/copy/isolate files from a list of file names?
November 12, 2017 9:32 AM   Subscribe

I have a folder of files (A, B, C, D, E, F, G, H...) and a list which represents a subset of those files (B, C, F, H...). Is there a way for me to use the list (excel, .csv) to automatically select these 300 or so files from the complete set? Ultimately I need to send the files from the list files (and ONLY these files from the list) to someone else. I can use either Mac or PC solutions here.
posted by brbmaroon to Computers & Internet (8 answers total) 1 user marked this as a favorite
 
There are several solutions on this page that should work in the command shell on your Mac. You'll just need to include path names as part of your list since there aren't all at the same path.
posted by paper chromatographologist at 9:46 AM on November 12, 2017


Well, in Windows, the command to copy a file is basically:

copy c:\sourcedir\file1.doc d:\destdir\file2.doc

If you have a spreadsheet of filenames in Excel, you can use an Excel function to generate a column containing the above commands as text.

The formula to concatenate the strings would look something like:

= "COPY c:\sourcedir\""" & A1 & """ d:\destdir\" & A1 & """"

where A1 contains the simple filename.

Copy and paste your resulting column of copy commands to a plain text file (one command per line), then change the extension of that file to .bat, and you can run the resultant batch file to execute all of the copy commands.

There are probably easier ways involving PowerShell or something.
posted by pipeski at 9:51 AM on November 12, 2017 [2 favorites]


Ok, so, if I had to do this, I would do what pipeski suggests. If you're already in excel, might as well use excel.

But I think this will also work, on mac:
cat files.csv | xargs -I_ cp c:\sourcedir\_ d:\destdir\_
If you have commas in the csv, insert a find-replace like so:
cat files.csv | sed "s/,//g" | xargs -I_ cp c:\sourcedir\_ d:\destdir\_
My imprecise explanations:

cat -- spew out the file list, line by line
sed -- edit each line, searching for , replacing with '', globally
xargs -I_ -- for each line, form and execute the following command, using _ as substitute for the contents of each line
posted by batter_my_heart at 10:12 AM on November 12, 2017 [1 favorite]


Similar to above solutions, I'd go about it this way....

export the column of filenames from excel as a text file.
open a terminal cd to the directory where the files live.
use zip to create an archive of the files you want using the list of files.
zip files.zip . -i@list-of-filenames.txt
send the zip file to whomever wants them.

The '-i@list-of-filenames.txt' tells the `zip` program to only include the files from 'list-of-files.txt' when creating the 'files.zip' archive.
posted by zengargoyle at 10:43 AM on November 12, 2017 [1 favorite]


On Windows, you'll want to use "for /f" as detailed here or here.
posted by cnc at 12:52 PM on November 12, 2017


I believe that powershell is the nicer of the shell languages available to do this type of heavy lifting.
roughly you can match up the two lists based on your file names then loop through and use copy-item

Import-CSV "listOfFiles.csv"
$listOfFiles = Get-ChildItem -path C:\Files\stuff-Recurse -Name
Copy-Item "C:\Files\stuff\originalitem" -Destination "C:\newplace"
posted by andendau at 8:27 PM on November 12, 2017


A tidy workflow for this kind of thing can be achieved if you keep your .csv lists, the pool of files you want to export from, and your exporter script all inside the same folder; then, to do an export, you drag one of the .csv lists and drop it on the exporter script, which without further ado creates a folder or .zip file containing only those files from the pool that were named in the list.

If you'd like me to write and test a Windows script that works this way, paste a sample .csv into a reply here and I'll happily do that.
posted by flabdablet at 7:42 AM on November 13, 2017


Just a note: if you're trying something like this using a shell pipeline, ALWAYS write the commands into a file, inspect the file, then use "sh file" to execute the commands. Otherwise, you run the risk of some ill-formed-but-still-valid commands erasing your files or some other rottenness. For batter_my_heart's example, just append "> tmp" to write the commands to the file named "tmp" and then after you're satisified they look correct, "sh ./tmp". For what it is worth, my brief inspection suggests they're correct, but I've been wrong a heartbreakingly large proportion of the time in the past...

I cannot comment on Micro$oft's "power shell," except to say that I looked at it once and felt my innards curdling. I'm sure it's just great, though. After all, they've had thirty or forty years to improve on the Unix shells...
posted by Gilgamesh's Chauffeur at 8:07 PM on November 18, 2017


« Older Where should I go to buy a diamond engagement ring...   |   Should I take vitamins? Newer »
This thread is closed to new comments.