使用具有特定命名要求的批处理脚本复制文件

时间:2016-10-25 12:16:29

标签: batch-file cmd

我正在尝试创建一个批处理脚本,它将目录和子目录中的所有文件复制到具有特殊名称的特定位置。

我创建了一个列表,我正在尝试复制文件

List.txt的内容

D:\HOST\subdir1\1111.txt
D:\HOST\subdir2\2222.txt
D:\HOST\subdir3\3333.txt
D:\HOST\subdir3\34rf.pdf
D:\HOST\subdir4\4444.txt
D:\HOST\subdir5\5555.txt
D:\HOST\subdir5\5tg.xls
D:\HOST\subdir5\subdir_55\007.xlsx

目标文件夹应包含以下文件:

subdir1_1111.txt
subdir2_2222.txt
subdir3_3333.txt
subdir3_34rf.pdf
subdir1_1111.txt
.
.
subdir5_007.xlsx

这里是HOST,subdir1到subdir 5和subdir_55都是子目录。

我尝试使用的代码如下:

@echo off & setlocal
cd %~dp0

IF EXIST list.txt del /F list.txt
cd %~dp0\HOST
dir /b /a-d /s *.* >> %~dp0\list.txt
cd..

for /F "tokens=1-4 delims=\" %%a in (%~dp0\list.txt) do copy /b     %~dp0\HOST\%%c\%%d %~dp0\TARGET\%%c_%%d 

以上代码仅在subdir1到subdir5中没有子目录时才有效。我的要求是,如果还有n个子目录,它应该复制文件。另一件事我不知道如何用空格处理子目录名。

我知道它不适用于令牌,因为可以有" n"可能存在的令牌数量" n"主子目录中的子目录数。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

我建议使用for /F和分隔符进行路径操作,而是使用forfor /D变量。

我会这样做:

  • 使用for /R循环遍历源根目录以检索直接子目录的所有名称;
  • 对于每个子目录,通过@echo off setlocal EnableExtensions DisableDelayedExpansion rem // Define constants here: set "_SOURCE=D:\HOST" & rem // (source root directory) set "_TARGET=D:\TARGET" & rem // (target root directory) set "_DIRPATT=*" & rem // (pattern for names of immediate sub-directories) set "_FILPATT=*.*" & rem // (pattern for names of files to copy) set "_SEPAR=_" & rem // (separator character to build new file names) rem // Walk through immediate sub-directories of source root directory: for /D %%D in ("%_SOURCE%\%_DIRPATT%") do ( rem /* Process each immediate sub-directory in a sub-routine; rem `%%~fD` is the full path to the current sub-directory; rem its pure name will be derived in the sub-routine: */ call :PROCESS "%%~fD" "%_FILPATT%" "%_TARGET%" ) endlocal exit /B :PROCESS val_directory val_pattern val_target rem /* Sub-routine to process a single sub-directory. Arguments: rem val_directory [%~1] sub-directory to process; rem val_pattern [%~2] pattern for file names; rem val_target [%~3] target directory; */ rem // Retrieve all files in the sub-directory recursively: for /R "%~1" %%F in ("%~2") do ( rem /* Build target file path; `%~n1` is the base name of the sub-directory, rem `%%~nxF` is the full name of the currently iterated file; rem then check whether the target file already exists: */ if not exist "%~3\%~n1%_SEPAR%%%~nxF" ( rem /* Target file does not exist, so copy the file; rem `%%~fF` is the full path of the currently iterated source file: */ copy /Y "%%~fF" "%~3\%~n1%_SEPAR%%%~nxF" ) ) exit /B 循环递归收集所有文件;
  • 构建每个最终目标文件名;

以下是代码:

if not exist

如果目标文件已存在,则不会被覆盖;如果您确实想要在这种情况下覆盖,只需删除围绕copy命令行的public static UIImage GenerateImage(NSUrl videoUrl) { var asset = AVAsset.FromUrl(videoUrl); var imageGenerator = AVAssetImageGenerator.FromAsset(asset); imageGenerator.AppliesPreferredTrackTransform = true; var actualTime = asset.Duration; CoreMedia.CMTime cmTime = new CoreMedia.CMTime(1, 60); NSError error; var imageRef = imageGenerator.CopyCGImageAtTime(cmTime, out actualTime, out error); if (imageRef == null) return null; var image = UIImage.FromImage(imageRef); return image; } 查询。