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
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!