批量,读取文件和复制文件名称

时间:2020-12-02 02:05:39

I get stuck on a basic problem :(

我陷入了一个基本问题:(

I want to read a txt file from a batch file, copy files name like found in the file to a new directory

我想从批处理文件中读取一个txt文件,将文件中找到的文件名复制到一个新目录

Exemple :
- My src folder contains Microsoft KB (arround 300 files) with name Like "Windows 6.1-KBxxxxx.msu"
- My file [KB.Txt] is look like :
KB320000
KB320001
- dst folder is empty

示例: - 我的src文件夹包含名称为“Windows 6.1-KBxxxxx.msu”的Microsoft KB(arround 300个文件) - 我的文件[KB.Txt]如下所示:KB320000 KB320001 - dst文件夹为空

... So I want to copy only KB in my KB.txt from src to dst

...所以我想只将我的KB.txt中的KB从src复制到dst

My batch look like :

我的批次看起来像:

set src=%~dp0\src
set dst=%~dp0\dst
set file=%~dp0KB.txt

for /f "delims=" %%i in (%file%) do (
xcopy "%src%\%%i" "%dst%\%%i" /i /z /y /s
)

This do not work because the script only try to copy with exact name. I have tested with wildcards with no success... What I have done wrong ?

这不起作用,因为脚本只尝试使用确切的名称进行复制。我用通配符测试没有成功......我做错了什么?

Thanks!

1 个解决方案

#1


set src=%~dp0\src
set dst=%~dp0\dst
set file=%~dp0KB.txt

for /f "delims=" %%i in (%file%) do (
   copy /y  "%src%\*%%i*" "%dst%\" 
)

copy command supports wildcards so enclosing %%i with asterisks should work. Using XCOPY for files (the command is designed for folders) is a bad idea as it will prompt if you want to create a destination directory.

copy命令支持通配符,因此用%as括起%% i应该可以工作。将XCOPY用于文件(该命令是为文件夹设计的)是一个坏主意,因为它会提示您是否要创建目标目录。

#1


set src=%~dp0\src
set dst=%~dp0\dst
set file=%~dp0KB.txt

for /f "delims=" %%i in (%file%) do (
   copy /y  "%src%\*%%i*" "%dst%\" 
)

copy command supports wildcards so enclosing %%i with asterisks should work. Using XCOPY for files (the command is designed for folders) is a bad idea as it will prompt if you want to create a destination directory.

copy命令支持通配符,因此用%as括起%% i应该可以工作。将XCOPY用于文件(该命令是为文件夹设计的)是一个坏主意,因为它会提示您是否要创建目标目录。