explorer
explorer

Reputation: 56

windows batch command to move all wanted sub-folders recrusively in a directory to a NEW folder

I have a local Folder-A which have many subfolders (svn-repository), these subfolders both have a subfolder named .svn. Now I want to move all .svn folders to Folder-B, but incase of oneday I wants the .svn folders back to their original place so I can SVN update them, so the ".svn" folder's parent folder info must be exits in Folder-B

starts like this:

Folder-A
 |_DR1
   |_.svn
   |_s1
      |_.svn
      |_s1_s1folder
        |_.svn
        |_file1
      |_file1  
   |_s2
      |_.svn
      |_file1
      |_file2
 |_DR2 ... etc

Desired MOVE result:

Folder-A only have actual sub-folders and data but without .svn folder

Folder-B
 |_DR1
   |_.svn
   |_s1
      |_s1_s1folder
        |_.svn
      |_.svn
   |_s2
      |_.svn
 |_DR2 ... etc

Desired RESTORE result:

Folder-A & Folder-B back to where we starts.

please help write commandlines to accomplish the MOVE and RESTORE mission, thank you

Upvotes: 0

Views: 1114

Answers (1)

Magoo
Magoo

Reputation: 80203

@ECHO OFF
SETLOCAL
:: establish source and destination directorynames; ensure dest exists
SET source=c:\folder-a
SET dest=u:\folder-b
MD %dest% 2>NUL
:: delete a tempfile if it exists
DEL "%temp%\svntmp2.tmp" 2>nul
:: create a dummy directory
MD c:\dummy
:: go to root of tree containing SVN directories
PUSHD "%source%" 
XCOPY /L /s . c:\dummy |find /i "\svn\" >"%temp%\svntmp1.tmp"
POPD
:: goto destination directory
PUSHD "%dest%"
FOR /f "delims=" %%i IN ('type "%temp%\svntmp1.tmp"') DO CALL :moveit "%%i"
FOR /f "delims=" %%i IN (
 'TYPE "%temp%\svntmp2.tmp"^|sort /r'
 ) DO RD "%%i" /S /Q
POPD

:: delete tempfiles 
del "%temp%\svntmp1.tmp"
del "%temp%\svntmp2.tmp"

:: delete the dummy directory
RD /s /q c:\dummy

GOTO :eof

:moveit
:: get filename, remove quotes, then remove leading '.'
SET sourcefile=%~1
SET sourcefile=%sourcefile:~1%
FOR %%i IN ("%sourcefile%") DO (
MD ".%%~pi" 2>nul
MOVE "%source%%%~i" ".%%~pnxi"
>>"%temp%\svntmp2.tmp" ECHO %source%%%~pi
)

GOTO :eof
  • Set your source and destination directories.
  • Make a dummy empty directory
  • go to your source and generate a list of the files to be moved using XCOPY/L and filtering for lines containing \svn\
  • go to your destination directory
  • move each file
    • remove the leading '.' from the XCOPY output
    • create the destination subdirectory; ignore 'it already exists' errors
    • move the file
    • record the directory name in a second tempfile
  • Sort the second tempfile in reverse, which puts the deepest subdirectories first
  • remove the directories, ignoring 'it is not there' errors
  • delete the tempfiles and dummy directory.

Note that the commands to actually DO the moves/directory creation/deletion are merely ECHOed. This is so that you can verify that the routine does what you want it to do. You would need to verify that it's correct, then remove the ECHO keyword from the statements to actually ACTIVATE the move. Test against a small dummy area first for safety's sake.

As for restoring - it's relatively easy.

xcopy /s/e/v "...folder-b\dr1\svn\" "\folder-a\dr1\svn\"

should do what you want, keeping the restored projects files in folder-b (delete if you like - it's your system) and you can restore on a project-by-project basis.

(I've used SVN throughout because the dot is getting a little small for me nowadays...)

Upvotes: 1

Related Questions