FInd in files with dos command and do something

Posted on 2007-10-02
Last Modified: 2012-06-21

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

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.


Question by:WvR
    LVL 30

    Accepted Solution

    @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"

    LVL 43

    Expert Comment

    by: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
    LVL 43

    Expert Comment

    by:Steve Knight
    Sorry, crossed posts there Steve...
    LVL 30

    Expert Comment

    No, that's okay.

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

    Both are good answers :)
    LVL 9

    Expert Comment

    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
                            objFSO.MoveFile myFile, "c:\destination\" & myFile.Name
                            Set objFile=Nothing
                            Exit Do
                      End If
          End If

    usage: cscript /nologo myscript.vbs
    LVL 1

    Author Comment

    SteveGTR I tryed all solutions, yours worked in one time

    LVL 43

    Expert Comment

    by:Steve Knight
    Fair enough, win some lose some!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now