在目录中的XML文件中搜索特定的字符串,然后使用bat脚本执行复制操作 [英] Search XML files in directory for specific string then do copy action using bat script
问题描述
我是批处理脚本的新手,因此,如果可能的话,我将感谢您提出评论的解决方案. 我正在寻找一个蝙蝠脚本,它将在包含XML文件的设置目录c:\中搜索包含"xyz"字符串(在其中而不是在其文件名中)的文件.找到带有"xyz"的文件后,应将其复制到另一个位置c:\ out. 出于说明目的,所有XML文件都具有相同的结构,并且字符串(应搜索文件)在标记内.
I'm new to batch scripts so I would appreciate commented solution if possible. I'm looking for a bat script that would search set directory c:\in containing XML files for files that contain "xyz" string (in them, not in their filename). Once the file with "xyz" is found it should be copied to another location c:\out . For illustration purposes all of XML files have the same structure and the string (that files should be search for) is within the tag .
推荐答案
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN ('findstr /m /L "xyz" "%sourcedir%\*.xml"') DO (
echo(COPY "%%a" "%destdir%\%%~nxa"
)
GOTO :EOF
您需要根据自己的情况更改sourcedir
和destdir
的设置.
You would need to change the settings of sourcedir
and destdir
to suit your circumstances.
所需的COPY命令仅被ECHO
用于测试目的. 确认命令正确无误后,将ECHO(COPY
更改为COPY
以实际复制文件.附加>nul
以禁止显示报告消息(例如1 file copied
)
The required COPY commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY
to COPY
to actually copy the files. Append >nul
to suppress report messages (eg. 1 file copied
)
只需将findstr
的输出收集到%%a
中,以在%sourcedir%\*.xml
中搜索文字xyz
. /m
开关输出找到的包含字符串的文件名.使用~nxa
构造适当的copy
命令以仅选择名称和扩展名.
simply, gather the output of findstr
searching for literal xyz
in %sourcedir%\*.xml
into %%a
. The /m
switch outputs the filenames found containing the string. Construct the appropriate copy
command using ~nxa
to select just the name and extension.
这篇关于在目录中的XML文件中搜索特定的字符串,然后使用bat脚本执行复制操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!