Batch file Xcopy目录模式匹配

Batch file Xcopy目录模式匹配,batch-file,pattern-matching,xcopy,Batch File,Pattern Matching,Xcopy,我需要使用bat文件将源文件复制到目标文件夹 我创造了这个: @echo off set source="D:\Folder1\file.dll" set destination="D:\Test\TestCopy*\Test\" xcopy /s /y %source% %destination% pause 我的目标路径是TestCopy.2.5.3.6。这个数字可以改变。因此,指定TestCopy*不起作用。指定TestCopy.*.也不起作用 如何解决这个问题?这不是使用xcopy

我需要使用bat文件将源文件复制到目标文件夹

我创造了这个:

@echo off

set source="D:\Folder1\file.dll"
set destination="D:\Test\TestCopy*\Test\"
xcopy /s /y %source%  %destination%
pause
我的目标路径是
TestCopy.2.5.3.6
。这个数字可以改变。因此,指定
TestCopy*
不起作用。指定
TestCopy.*.
也不起作用


如何解决这个问题?

这不是使用
xcopy
的方式;它无法将目录结构/文件复制到多个文件夹,例如,将文件
random.ext
复制到
C:\folder1\test
C:\folder2\test
,等等

此外,无需使用
xcopy
来复制文件。只需使用
copy
即可

为此,请使用:

@echo关闭
pushd“D:\Test”
set source=“D:\Folder1\file.dll”
对于/F“delims=eol=“%%A IN('dir/B/AD“D:\Test\TestCopy*”)do(
复制%source%%“%%~fA\Test\”
)
邻苯二胺
或者,最好用一行
表示/F

@echo关闭
pushd“D:\Test”
set source=“D:\Folder1\file.dll”
对于/F“delims=eol=”('dir/B/AD“D:\Test\TestCopy*”)中的%%A,请复制%source%%“%%~fA\Test\”
邻苯二胺

这适用于具有多个子文件夹的情况:

::It's better to comment out @echo off when you are testing the batch file
::@echo off
::Moved the open quote's position, so the source variable won't have quotes inside.
set "source=D:\Folder1\file.dll"
for /F "delims=" %%i IN ('dir /ad /b "D:\Test\TestCopy*"') do call :fcopy "%%i"
::Comment or remove pause when it's okay.
pause
goto :eof

:fcopy
if not exist "D:\Test\%1\Test\" goto :eof
xcopy "%source%" "D:\Test\%1\Test\"
使用子过程:fcopy,将每个
TestCopy*
子文件夹名称传递给它。
所有的
::
起始行都是注释行。他们没有被处决


我认为在使用时引用每个path变量是一个好习惯,但不要在变量中包含引号本身——这就是为什么我将引号移动到
set source
行中。

通配符,如
*
只能在路径的最后一个元素中使用;您可以使用如下方法解决问题:
for/D%%D in(“D:\Test\TestCopy.*.*)执行mkdir“%%~D\Test”©/Y“D:\Folder1\file.dll”“%%~D\Test\”