使用批处理将单个文件复制到子文件夹及其子文件夹等

using a batch to copy single file into subfolders and their subfolders and so on

我很好奇是否可以通过使用批处理来完成必须执行的任务。 我必须将单个文件复制到多个子文件夹中,这些子文件夹也必须将文件复制到子文件夹中。 使用批处理,输入看起来像这样:

@echo off
for /d %%a in ("C:\Users\thomas\Desktop\Battest\*.\*") do xcopy /y /d C:\Users\thomas\Desktop\Battest\indextest.php "%%a"

这只会将 index.php 复制到直接位于下一层的第一个子文件夹中。
由于 index.php 需要复制到 700 个文件夹中,而且我不知道所有具体的目录路径,所以我不确定如何使用通配符处理这个问题,或者这个任务是否可以像这样完成。

我试过使用批处理将单个文件复制到多个子文件夹中,但我不知道哪个路径。到目前为止,我只是设法将文件复制到第一个子文件夹中。

这是显示定向结构的命令输出。可以看到每个文件夹里面都有一个index.php需要replaced(我之前没有指定,sorry):

C:\Users\thomasb\Desktop_General Catalogues and Price Lists>dir
 Datenträger in Laufwerk C: ist Windows
 Volumeseriennummer: 3C59-5A60

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.03.2022  07:53    <DIR>          01_Sealing Guide
25.03.2022  07:54    <DIR>          02_Precast Concrete Plant Catalogue
25.03.2022  07:54    <DIR>          03_Structural and Civil Engineering Catalogue
25.03.2022  07:54    <DIR>          04_Price list
25.03.2022  07:54    <DIR>          05_Press Releases
25.03.2022  07:54    <DIR>          06_News
25.03.2022  07:54    <DIR>          07_Consulting Catalogue
23.03.2022  16:26             3.627 index.php
               1 Datei(en),          3.627 Bytes
               9 Verzeichnis(se), 343.890.636.800 Bytes frei

C:\Users\thomasb\Desktop_General Catalogues and Price Lists>dir /s
 Datenträger in Laufwerk C: ist Windows
 Volumeseriennummer: 3C59-5A60

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.03.2022  07:53    <DIR>          01_Sealing Guide
25.03.2022  07:54    <DIR>          02_Precast Concrete Plant Catalogue
25.03.2022  07:54    <DIR>          03_Structural and Civil Engineering Catalogue
25.03.2022  07:54    <DIR>          04_Price list
25.03.2022  07:54    <DIR>          05_Press Releases
25.03.2022  07:54    <DIR>          06_News
25.03.2022  07:54    <DIR>          07_Consulting Catalogue
23.03.2022  16:26             3.627 index.php
               1 Datei(en),          3.627 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Sealing Guide

25.03.2022  07:53    <DIR>          .
25.03.2022  07:53    <DIR>          ..
25.03.2022  07:53    <DIR>          English
25.03.2022  07:53    <DIR>          French
25.03.2022  07:53    <DIR>          German
23.03.2022  16:26             3.627 index.php
25.03.2022  07:53    <DIR>          Russian
25.03.2022  07:53    <DIR>          Spanish
               1 Datei(en),          3.627 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Sealing Guide\English

25.03.2022  07:53    <DIR>          .
25.03.2022  07:53    <DIR>          ..
02.10.2020  14:14         2.393.613 BTi_Catalogue_Sealing Guide_EN_2020-09.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      2.395.719 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Sealing Guide\French

25.03.2022  07:53    <DIR>          .
25.03.2022  07:53    <DIR>          ..
07.05.2021  08:53         2.997.413 BTi_Sealing Guide_FR_2020-09.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      2.999.519 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Sealing Guide\German

25.03.2022  07:53    <DIR>          .
25.03.2022  07:53    <DIR>          ..
17.06.2021  09:27         2.872.797 BTi_Abdichtungsguide_DE_2020-09.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      2.874.903 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Sealing Guide\Russian

25.03.2022  07:53    <DIR>          .
25.03.2022  07:53    <DIR>          ..
07.05.2021  08:42         3.039.947 BTi_Руководство по герметизации_RU_09-2020.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      3.042.053 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Sealing Guide\Spanish

25.03.2022  07:53    <DIR>          .
25.03.2022  07:53    <DIR>          ..
13.12.2021  11:34         3.560.598 BTi_Katalog_Abdichtung_ES_2021-12_web.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      3.562.704 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Precast Concrete Plant Catalogue

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.03.2022  07:53    <DIR>          English
25.03.2022  07:53    <DIR>          French
25.03.2022  07:54    <DIR>          German
23.03.2022  16:26             3.627 index.php
25.03.2022  07:54    <DIR>          Polish
25.03.2022  07:54    <DIR>          Russian
25.03.2022  07:54    <DIR>          Spanish
               1 Datei(en),          3.627 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Precast Concrete Plant Catalogue\English

25.03.2022  07:53    <DIR>          .
25.03.2022  07:53    <DIR>          ..
07.10.2020  08:27         9.967.701 BTi_Catalogue_Precast Concrete Plant_EN_2020-09.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      9.969.807 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Precast Concrete Plant Catalogue\French

25.03.2022  07:53    <DIR>          .
25.03.2022  07:53    <DIR>          ..
10.07.2020  15:01         9.737.367 BTi_Catalogue_Precast Concrete Plant_FR_2020_07.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      9.739.473 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Precast Concrete Plant Catalogue\German

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
10.07.2020  12:34        10.145.406 BTi_Katalog_Betonfertigteilwerk_DE_2020-07.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),     10.147.512 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Precast Concrete Plant Catalogue\Polish

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
10.07.2020  15:35        10.116.783 BTi_Catalogue_Precast Concrete Plant_PL_2020-07.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),     10.118.889 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Precast Concrete Plant Catalogue\Russian

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
11.08.2020  07:53         5.882.519 BTi_Catalogue_Precast Concret Plant_RU_2020-07_web.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      5.884.625 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Precast Concrete Plant Catalogue\Spanish

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
10.07.2020  14:24         9.857.478 BTi_Catalogue_Structural and Civil Engineering_ES_2020-07.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      9.859.584 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Structural and Civil Engineering Catalogue

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.03.2022  07:54    <DIR>          English
25.03.2022  07:54    <DIR>          French
25.03.2022  07:54    <DIR>          German
23.03.2022  16:26             3.627 index.php
25.03.2022  07:54    <DIR>          Russian
25.03.2022  07:54    <DIR>          Spanish
               1 Datei(en),          3.627 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Structural and Civil Engineering Catalogue\English

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
26.07.2019  12:49         7.243.746 BTi_Catalogue_Structural and Civil Engineering_EN_2019-07.pdf
28.04.2021  15:43             2.106 index.php
11.12.2019  08:36               283 Web_Version.url
               3 Datei(en),      7.246.135 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Structural and Civil Engineering Catalogue\French

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.09.2019  09:04         5.916.375 BTi_Catalogue_Structural and Civil Engineering_FR_2019-09.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      5.918.481 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Structural and Civil Engineering Catalogue\German

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.07.2019  08:46         7.284.669 BTi_Katalog_Produktkatalog Hoch- und Tiefbau_DE_2019-07.pdf
16.11.2021  16:09        12.879.830 BTi_Katalog_Schalungstechnik_Syflex_DE_2021-11.pdf
28.04.2021  15:43             2.106 index.php
11.12.2019  08:34               267 Web-Version.url
               4 Datei(en),     20.166.872 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Structural and Civil Engineering Catalogue\Russian

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
11.08.2020  07:54         3.245.824 BTi_Catalogue_Structural and Civil Engineering_RU_2019-11_web.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      3.247.930 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Structural and Civil Engineering Catalogue\Spanish

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
27.03.2020  13:00         7.493.352 BTi_Catalogue_Structural and Civil Engineering_ES_2020-03.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      7.495.458 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Price list

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
23.03.2022  16:26             3.627 index.php
               1 Datei(en),          3.627 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Press Releases

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.03.2022  07:54    <DIR>          English
25.03.2022  07:54    <DIR>          French
25.03.2022  07:54    <DIR>          German
23.03.2022  16:26             3.627 index.php
25.03.2022  07:54    <DIR>          Russian
25.03.2022  07:54    <DIR>          Spanish
               1 Datei(en),          3.627 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Press Releases\English

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
26.04.2021  09:02        11.764.334 BTi_Press Kit_EN_2021-04.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),     11.766.440 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Press Releases\French

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
26.04.2021  10:11         8.163.842 BTi_Dossier de presse_FR_2021-04.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      8.165.948 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Press Releases\German

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
30.03.2021  12:46           585.743 30 Jahre B.T innovation_de.pdf
04.03.2021  09:38        12.443.367 BTi_Pressemappe_DE_2021-03.pdf
28.04.2021  15:43             2.106 index.php
               3 Datei(en),     13.031.216 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Press Releases\Russian

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
26.04.2021  09:42         7.846.420 BTi_Press Kit_RU_2021-04.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      7.848.526 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Press Releases\Spanish

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
26.04.2021  10:05         7.596.592 BTi_Kit de prensa_SP_2021-04.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),      7.598.698 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_News

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
08.06.2021  14:06           461.634 BTi_Important Information Regarding Prises and Offers 06-08-2021.pdf
23.03.2021  08:59           328.846 BTi_Price Increase for MultiForm starting 04-01-2021.pdf
23.03.2021  09:00           320.885 BTi_Price increase for shuttering magnets starting 05-01-2021.pdf
23.03.2022  16:26             3.627 index.php
07.03.2022  12:20           368.344 Price Increase BT-Spannschloss® (Turnbuckle) - BT innovation.pdf
31.01.2022  15:40           493.366 Price increase for RubberElast - BT innovation.pdf
25.01.2022  08:13           112.744 Price Increase GRP Products - BT innovation.pdf
               7 Datei(en),      2.089.446 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Consulting Catalogue

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.03.2022  07:54    <DIR>          English
25.03.2022  07:54    <DIR>          German
23.03.2022  16:26             3.627 index.php
25.03.2022  07:54    <DIR>          Russian
               1 Datei(en),          3.627 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Consulting Catalogue\English

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
24.11.2021  11:27        14.069.238 BTi_Catalogue_Consulting_EN_2021-10_web.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),     14.071.344 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Consulting Catalogue\German

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.11.2021  13:37        14.651.703 BTi_Produktkatalog_consulting_DE_2021-10_Web.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),     14.653.809 Bytes

 Verzeichnis von C:\Users\thomasb\Desktop_General Catalogues and Price Lists_Consulting Catalogue\Russian

25.03.2022  07:54    <DIR>          .
25.03.2022  07:54    <DIR>          ..
25.11.2021  09:10        11.576.341 BTi_Catalogue_Consulting_RU_web.pdf
28.04.2021  15:43             2.106 index.php
               2 Datei(en),     11.578.447 Bytes

     Anzahl der angezeigten Dateien:
              66 Datei(en),    205.498.927 Bytes
              95 Verzeichnis(se), 343.888.109.568 Bytes frei

你不能用 forfiles 做到这一点吗?类似(未测试):

FORFILES /C "cmd /c if @isdir==TRUE xcopy /y /d C:\Users\...\indextest.php @path"
for /r "C:\Users\thomas\Desktop\Battest" %%a in (.) do if /i "%%~dpa" neq "C:\Users\thomas\Desktop\Battest\" ECHO xcopy /y /d C:\Users\thomas\Desktop\Battest\indextest.php "%%a"

应该echo复制命令为每个子目录指定的目录后/r.

很明显,验证后去掉echo关键字才能真正执行xcopy.

有关详细信息,请参阅提示中的 for /?

根据具体情况,有几种可能性:

  1. (重新)将文件 indextest.php 放入根目录 %UserProfile%\Desktop\Battest 的直接子目录中:

     for /D %%J in ("%UserProfile%\Desktop\Battest\*") do (
         xcopy /Y /D "%UserProfile%\Desktop\Battest\indextest.php" "%%~J"
     )
    
  2. (重新)将文件indextest.php放入根目录grand-children中%UserProfile%\Desktop\Battest:

     for /D %%J in ("%UserProfile%\Desktop\Battest\*") do (
         for /D %%I in ("%%~J\*") do (
             xcopy /Y /D "%UserProfile%\Desktop\Battest\indextest.php" "%%~I"
         )
     )
    
  3. (重新)将文件 indextest.php 放入根目录 %UserProfile%\Desktop\Battest 的子目录和 grand-children:

     for /D %%J in ("%UserProfile%\Desktop\Battest\*") do (
         xcopy /Y /D "%UserProfile%\Desktop\Battest\indextest.php" "%%~J"
         for /D %%I in ("%%~J\*") do (
             xcopy /Y /D "%UserProfile%\Desktop\Battest\indextest.php" "%%~I"
         )
     )
    
  4. (重新)将文件 indextest.php 放入根目录 %UserProfile%\Desktop\Battest 的所有后代中:

     for /D /R "%UserProfile%\Desktop\Battest" %%J in (*) do (
         xcopy /Y /D "%UserProfile%\Desktop\Battest\indextest.php" "%%~J"
     )
    

您可以将命令 xcopy /Y /D 替换为 replace /U 以避免在 sub-directories 中添加尚未包含此类的文件 indextest.php

所以我在这里尝试了这个 (v4 @aschipfl):

for /D /R "%UserProfile%\Desktop_General Catalogues and Price Lists" %%J in (*) do (replace /S "%UserProfile%\Desktop_General Catalogues and Price Lists\index.php" "%%~J")

对于我的实际项目,它成功了! 谢谢@aschipl 和其他人!