1

I'm running Windows and I have a massive group of .pdf files (several thousand) all stored in one large directory, and then all variously organized into a variety of subdirectories within that main directory.

e.g.: K:\OriginalDirectory [main directory] K:\OriginalDirectory\2010 [subdirectory 1] K:\OriginalDirectory\2011 [subdirectory 2] K:\OriginalDirectory\2013 [subdirectory 3]

My problem: I need to copy-and-paste about 1900 of these files into a new directory. The Windows "find" function won't let me do this, as trying to use the "or" operator to combine 1900 unique file names exceeds the 255 character limit for "find."

So I tried the following (based on another StackOverflow answer), using the command line: C:\OriginalDirectory>for %I in (doc1.pdf doc2.pdf doc3.pdf) do copy %I C:\SomeOtherDirectory

This works, but doesn't search the subdirectories. Also, it requires me to type out all 1900 file names, which isn't ideal.

Is there a way for me to complete this task using just the command line that searches all subdirectories within my main directory AND doesn't require me to type out 1900 file names?

kknight
  • 43
  • 1
  • 6
  • Do you want to preserve the directory structure, but skip non-PDF files when copying? Have a look at `robocopy` (it's a tool that comes with recent versions of Windows), it applies filename filters (`/XF` and `/XD`) while copying a directory tree. – Ben Voigt May 31 '17 at 19:06
  • No need to preserve directory structure--I just need to move a huge group of files, all stored randomly across the various subdirectories. All files within this directory/subdirectories are PDFs. Robocopy looks nice, but I don't see a way around having to type out the individual file names (I don't need to move all of the files, just 1900 of the 7000 or so files). – kknight May 31 '17 at 19:18
  • Robocopy documentation: "*File* Specifies the file or files to be copied. **You can use wildcard characters (`*` or `?`)**" – Ben Voigt May 31 '17 at 19:20
  • So whatever rule you have for deciding whether a file should or should not be copied, you use wildcards to include, and `/XF` with another wildcard for things to exclude. – Ben Voigt May 31 '17 at 19:21
  • Alas, every file (both the ones that need to be moved and the ones that don't) in the directory/subdirectories is named like so: 3445604401216.pdf, 3445602510940.pdf, 3445603608022.pdf, and so on. The only rule for deciding to move is whether or not that file name is on the list I have. – kknight May 31 '17 at 19:33
  • Ok, and what format is the list in? A text file with all the names? – Ben Voigt May 31 '17 at 19:43
  • Yes--and I can reformat it if necessary (one column, delimited by commas or tabs, etc.) – kknight May 31 '17 at 19:45
  • Then you want `FOR /F` – Ben Voigt May 31 '17 at 19:46

1 Answers1

0

You can use a batch script which would do the work. Found on: Windows batch copy files from subfolders to one folder

Adjusted for you it should be something like:

pushd C:\OriginalDirectory
   for /r %%a in (*.pdf) do (
     COPY "%%a" "C:\SomeOtherDirectory\%%~nxa"
   )
 popd

edit cause of comment

Copy only those files which are in a list: (Again not sure about the type "C:/my..." because i am currently not able to test it but should work in such a way)

pushd C:\OriginalDirectory
   for /r %%a in (type "C:/myFileList.txt") do (
     COPY "%%a" "C:\SomeOtherDirectory\%%~nxa"
   )
 popd
Markus G.
  • 1,620
  • 2
  • 25
  • 49
  • This looks like it would work if I wanted to move all of the .pdf files, but I need to move just a set of them (1900 out of 7000 or so). The files--both the ones I want to move and the ones I don't--all share similar names (344560440216.pdf, 3445603604366.pdf, etc). Is there a way to "point" a batch script at, say, a list of file names? – kknight May 31 '17 at 19:37
  • Do you mean you want to write a list of about 1900 file names and move only those files in the list? Because I guess, thats the way how you have to do it when the .pdf names aren't differenct of the 1900 you want to move out of the 7000. Am I right? – Markus G. May 31 '17 at 19:46
  • Yes: I have a text file that lists all of the file names for the pdfs that need to be moved. Are you saying to replace "%%a" with my file name? Sorry--I'm just unclear (obviously) on how I should do this. – kknight May 31 '17 at 19:57
  • Edited the solution above – Markus G. May 31 '17 at 19:58
  • I truly appreciate you taking the time to help me. Thank you so much! – kknight May 31 '17 at 20:08