0

情景是这样的。

我在 I:\test.bat 中有一个批处理文件在我的驱动器中,有 3 个文件夹。像这样。

I:\folder1
I:\folder2
I:\folder2\folder3

假设我不知道folder1、folder2和folder3的名称那么,如何制作一个知道当前路径中文件夹的批处理文件,然后以相同的名称复制它?

我想要这个。

I:\folder1\test.bat
I:\folder2\test.bat
I:\folder2\folder3\test.bat

你如何做到这一点?

4

2 回答 2

2

试试这个,查看输出并删除echo,如果没问题:

@echo off
cd /d I:\
for /r /d %%i in (folder?) do echo copy "%~f0" "%%i"

这会将批处理文件名扩展为完整路径+文件名:%~f0.

于 2013-05-19T09:10:53.877 回答
0

从命令行这有效:

for /d %a in (*) do copy test.bat "%a"

如果您计划将其用于批处理文件,请使用 double %%a

在批处理文件中将自身复制到所有子文件夹中

@echo off 
for /d %%a in (*) do copy "%~n0%~x0" "%%a"

help for命令:

FOR /D %variable IN (set) DO command [command-parameters]

    If set contains wildcards, then specifies to match against directory
    names instead of file names.

help call命令:

 Substitution of batch parameters (%n) has been enhanced.  You can
 now use the following optional syntax:

     %~n1        - expands %1 to a file name only
     %~x1        - expands %1 to a file extension only
于 2013-05-19T08:53:06.703 回答