简体   繁体   English

批处理文件,仅将每个子文件夹的文件类型之一复制到另一个文件夹

[英]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. 您需要更改sourcedirdestdir的设置以适合您的情况。

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.

相关问题 如何在批处理文件中将选定的文件从一个文件夹复制到另一个文件夹? - How to copy selected files from one folder to another in Batch file? 将子文件夹的内容复制到另一个文件夹 - Copy contents of subfolder to another folder 如何使用此批处理文件将所有* .txt文件从子文件夹复制到批处理文件文件夹? - How to copy all *.txt files from subfolder to batch file folder using this batch-file? 批处理文件将超过30分钟的文件从一个文件夹复制到另一个文件夹 - batch file to copy files older than 30 minutes from one folder to another 批处理:将一个txt文件的内容复制到另一个 - Batch: copy content of one txt file into another 循环到子文件夹批处理文件重复相同的文件夹 - Looping to subfolder batch file repeats same folder 批处理文件,仅当有一个文件且名称相同且没有扩展名时,才将内容从子文件夹复制到父文件夹 - Batch file to copy contents from sub-folder to parent only if there's one file and it's the same name without extension 批量创建文件夹和子文件夹 - Create folder and subfolder in batch 将每个文件复制到每个文件夹 - copy each file to each folder 如何使用PowerShell仅将新文件从一个文件夹复制到另一个文件夹 - How To copy only new files from one folder to another with PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM