[英]batch file to copy only one of filetype for each subfolder to another folder
here is my code: 这是我的代码:
xcopy "C:\Users\Me\Desktop\data\*.mtl" "C:\Users\Me\Desktop\mtl" /s /e
What it does is copy all the files of filetype .mtl from the subfolders under the data folder, and copy it to the mtl folder in the desktop while retaining its folder structure at the same time. 它的作用是从数据文件夹下的子文件夹复制所有文件类型.mtl的文件,然后将其复制到桌面的mtl文件夹中,同时保留其文件夹结构。
Which is good, but my goal is to limit the file selection for each subfolder to only one of the filetype .mtl. 这很好,但是我的目标是将每个子文件夹的文件选择限制为.mtl文件类型之一。 So instead of copying all .mtl files in each subfolder, it would copy one .mtl file for each subfolder.
因此,与其在每个子文件夹中复制所有.mtl文件,不如为每个子文件夹复制一个.mtl文件。
How can I do this? 我怎样才能做到这一点?
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
xcopy /T /E "%sourcedir%\*.mtl" "%destdir%\"
SET "source=:"
FOR /f "delims=" %%a IN (
'xcopy /L /s "%sourcedir%\*.mtl" "%destdir%\" '
) DO (
CALL :chksubdir "%%a"
)
GOTO :EOF
:chksubdir
ECHO "%~1"|FIND "\">NUL
IF ERRORLEVEL 1 GOTO :EOF
IF /i "%source%"=="%~dp1" GOTO :EOF
SET "source=%~dp1"
CALL SET "sub=%%source:%sourcedir%=%%"
COPY "%~1" "%destdir%%sub%%~nx1" >NUL 2>nul
GOTO :eof
You would need to change the settings of sourcedir
and destdir
to suit your circumstances. 您需要更改
sourcedir
和destdir
的设置以适合您的情况。
The first step is to use the /t
option of xcopy
to create the destination subtree. 第一步是使用
xcopy
的/t
选项创建目标子树。
Having set source
to a dummy value, using the subroutine chksubdir
to process each line of XCOPY /L /S
for the files. 将
source
设置为虚拟值后,使用子例程chksubdir
来处理文件的XCOPY /L /S
每一行。 This is a list-only, and does not actually do the copy. 这是仅列表,并不实际进行复制。
The subroutine checks whether the supplied parameter contains \\
as the very last line of the xcopy /L
is a count-report. 该子例程检查提供的参数是否包含
\\
因为xcopy /L
最后一行是计数报告。 If no \\
is found, exit. 如果找不到
\\
,请退出。
Otherwise, match source
to the drive+path of the selected file. 否则,将
source
匹配到所选文件的驱动器+路径。 If these are not-equal, then a new directory has been found, otherwise skip this file. 如果这些不相等,则找到一个新目录,否则跳过此文件。
Set source
to the new subdirectory name and then set sub
to that full name minus the relative root, giving the subdirectory string below the relative root. 将
source
设置为新的子目录名称,然后将sub
设置为该全名减去相对根,在相对根下面给出子目录字符串。
String it all together to generate a simple copy
command. 将所有字符串串在一起以生成简单的
copy
命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.