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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
I like your /M solution. It's another option that is simpler than mine.
Both are good answers :)
alternative in vbscript:
On Error Resume Next
Set objFSO=CreateObject("Scrip ting.FileS ystemObjec t")
myFolder = "c:\temp"
Set objFolder = objFSO.GetFolder(myFolder)
For Each myFile In objFolder.Files
If objFSO.GetExtensionName(my File) = "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
On Error Resume Next
Set objFSO=CreateObject("Scrip
myFolder = "c:\temp"
Set objFolder = objFSO.GetFolder(myFolder)
For Each myFile In objFolder.Files
If objFSO.GetExtensionName(my
Set objFile = objFSO.OpenTextFile(myFile
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
ASKER
SteveGTR I tryed all solutions, yours worked in one time
Thanks!
Thanks!
Fair enough, win some lose some!
Steve
Steve
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