Copying a set of specific files from a directory to another directory while retaining folder structure

StackOverflow https://stackoverflow.com/questions/23221996

Question

I have a directory which looks like this:

\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\scannedDocs\2004\2009\jan\file2.pdf
\isa\documents\2005\2008\jac\file1.txt
\isa\documents\2003\jan\file1.txt
\isa\scannedDocs\2002\2009\jan\file2.pdf

I have a list of files I need copied (exported from a database), but because I don't need EVERY file in the directory, I only want the ones copied from my list:

Files-needed.txt:

\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\documents\2004\jac\file3.txt
\isa\documents\2003\jan\file1.txt

Basically:

  1. I'll need to copy out the specific files needed, and
  2. Log when a file can't be found, and,
  3. I need folder structure retained, as the files might have the same name, just in different directories.

It's on a windows 7 machine, and I can run PowerShell and batch files. I tried robocopy and xcopy and either got all of the directories and no files or all files and no directories...

Any assistance would be great.

EDITS:

Okay, so i have tried Robocopy, but it is either copying the directories and no files or files and no directories. I haven't tried anything in powershell yet, but that might be the way...

It is Windows, I might have just written the slashes incorrectly above, I work between both environments, and was just trying to explain the problem.

Things I tried:

@echo off
set src_folder="C:\batchScripting\TEST_DIR\"
set dst_folder="C:\batchScripting\COPY2_DIR\"
robocopy "C:\batchScripting\TEST_DIR" "C:\batchScripting\COPY2_DIR" FileList.txt /S /V /NP /LOG:"log.log" /R:10 /W:30 

and

 @echo off
 set src_folder=C:\batchScripting\TEST_DIR\
 set dst_folder=C:\batchScripting\COPY2_DIR\
 for /f "tokens=*" %%i in (File-list.txt) DO xcopy /s /i "%src_folder%\%%i" "%dst_folder%"
Was it helpful?

Solution

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (q23221996.txt) DO (
 IF EXIST "%sourcedir%%%a" (ECHO f|xcopy /y "%sourcedir%%%a" "%destdir%%%a" 2>NUL >nul
 ) ELSE (ECHO "%sourcedir%%%a" does NOT exist)
)

GOTO :EOF

I used a file named q23221996.txt containing your data for my testing. sourcedir and destdir are both set up to suit my system.

The /y on the xcopy command forces overwrite if the destination file already exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top