FInd in files with dos command and do something

Hi,

I need a dos command which can do some things.
I have a dir c:\temp with 3 files
file1.txt
file2.txt
file3.txt

I need a dos command which can search in those files for the word "BACKORDER"
If the word "BACKORDER" exist in a file, the command has to move the file to an other dir (c:\temp\backorder)

If the word "BACKORDER" is not in the file skip and find in the next file.

Thanks!

HP
LVL 1
WvRAsked:
Who is Participating?
 
SteveGTRCommented:
@echo off

pushd "c:\temp"

for /f "tokens=*" %%a in ('dir /b /a-d 2^>NUL') do find /i "BACKORDER" "%%a" >NUL 2>&1&&move "%%a" "backorder"

popd
0
 
Steve KnightIT ConsultancyCommented:
You can get a list of files with the word in using

findstr /M BACKORDER *.txt

so you could combine that with a FOR command to step down the results and move the file into another directory.  This could be expanded with some error checking but should work as is:

cd \temp
mkdir c:\temp\backorder 2>NUL
rem step through each line returned by findstr and carry out a move command on it..
for /F %%a "tokens=*" in ('findstr /M BACKORDER *.txt 2^>NUL') do move %%a c:\temp\backorder
0
 
Steve KnightIT ConsultancyCommented:
Sorry, crossed posts there Steve...
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
SteveGTRCommented:
No, that's okay.

I like your /M solution. It's another option that is simpler than mine.

Both are good answers :)
0
 
ghostdog74Commented:
alternative in vbscript:

On Error Resume Next
Set objFSO=CreateObject("Scripting.FileSystemObject")
myFolder = "c:\temp"
Set objFolder = objFSO.GetFolder(myFolder)
For Each myFile In objFolder.Files
      If objFSO.GetExtensionName(myFile) = "txt" Then
            Set objFile = objFSO.OpenTextFile(myFile,1)
            Do Until objFile.AtEndOfLine
                line = objFile.ReadLine
                  If InStr(1,line,"BACKORDER") > 0 Then
                        WScript.Echo "found BACKORDER in " ,myFile
                        objFile.Close
                        objFSO.MoveFile myFile, "c:\destination\" & myFile.Name
                        Set objFile=Nothing
                        Exit Do
                  End If
            Loop
      End If
Next

usage: cscript /nologo myscript.vbs
0
 
WvRAuthor Commented:
SteveGTR I tryed all solutions, yours worked in one time

Thanks!
0
 
Steve KnightIT ConsultancyCommented:
Fair enough, win some lose some!

Steve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.