首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使脚本也复制带有批处理的目录树

如何使脚本也复制带有批处理的目录树
EN

Stack Overflow用户
提问于 2022-10-10 20:20:19
回答 1查看 70关注 0票数 1

我制作了一个可以copy certain folders的脚本,但我希望它也能在找到文件夹的地方找到copy the directory folders

这些是我的文件夹,现在我保持它简短,因为我有更多的文件夹比我在这里共享的,在面包文件夹我有20个文件夹,在小麦文件夹我有更多的15个文件夹,我有更多的主文件夹数以千计。

为了我的目标,我知道RobocopyXcopy的情况,这些脚本不会完成这项工作,因为我必须对许多文件夹进行/XD,并且必须有很多文件夹来键入脚本的每个文件夹。

这是我的工作站

代码语言:javascript
复制
Grains.bat
Food Network
+ ---- Bread
        + --- Grains
                |-- data.pgt
        + --- Corn
                |-- data.pgt
+ ---- Wheat
        + --- Grains
                |-- data.pgt
        + --- Flour
                |-- data.pgt

Results
All Grains
+ ---- Grains
         |-- data.pgt

每个Grains文件夹都被合并,data.pgt覆盖

我在这里的目标是获得这个结果,并将重点放在文件夹名上。

代码语言:javascript
复制
My resluts
All Grains
+ ---- Bread
        + --- Grains
                |-- data.pgt
+ ---- Wheat
        + --- Grains
                |-- data.pgt

这是我的剧本

代码语言:javascript
复制
for /f "tokens=*" %%G in ('dir /b /s /a:d "Grains"') do move "%%G" "All Grains"

现在,我只能让脚本MOVE文件夹,我测试了拷贝,但它不复制

这就是设置这个脚本的方法

代码语言:javascript
复制
@ECHO OFF
SETLOCAL

SET "sourcedir=%~dp0"
SET "destdir=%~dp0"

SET "tdnames="Food Network""

SET "leafnames="Grains""

SET "destsub="All Grains""

FOR %%T IN (%destsub%) DO (
 FOR %%t IN (%tdnames%) DO (
  FOR %%s IN (%leafnames%) DO (
   FOR /f "delims=" %%b IN ('dir /b /s /ad "%sourcedir%\%%~t\%%~s" 2^>nul') DO (
    FOR %%c IN ("%%~dpb.") DO (
     MD "%destdir%\%%~T\%%~nxc\%%~nxb" 2>nul
     Xcopy "%%~b\*" "%destdir%\%%~T\%%~nxc\%%~nxb"
    )
   )
  )
 )
)

GOTO :EOF

下面是使用/p更新的脚本

代码语言:javascript
复制
@ECHO OFF
SETLOCAL

Set /p FValue1=Enter Location to Copy:
Set /p FValue2=Enter Location to Save:

SET "sourcedir=%FValue1%"
SET "destdir=%FValue2%"
SET "tdnames="Food Network""
SET "leafnames="Grains""
SET "destsub="All Grains""

FOR %%T IN (%destsub%) DO (
 FOR %%t IN (%tdnames%) DO (
  FOR %%s IN (%leafnames%) DO (
   FOR /f "delims=" %%b IN ('dir /b /s /ad "%sourcedir%\%%~t\%%~s" 2^>nul') DO (
    FOR %%c IN ("%%~dpb.") DO (
     MD "%destdir%\%%~T\%%~nxc\%%~nxb" 2>nul
     Xcopy "%%~b\*" "%destdir%\%%~T\%%~nxc\%%~nxb"
     rem move "%%~b\*" "%destdir%\%%~T\%%~nxc\%%~nxb"
     rem rd "%%b"
    )
   )
  )
 )
)
goto :CopierStation5
GOTO :EOF

新测试领域

代码语言:javascript
复制
Grains.bat
    Food Network
    + ----- Breads
            + ---- Bread
                   + --- Grains
                           |-- data.pgt
    + ----- Corns
            + --- Corn
                    |-- data.pgt

    + ----- Wheats
            + ---- Wheat
                   + --- Grains
                           |-- data.pgt
    + ----- Flours
            + --- Flour
                    |-- data.pgt

    Results
    All Grains
    + ----- This Folder is missing < ----------
            + ---- Bread
                   + --- Grains
                           |-- data.pgt
    + ----- This Folder is missing < ----------
            + ---- Wheat
                   + --- Grains
                           |-- data.pgt
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-10 22:40:47

代码语言:javascript
复制
@ECHO OFF
SETLOCAL
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"

:: Directory names to copy

SET "tdnames="Food Network""

:: Leafnames to copy

SET "leafnames="Grains""

:: Destination subdirectory

SET "destsub="All Grains""

:: %%T : Target directory
:: %%t, %%s : top and sub of source

FOR %%T IN (%destsub%) DO (
 FOR %%t IN (%tdnames%) DO (
  FOR %%s IN (%leafnames%) DO (
   FOR /f "delims=" %%b IN ('dir /b /s /ad "%sourcedir%\%%~t\%%~s" 2^>nul') DO (
    FOR %%c IN ("%%~dpb.") DO (
     ECHO MD "%destdir%\%%~T\%%~nxc\%%~nxb" 2>nul
     ECHO XCOPY "%%~b\*" "%destdir%\%%~T\%%~nxc\%%~nxb"
    )
   )
  )
 )
)

GOTO :EOF

在应用于实际数据之前,始终要针对测试目录进行验证。

请注意分配给各个变量的字符串的格式。如果您想要添加包含数据的其他目录,那么只需在引号中将目录名添加到空格分隔的列表中,因此要将another directory添加到Food Network,请使用SET "tdnames="Food Network" "another directory""

从那开始,这就很简单了。每个for处理一个名称列表,每个名称都被引用,因此%%~X将使用未引用的名称。

dir从源目录中查找%%t下面的目录名称%%s。如果不存在这种情况,2>nul将抑制错误消息(插入符号escapes -- >重定向器,告诉cmd >dir的一部分,而不是for)。

%%b设置为完整的目录名,因此%%~nxb生成传单名,%%~nxc生成父名称。

所需的mdxcopy行只是为验证而编写的echo。如果所有内容都如预期的那样出现,请删除这些echo语句以激活副本.

额外子目录级别的======修订

代码语言:javascript
复制
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"

:: Directory names to copy

SET "tdnames="Food Network""

:: Leafnames to copy

SET "leafnames="Grains" "Corn""
:: Destination subdirectory

SET "destsub="All Grains""

:: %%T : Target directory
:: %%t, %%s : top and sub of source

FOR %%T IN (%destsub%) DO (
 FOR %%t IN (%tdnames%) DO (
  FOR %%s IN (%leafnames%) DO (
   FOR /f "delims=" %%b IN ('dir /b /s /ad "%sourcedir%\%%~t\%%~s" 2^>nul') DO (
    SET "target=%%b"
    SET "target=!target:%sourcedir%\%%~t=!"
    ECHO MD "%destdir%\%%~T!target!" 2>nul
    ECHO XCOPY "%%~b\*" "%destdir%\%%~T!target!"
   )
  )
 )
)

GOTO :EOF

在应用于实际数据之前,始终要针对测试目录进行验证。

这一次,调用delayedexpansion允许在代码块Stephan's DELAYEDEXPANSION link中修改字符串。

请注意,leafnames包括用于测试的corn

变量target设置为grains目录的完整目录名,然后将源目录和第一个子级别从该字符串中删除,剩下的目录级别将被包含在mdxcopy命令中以供执行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74020305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档