用于执行搜索和文件复制的脚本

时间:2012-12-13 16:50:38

标签: batch-file vbscript

我是一个完整的脚本新手。我想知道是否有人会帮我创建一个脚本。我正在寻找的脚本是一个批处理文件来执行查找和移动过程。该查找将在dicom文件上搜索文本字符串(示例患者ID)。此外,查找还需要在子文件夹中搜索。此外,查找的文件扩展名为.dcm或.raw。完成查找后,找到包含文本字符串的文件。我希望脚本然后将找到的文件复制到桌面。任何有关这方面的帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

setlocal enabledelayedexpansion
for /r C:\folder %%a in (*.dcm *.raw) do (
find "yourstring" "%%a"
if !errorlevel!==0 copy "%%a" "%homepath%\Desktop" /y
)

答案 1 :(得分:1)

这应该为你做。要在命令行中查看每个命令类型command /?可用的所有选项。

echo /?
for /?
find /?
xcopy /?
findstr /?
...

方法1 :(推荐)

:: No delayed expansion needed.
:: Hide command output.
@echo off
:: Set the active directory; where to start the search.
cd "C:\Root"
:: Loop recusively listing only dcm and raw files.
for /r %%A in (*.dcm *.raw) do call :FindMoveTo "patient id" "%%~dpnA" "%UserProfile%\Desktop"
:: Pause the script to review the results.
pause
goto End

:FindMoveTo <Term> <File> <Target>
:: Look for the search term inside the current file. /i means case insensitive.
find /c /i "%~1" "%~2" > nul
:: Copy the file since it contains the search term to the Target directory.
if %ErrorLevel% EQU 0 xcopy "%~2" "%~3\" /c /i /y
goto :eof

:End

方法2 :(由于FINDSTR /s bug而不推荐)

@echo off
for /f "usebackq delims=" %%A in (`findstr /s /i /m /c:"patient id" *.dcm`) do xcopy "%%~dpnA" "%UserProfile%\Desktop\" /c /i /y
for /f "usebackq delims=" %%A in (`findstr /s /i /m /c:"patient id" *.raw`) do xcopy "%%~dpnA" "%UserProfile%\Desktop\" /c /i /y
pause
相关问题