• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

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
0
WvR
Asked:
WvR
1 Solution
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now