2

I am wondering whether it is possible to write a batch script for CMD.EXE (Target system: MS Windows 2k3) for doing the following:

Let us have folder rootfolder containing a lot of files and directories. Some of the subdirectories (at different levels) might be called dirname. I would like to create a folder rootfolder2, copying the directory tree structure of rootfolder but containing only the folders dirname with their content. I would also like to delete the same folders after having them copied in rootfolder2

Example:

rootfolder
 `- dir1
 `- dir2
     `- filew
     `- dirname
         `- filey
 `- dirname
     `- file1
     `- dirx
         `- file2
 `- filez

And the output I'm looking for would be:

rootfolder
 `- dir1
 `- dir2
     `- filew
 `- filez

rootfolder2
 `- dir2
     `- dirname
         `- filey
 `- dirname
     `- file1
     `- dirx
         `- file2

Can I do this without having to write a console application in C/C++/Java/etc.

Thanks in advance, Joe


Here's the answer to my question using simply xcopy and batch scripting:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set StartDir=Folder1
set BackupDir=Folder1 BK
mkdir "%BackupDir%"
call :ProcessDir "%StartDir%"
exit /b 0
:ProcessDir
    echo Processing directory "%~1"
    for /f "delims=" %%d in ('dir /ad /b "%~1"2^>nul') do (
        if "%%d"=="foldername" ( 
            xcopy /s /e /i "%~1\%%d" "%BackupDir%\%~1\%%d" 
            rmdir /S /Q "%~1\%%d"   
        ) else ( 
            call :ProcessDir "%~1\%%~d"
        )
    )
    exit /b 0

credits go to: recursive renaming file names + folder names with a batch file

Community
  • 1
  • 1
JoeSlav
  • 4,479
  • 4
  • 31
  • 50
  • Have you tried using robocopy? – BugFinder May 24 '11 at 15:26
  • Thanks, I did but with no luck. Modified by message! – JoeSlav May 24 '11 at 15:53
  • Surely thats exactly what the move function of robocopy does though? I must have missed why its not worked. – BugFinder May 24 '11 at 15:55
  • BugFinder, would you mind sharing the flags to achieve it? – JoeSlav May 25 '11 at 12:32
  • robocopy source dest filespec [more filespecs] /move /s .. so something like robocopy rootfolder rootfolder2 dirname/*.* file1 file2 /move /s – BugFinder May 25 '11 at 12:55
  • I'm not sure I understand how the above will work with the above situation - it also gives me an error on the "dirname/*.*" parameter. By the way, I do not know which will be the content of my "dirname" in advance. Thanks, Regards. – JoeSlav May 25 '11 at 13:12
  • But you said you knew the directory name. Hence I added it.. – BugFinder May 25 '11 at 13:15
  • directory name yes but still the error is present when launching the command. It is the directory content that is not known in advance. – JoeSlav May 25 '11 at 14:31
  • You can add your "answer" as a regular answer and recognize it as such, if you want to. – xpda Jan 16 '13 at 03:17

1 Answers1

0

Here's the answer to my question using simply xcopy and batch scripting:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set StartDir=Folder1
set BackupDir=Folder1 BK
mkdir "%BackupDir%"
call :ProcessDir "%StartDir%"
exit /b 0
:ProcessDir
    echo Processing directory "%~1"
    for /f "delims=" %%d in ('dir /ad /b "%~1"2^>nul') do (
        if "%%d"=="foldername" ( 
            xcopy /s /e /i "%~1\%%d" "%BackupDir%\%~1\%%d" 
            rmdir /S /Q "%~1\%%d"   
        ) else ( 
            call :ProcessDir "%~1\%%~d"
        )
    )
    exit /b 0

credits go to: recursive renaming file names + folder names with a batch file

That way it's done!

Community
  • 1
  • 1
Paul
  • 2,620
  • 2
  • 17
  • 27