1

Since I cannot add a comment, I am asking a related question.

The original posting found here works quite well.

Is there a way to use this with a list of file names? I've seen where a file list can be passed to the ROBOCOPY command but I have been unable to get it to work.

Taking a step back, I have a series of folders and there are specific files inside of them that I want to copy out to a single folder. I have a text file which lists the names of these files.

I am looking for a batch routine that will look for each of the files in the text file in each of the folders and then copy the files out to a new folder.

Thank you!

Community
  • 1
  • 1
ThomK
  • 35
  • 1
  • 4
  • Are all the folders in one directory tree? – foxidrive Jul 16 '14 at 14:11
  • Yes, there is one parent folder and all of the subfolders are in that one folder. I do not believe there are any more levels of folders meaning the subfolders don't have subfolders. – ThomK Jul 16 '14 at 15:04

2 Answers2

0

Test this - the file.txt has a filename on each line.

It doesn't handle filename conflicts.

@echo off
cd /d "c:\base\folder"
for /f "usebackq delims=" %%a in ("file.txt") do (
    for /f "delims=" %%b in ('dir "%%a" /b /s /a-d ') do copy "%%b" "d:\target\folder"
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • That, and by proxy you, are amazing! Thank you for responding so quickly! That works perfectly. Thanks again! – ThomK Jul 16 '14 at 15:50
0

I recently had to tackle this problem, and many files that I wanted to move to from the hierarchy to a single folder had the same name as each other, and I wanted to still flatten the hierarchy without them to being over-written. What I did was write a script that moves the file, but renames it with the old hierarchy path in the name for example: source files:

C:\files\somefiles\file.txt

C:\files\otherfiles\file.txt

destination is C:\newdir\ files are created as

C:\newdir\somefiles-file.txt

C:\newdir\otherfiles-file.txt

here is the code, batch file 1 goes thru the files, batch file 2 renames and moves them (could also copy instead, if you want to preserve the source:

@echo off
for /r %%f in (*.*pr) do @renameandmovefilespart2.bat "%%f" "%%~ff" "%%~xf"

renameandmovefilespart2.bat

@echo off
Setlocal EnableDelayedExpansion
rem set the whole file path
set origWhole=%1
set origPathOnly=%2
set extension=%3
rem here you can set where the directory to hold the flattened hierarchy is
set destDir=c:\destinationDir\
rem  set the directory to do a string replace
rem make this the starting directory, that you dont want in the newly renamed files
set startingDir=C:\starting\directory\
set nothing=
set slash=\
rem here you can set what the character to represent the directory indicator \ in the new files 
set reaplcementDirectoryCharacter=--
set quote="
rem cut out the starting part of the directory
call set newname=%%origWhole:!startingDir!=!nothing!%%
rem replace slashes with new character
call set newname=%%newname:!slash!=!reaplcementDirectoryCharacter!%%
rem remove quotes
call set newname=%%newname:!quote!=!nothing!%%
rem @echo shortened: %newname%
rem @echo source path: %origPathOnly% newPath: %startingDir%
rem @echo extension: %extension%
rem rename the files
ren %origWhole% %newname%
rem prepare to move the file, clean up the source path
call set origPathOnly=%%origPathOnly:!quote!=!nothing!%%
move "%origPathOnly%%newname%" "%destDir%"
Max
  • 454
  • 4
  • 10