【问题标题】:Batch file to copy files from a file contents批处理文件从文件内容中复制文件
【发布时间】:2012-07-18 22:54:23
【问题描述】:

我有一个主文件 (File1.txt),其中一些名称保留在内容中。我必须在一个文件夹中找到所有具有这些名称(通配符)的文件,然后使用批处理文件程序将它们移动到不同的文件夹中。

例如:File1.txt 有内容

  abcd
  efgh

现在在文件夹中说c:\temp\Source 我有类似的文件

12abcd34.asc
56efgh78.asc
testing.asc

我只需要将这 2 个文件移动到一个文件夹中,比如 c:\temp\Target。

这是我的代码,但它给出了错误,说 i*.* 目前是意外的。你能帮忙吗。

@Echo Off
title Test move files
set dir1=C:\temp\Source
dir %dir1%
Echo Directory Changed
FOR /f "eol=; delims=, " %i in (file1.txt) do move /y "*%i*.*" Target

【问题讨论】:

  • 在批处理文件中,%i 的 % 必须翻倍,例如 %%i

标签: dynamic batch-file filenames move


【解决方案1】:

给你....

这就是我开始时的目录结构...

C:\Temp>tree /f
Folder PATH listing for volume OS
Volume serial number is XXXX-XXXX
C:.
│   file1.txt
│   run.bat
│
├───Source
│       12abcd34.asc
│       56efgh78.asc
│       testing.asc
│
└───Target

这是我稍后会运行的 run.bat .. 包括错误修复...

C:\Temp>copy run.bat con
@Echo Off

title Test move files

set dir1=Source

dir %dir1%

Echo Directory Changed

FOR /f "eol=; delims=, " %%i in (file1.txt) do move /y "%dir1%\*%%i*.*" Target
        1 file(s) copied.

现在我运行批处理文件...

C:\Temp>run.bat
 Volume in drive C is OS
 Volume Serial Number is XXXX-XXXX

 Directory of C:\Temp

19/07/2012  00:03    <DIR>          .
19/07/2012  00:03    <DIR>          ..
18/07/2012  23:59                 0 12abcd34.asc
18/07/2012  23:59                 0 56efgh78.asc
18/07/2012  23:59                 0 testing.asc
               3 File(s)              0 bytes
               2 Dir(s)  41,653,194,752 bytes free
Directory Changed
C:\Temp\Source\12abcd34.asc
        1 file(s) moved.
C:\Temp\Source\56efgh78.asc
        1 file(s) moved.

现在这是最终的目录结构......所以你可以看到它正在工作......

C:\Temp>tree /f
Folder PATH listing for volume OS
Volume serial number is XXXX-XXXX
C:.
│   file1.txt
│   run.bat
│
├───Source
│       testing.asc
│
└───Target
        12abcd34.asc
        56efgh78.asc

这是您需要的 for 循环...

FOR /f "eol=; delims=, " %%i in (file1.txt) do move /y "%dir1%\*%%i*.*" Target

变化:

[1] within FOR you use %%i not %i.
[2] You need this format:

%dir1%  <-- Where
\       <-- path delimiter
*       <-- starts with anything
%%i     <-- contains what you want to search
*.*     <-- ends with anything

希望这会有所帮助。

【讨论】:

  • 哇!感谢您的快速回复和帮助。实际上我的第一个错误是 file1.txt 不在批处理文件所在的位置。他们在 2 个不同的地方,当我尝试使用 % 时,它说找不到文件。
猜你喜欢
  • 2022-12-31
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多