2012-12-13 38 views
1

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

+1

漂亮类似于http:// stackoverflow.com/questions/8750206/vbscript-to-find-and-move-文件 - 自动呢? – RhysW

回答

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 
+1

方法2可能会给出不正确的结果,因为有关8.3短文件名的讨厌的FINDSTR错误。有关更多信息,请参见[Windows FINDSTR命令的未记录功能和限制?](http://stackoverflow.com/q/8844868/1012053)。 – dbenham

+0

@dbenham谢谢,我还没有看到这篇文章。我会将其添加到我的答案中。 –

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

+1 - 您可能想要抑制FIND的输出。也不需要延迟扩展。 (* .dcm * .raw)do> nul找到“yourstring”“%% a”&& copy“%% a”“%homepath% \桌面“/ y' – dbenham

+0

谢谢,是的,一个班轮是伟大的,它节省了延迟扩张的需要。 –