将自身复制到当前目录中的文件夹的批处理文件?

时间:2013-05-19 08:49:11

标签: file batch-file copy

情景就是这样。

我在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

你是如何实现这一目标的?

2 个答案:

答案 0 :(得分:2)

尝试此操作,查看输出并删除echo,如果可以:

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

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

答案 1 :(得分: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