在目录中搜索特定字符串的XML文件,然后使用bat脚本执行复制操作

时间:2016-09-19 16:03:05

标签: xml windows batch-file

我是批处理脚本的新手,所以如果可能的话,我会很感激评论解决方案。 我正在寻找一个bat脚本,它将搜索包含XML文件的set目录c:\中包含“xyz”字符串的文件(在其中,而不是在其文件名中)。找到带有“xyz”的文件后,应将其复制到另一个位置c:\ out。 为了便于说明,所有XML文件都具有相同的结构,字符串(应搜索文件)位于标记内。

3 个答案:

答案 0 :(得分:2)

此代码的灵感来自Local_Search_Engine.bat

@ECHO OFF
Title Scan a folder and Search a string in XML-files by (c) Hackoo 2016
mode con cols=75 lines=2
Call :init
Call :inputbox "Please enter something to search :" "Search a string in multi-files by (c) Hackoo 2016"
If  "%input%" == ""  Color 0C & (
    echo(
    echo           You must enter a string to continue with this program 
    pause>nul & exit
) else (
    Call :Browse4Folder "Choose source folder to scan for %input%" "c:\scripts"
)
::******************************************************************************************
Set "ROOT=%Location%"
::Does string have a trailing back slash ? if so we remove it !
IF %ROOT:~-1%==\ SET ROOT=%ROOT:~0,-1%
Set "NewFolder2Copy=%userprofile%\Desktop\NewCopyXMLFiles"
SET "EXT=xml"
SET "Count=0"
set "Word2Search=%input%"
Set "LogFile=%~dp0%~n0_%Word2Search%.txt"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder and its subfolders.
REM And Populate the array with existent files in this folder and its subfolders
For %%a in (%EXT%) Do ( 
    Call :Scanning "%Word2Search%" "*.%%a" 
    Call :PS_Sub 'information' 10 '"Scanning now for """%Word2Search%""" on """*.%%a""" . . . "' "'Please wait. . . Scan is in progress on all """*.%%a""" . . .'" 'info' 5
    FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\*.%%a"') DO (
        ( find /I "%Word2Search%" "%%f" >nul 2>&1 ) && (
            SET /a "Count+=1"
            set "list[!Count!]=%%~nxf"
            set "listpath[!Count!]=%%~dpFf"
        )
    ) || (
            ( Call :Scanning "%Word2Search%" "%%~nxf" )
    )
)
::***************************************************************
:Display_Results
cls & color 0B
echo wscript.echo Len("%ROOT%"^) + 20 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")
If %cols% LSS 50 set /a cols=%cols% + 24
rem If %cols% LSS 50 set /a cols=%cols% + 15
set /a lines=%Count% + 12
Mode con cols=%cols% lines=%lines%
echo(
Call :color 0A " ------------------------------------------------" 1
ECHO   Folder : "%ROOT%"
Call :color 0A " ------------------------------------------------" 1
rem If Exist "%LogFile%" Del "%LogFile%"
rem Display array elements and save results into the LogFile
for /L %%i in (1,1,%Count%) do (
    echo [%%i] : !list[%%i]!
    echo [%%i] : !list[%%i]! -- "!listpath[%%i]!" >> "%LogFile%"     
)

(   
    ECHO.
    ECHO Total of [%EXT%] files(s^) : %Count% file(s^) that contains the string "%Word2Search%"
)>> "%LogFile%"
ECHO(
ECHO Total of [%EXT%] files(s) : %Count% file(s)
echo.
Call :color 0D "   Type the number of file that you want to explore" 1
echo(
Call :color 0C "     To save those files just hit 'S'" 1
set /p "Input="
For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Call :Explorer "!listpath[%%i]!"
    )
    IF /I "%INPUT%"=="S" (
        Call :CopyFiles
    )
)   
Goto:Display_Results
::**************************************************************
:Scanning <Word> <file>
mode con cols=75 lines=3
Cls & Color 0E
echo(
echo         Scanning for the string "%~1" on "%~2" ...
goto :eof
::*************************************************************
:Explorer <file>
explorer.exe /e,/select,"%~1"
Goto :EOF
::*************************************************************
:MakeCopy <Source> <Target>
If Not Exist "%~2\" MD "%~2\"
Copy /Y "%~1" "%~2\"
goto :eof
::*************************************************************
:CopyFiles
cls
mode con cols=80 lines=20
for /L %%i in (1,1,%Count%) do (
    echo Copying "!list[%%i]!" "%NewFolder2Copy%\"
    Call :MakeCopy  "!listpath[%%i]!" "%NewFolder2Copy%">nul 2>&1 
)
Call :Explorer "%NewFolder2Copy%\"
Goto:Display_Results
::***************************************************************************
:InputBox
set "input="
set "heading=%~2"
set "message=%~1"
echo wscript.echo inputbox(WScript.Arguments(0),WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do ( 
    set "input=%%a"
)
exit /b
::***************************************************************************
:PS_Sub $notifyicon $time $title $text $icon $Timeout
PowerShell  ^
  [reflection.assembly]::loadwithpartialname('System.Windows.Forms') ^| Out-Null; ^
 [reflection.assembly]::loadwithpartialname('System.Drawing') ^| Out-Null; ^
 $notify = new-object system.windows.forms.notifyicon; ^
  $notify.icon = [System.Drawing.SystemIcons]::%1; ^
  $notify.visible = $true; ^
  $notify.showballoontip(%2,%3,%4,%5); ^
  Start-Sleep -s %6; ^
  $notify.Dispose()
%End PowerShell%
exit /B
::****************************************************************************
:Browse4Folder
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
(
    echo set shell=WScript.CreateObject("Shell.Application"^) 
    echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^) 
    echo if typename(f^)="Nothing" Then  
    echo wscript.echo "set Location=Dialog Cancelled" 
    echo WScript.Quit(1^)
    echo end if 
    echo set fs=f.Items(^):set fi=fs.Item(^) 
    echo p=fi.Path:wscript.echo "set Location=" ^& p
)>%vbs%
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
::****************************************************************************
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
::****************************************************************************
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
    <nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
    echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
::***************************************************************************

答案 1 :(得分:1)

@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

您需要更改sourcedirdestdir的设置以适合您的具体情况。

为了测试目的,所需的COPY命令仅为ECHO在您确认命令正确后,将ECHO(COPY更改为COPY以实际复制文件。附加>nul以取消报告消息(例如1 file copied

简单地说,将findstrxyz的{​​{1}}的{​​{1}}输出收集到%sourcedir%\*.xml%%a开关输出找到的包含字符串的文件名。使用/m构造相应的copy命令,仅选择名称和扩展名。

答案 2 :(得分:0)

试试这个:

@echo on
set "BaseDir=folder_containing_xml_files"
set "DestDir=destination_dir"
set "ListFile=%userprofile%\Desktop\list.txt"
for /F "usebackq tokens=*" %%A in ("%ListFile%") do copy "%BaseDir%\%%~A" "%DestDir%"
pause

在桌面上创建list.txt,然后在xyz.xml中输入list.txt(文件名)。然后保存文件。编辑批次的路径,然后运行。