Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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.

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
:startset 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 > nulOne 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_______________________________________________________

![]() |
![]() |
![]() |

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