批量复制文件夹中的某些文件

Batch copying certain files within a folder

我正在尝试在文件夹及其子文件夹中查找特定文件并将它们复制到新文件夹中。我曾经为此目的使用此批处理文件,但现在出现此错误:

系统找不到指定的文件。

批处理文件内容如下:

pushd "\internal.company.com\path\"
md myfile
FOR /R "\internal.company.com\path\" %%G in (prefix_myfile*) do copy %%G "\internal.company.com\path\myfile"

欢迎任何意见。

更新

我试过像这样打印 %%G

FOR /R "\internal.company.com\path\" %%G in (prefix_myfile*) do echo %%G

而且效果很好。 copy 命令出现问题,该命令无法将 %%G 作为参数读取。

我会改成

... copy "%%G" ...

这将满足 %%G.

中的空间

也许您应该确定问题出在 for/r 还是 %%G。如果您只是用 echo %%G 命令显示 %%G,那么您可能会发现一些问题。如果它仍然不会 运行,那么 for /r 就是问题所在。

由于您正在 pushd 访问适当的目录,因此无需在 for/r 中指定目标目录。可以省略或替换为.

或直接在 pushd 之后 echo %cd% 以显示 pushd 是否是问题的原因。

我意识到这对我有用(在 cmd 中执行)。但是,这是合理的,因为我在主目录中没有任何 *.txt 文件。不是最好的解决方案,而是一种解决方法。

pushd "\internal.company.com\path\"
rem first copy the desired files (text files) to the main-folder
for /f "tokens=*" %f in ('dir /a:-D /s /b myfile*') do copy "%f"
rem then make a new-folder and move them to there
md myfile
move *.txt "\internal.company.com\path\myfile"

注意:如果你想把它作为一个批处理文件执行,你需要使用%%f而不是%f

这会将文件复制到主文件夹中,然后将它们移动到所需的子文件夹中。