我想知道是否可以为CMD.EXE
(目标系统:MS Windows 2k3)编写批处理脚本来执行以下操作:
让我们拥有包含大量文件和目录的文件夹rootfolder
。某些子目录(在不同级别)可能被称为dirname
。我想创建一个文件夹rootfolder2
,复制rootfolder
的目录树结构,但只包含文件夹dirname
及其内容。我还希望在rootfolder2
示例:
rootfolder
`- dir1
`- dir2
`- filew
`- dirname
`- filey
`- dirname
`- file1
`- dirx
`- file2
`- filez
我正在寻找的输出是:
rootfolder
`- dir1
`- dir2
`- filew
`- filez
rootfolder2
`- dir2
`- dirname
`- filey
`- dirname
`- file1
`- dirx
`- file2
我是否可以在不用C / C ++ / Java /等编写控制台应用程序的情况下执行此操作。
提前致谢, 乔
以下是使用简单xcopy和批处理脚本的问题的答案:
@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
积分转到:recursive renaming file names + folder names with a batch file
答案 0 :(得分:0)
以下是使用简单的xcopy和批处理我的问题的答案 脚本:
@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
学分去: recursive renaming file names + folder names with a batch file
就这样完成了!