Computing.Net > Forums > Disk Operating System > Find text using Dos Batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Find text using Dos Batch

Reply to Message Icon

Name: JB
Date: December 27, 2002 at 11:42:03 Pacific
OS: Windows 2000
CPU/Ram: P4 1.7 Ghz 512 MB
Comment:

Hey All,

Thanks in advance for any help.

Is it possible to write a script in a DOS batch file that will read a text file for a string of characters, then if the string exists within the file, copy the file to a Path A, if not Copy the file into Location B.

Ex.
I want to search Batchtest.dat for the character string "TEST" on line 5, column 6
If "TEST" exist, copy Batchtest.dat to C:\TRUE, if false copy Batchtest.dat to C:\FALSE.

Hope this isnt to vague. Thanks again for any help you can provide




Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: December 27, 2002 at 13:36:23 Pacific
Reply:

I hope the following helps you:

@Echo Off

:: TSTSTR.BAT Batch to scan a text file for a string
:: Syntax: TSTSTR string [pathname]filename

:: Use Find /I if you want to ignore low/uppcase
Echo.
Find "%1" %2 > Nul
If ErrorLevel 1 GoTo FALSE

:TRUE
Copy %2 C:\True > Nul
Echo %1 Found: %2 copied to C:\TRUE
GoTo EXIT

:FALSE
Copy %2 C:\False > Nul
Echo %1 not Found: %2 copied to C:\FALSE
GoTo EXIT

:EXIT
Echo.


0

Response Number 2
Name: Secret_Doom
Date: December 27, 2002 at 21:07:10 Pacific
Reply:

All the task JB described is simple, but for the part he says "on line 5, column 6". That makes it much more complicated. IVO's script can perform the task only if you disregard this offset search.

This much more complex file is the approach I could get to regard the line and the column of the string:

===== BATCH SCRIPT BEGIN =====
@echo off
if "%1"=="GoTo" goto %2
%comspec% /V:ON /C %0 GoTo start
goto:eof
:start

set LINE=0
for /F "delims=" %%L in (batchtest.dat) do (
        set /A LINE += 1
        if "!LINE!" == "5" (
                set STR=%%L
                echo.!STR:~5!> %TEMP%.\T1.DAT
                FINDSTR /B /C:"TEST" %TEMP%.\T1.DAT > nul
                del %TEMP%.\T1.DAT
                if not errorlevel=1 (
                        copy batchtest.dat C:\TRUE
                ) else (
                        copy batchtest.dat C:\FALSE
                )
                goto forend
        )
)
:forend
set LINE=
set STR=

:eof
===== BATCH SCRIPT END =====

That script was NOT tested under Win2000, only on WinXP. I'm not sure it will work under Win2000, but I think it will.

The red parts are the setting you may change. The 5 (five) on the following line:

echo.!STR:~5!> %TEMP%.\T1.DAT

Is the column where to look for the string less one. In order to look for string 6, put the number 5. The rest of the settings are pretty obvious.

The search is case-sensitive. To make it insensitive, add the /I switch on the following line:

FINDSTR /B /C:"TEST" %TEMP%.\T1.DAT > nul
FINDSTR /I /B /C:"TEST" %TEMP%.\T1.DAT > nul

One more detail: it skips blank lines on the counting. That means the fifth line on a file which begins with a blank line is considered the fourth.

The script is not user-friendly like IVO's, but it didn't seem to need to be. Nevertheless, you can make it more user-friendly if you need to.

-- Leonardo Pignataro - Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

_______________________________________________________


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Find text using Dos Batch

Inputing windows text into DOS batch fil www.computing.net/answers/dos/inputing-windows-text-into-dos-batch-fil/6335.html

DOS Batch Programming - Dynamic assign filename? www.computing.net/answers/dos/dos-batch-programming-dynamic-assign-filename/5488.html

performing a find in a DOS Batch www.computing.net/answers/dos/performing-a-find-in-a-dos-batch/14094.html