1

I am a complete novice with scripting. I was wondering if someone would help me creating a script. The script that I am looking for is a a batch file to perform find and move process. The find would be searching for a text string (example patient ID) on dicom files. Also, the find would need to search in subfolders as well. In addition the file extensions that the find would be lookin is .dcm or .raw. Once the find was completed and found the files that contained the text string. I would like the script then copy the files it found to the desktop. Any help on this would be greatly appreciated.

  • 1
    pretty similar to http://stackoverflow.com/questions/8750206/vbscript-to-find-and-move-files-automatically then? –  Dec 13 '12 at 16:54

2 Answers2

3
setlocal enabledelayedexpansion
for /r C:\folder %%a in (*.dcm *.raw) do (
find "yourstring" "%%a"
if !errorlevel!==0 copy "%%a" "%homepath%\Desktop" /y
)
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • 2
    +1 - You probably want to suppress output of FIND. Also no need for delayed expansion. The entire single line script could be `for /r C:\folder %%a in (*.dcm *.raw) do >nul find "yourstring" "%%a" && copy "%%a" "%homepath%\Desktop" /y` – dbenham Dec 13 '12 at 19:02
  • Thanks, yeah that one liner is great, it saves the need for delayed expansion. – Bali C Dec 13 '12 at 20:40
1

This should do it for you. To see all of the options available for each command type command /? in the command line.

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

Method 1: (Recommended)

:: 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

Method 2: (Not Recommended due to 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
Community
  • 1
  • 1
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • 1
    Method 2 can give incorrect results because of a nasty FINDSTR bug concerning short 8.3 file names. See [What are the undocumented features and limitations of the Windows FINDSTR command?](http://stackoverflow.com/q/8844868/1012053) for more information. – dbenham Dec 13 '12 at 18:59
  • @dbenham Thanks, I have not seen that post. I will add that to my answer. – David Ruhmann Dec 13 '12 at 19:21