Search code examples
for-loopbatch-filedirectory-structurexcopy

Why is xcopy making the subdirs but putting files in .?


I have the following code:

@echo off
cd %temp%
rd /s /q svnbackup >nul 2>&1
md svnbackup\uncomitted_db-pc
cd svnbackup\uncomitted_db-pc
type NUL>files.txt
for /F "tokens=1*" %%T in ('svn status C:\SVN-EDGE') do ( echo %%U>>files.txt && xcopy "%%U" . /E /S /I /F /H /K /X /Y /R )

I get the following output in files.txt:

C:\SVN-EDGE\SomeClient\BUTCHERED.docx 
C:\SVN-EDGE\SomeClient\Development notes.txt 
C:\SVN-EDGE\SomeClient\Received\~$formation for prospective clients.docx 
C:\SVN-EDGE\SomeClient\Sales\Estimates.xlsx 
C:\SVN-EDGE\SomeClient\Sales\Specification.docx 
C:\SVN-EDGE\SomeClient\Sales\~$Estimates.xlsx 
C:\SVN-EDGE\SomeClient\Sales\~$ecification.docx 
C:\SVN-EDGE\SomeClient\removed.docx 
C:\SVN-EDGE\SomeClient\site spec1.docx 
C:\SVN-EDGE\SomeClient\~$TCHERED.docx 
C:\SVN-EDGE\SomeClient\~$emoved.docx 
C:\SVN-EDGE\SomeClient\~$te spec1.docx 

But the thing is, xcopy creates the Received and Development folders inside %temp%\svnbackup\uncomitted_db-pc, but then places the relevant files in the top level of %temp%\svnbackup\uncomitted_db-pc anyway.

At no point are the folders SVN-EDGE or SomeClient created. How can I ensure these folders are created, and that the files fed into xcopy individually listed above are put into the correct subdir?


Solution

  • The XCOPY is behaving as it should. It does not replicate the folder hierarchy explicitly specified in your source path. It only replicates folders found from the wildcard side of things, of which your command has none - you are XCOPYing specific files each time.

    Not fully tested, but I believe you can replace your FOR statement with the following and everything should work as you want.

    for /F "tokens=1*" %%T in ('svn status C:\SVN-EDGE') do (
      echo %%U>>files.txt
      xcopy "%%U" ".\%%~pU" /F /H /K /X /Y /R
    )