Link to home
Start Free TrialLog in
Avatar of WvR
WvR

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve Knight
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
Sorry, crossed posts there Steve...
No, that's okay.

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

Both are good answers :)
Avatar of ghostdog74
ghostdog74

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
Avatar of WvR

ASKER

SteveGTR I tryed all solutions, yours worked in one time

Thanks!
Fair enough, win some lose some!

Steve