通过FOR / r命令发出复制文件

时间:2014-07-10 23:38:29

标签: batch-file command-line cmd

只是想感谢任何人帮助我解决这个问题。我后方的疼痛。

无论如何,我要做的是将单个文件复制到任何指定子目录的每个子目录。我遇到的问题是它实际上并没有复制.....

我认为这可能是因为路径存在某种错误。我把它缩小到了那个罪魁祸首。关于路径中的空间的东西。

使用此程序,我向用户询问他们要将文件复制到所有指定目录子目录的目录。然后我问他们想要复制哪个文件,然后繁荣。我运行FOR / R来反复执行副本。但是,当我使用其中包含空格的路径执行此操作时示例:Program Files,该文件不会复制。

我尝试过各种各样的东西,例如引用,设置程序文件到Progra~1,但我无法理解。你能帮忙吗?

ECHO OFF
REM Batch file for copying and pasting user specified file into every sub-folder
REM of a specified folder.
ECHO NOTE! Make sure before you run this program, that you have
ECHO completed the following.
ECHO .
ECHO 1. Your file that you wish to be copied is in the same
ECHO directory as this batch file.
echo .
ECHO 2. When specifying the file to be copied, only include
ECHO the file name and extension. example: Test.txt
echo .

set /p Directory=Enter the folder path where the file will be copied:%=%

set /p FileToCopy=Enter the File name and extension:%=%

FOR /R %~Directory% %%G IN (%FileToCopy%) DO copy %FileToCopy% %%G

2 个答案:

答案 0 :(得分:0)

看起来你有一点倒退。

尝试从这样的事情开始:

@echo off
set /p BaseTargetFolder=Full Path to Base Target Folder: 
set /p File=Full Path to File: 
cd %BaseTargetFolder%
copy "%File%" "%BaseTargetFolder%"
for /d /r %%G in ("*") do copy "%File%" "%%G"
pause

结果如下。

Full Path to Base Target Folder: C:\My Test
Full Path to File: C:\Temp\Test.txt
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
    1 file(s) copied.
Press any key to continue . . .

以下是我用来测试的文件夹结构:

"C:\My Test\Test 1"
"C:\My Test\Test 2"
"C:\My Test\Test 1\Test 1A"
"C:\My Test\Test 1\Test 1B"
"C:\My Test\Test 1\Test 1A\Test 1A1"
"C:\My Test\Test 1\Test 1A\Test 1A2"
"C:\My Test\Test 1\Test 1B\Test 1B1"
"C:\My Test\Test 1\Test 1B\Test 2B1"
"C:\My Test\Test 2\Test 2A"
"C:\My Test\Test 2\Test 2B"
"C:\My Test\Test 2\Test 2A\Test 2A1"
"C:\My Test\Test 2\Test 2A\Test 2A2"
"C:\My Test\Test 2\Test 2B\Test 2B1"
"C:\My Test\Test 2\Test 2B\Test 2B2"

答案 1 :(得分:0)

你差不多完成了

set /p "Directory=Enter the folder path where the file will be copied: "
set /p "FileToCopy=Enter the File name and extension: "

FOR /D /R "%Directory%" %%G IN (*) DO copy "%FileToCopy%" "%%~fG"

您需要迭代的是文件夹结构,而不是要复制的文件,引号将处理路径中的空间问题。

如果需要将文件复制到指定的文件夹,请将*替换为.,这样也会包含起始文件夹。