1.

Solve : need batch script to Keep Certain data from a text file.?

Answer»

Thank you all for you help again. I have one more question i was TRYING to use the path where the file is stored. and the script just quits instantly. example below..




Code: [Select]rem trying to change..
for /f "delims=" %%a in (before.txt) do (

rem to this...
for /f "delims=" %%a in (U:\task\elect input\before.txt) do (
rem but it doesnt work

[/code]A path or filename with spaces must be enclosed in quotes.

for /f "delims=" %%a in ("U:\task\elect input\before.txt") do (still did not work with quotes Are you quite sure the path is right?
i just double checked and the path is 100% correct. here's what i tried...
Code: [Select]

rem I tried this...(this open the actual text file and display it on my computer but once i hit enter to continue it closes.
for /f "delims=" %%a in ('"U:\task\elect input\before.txt"') do (

rem this closes cmd window once i hit enter to continue
for /f "delims=" %%a in ("U:\task\elect input\before.txt") do (
(1)

for /f "delims=" %a in ('type "U:\task\elect input\before.txt"') do

(2)

for /f "usebackq delims=" %a in ("U:\task\elect input\before.txt") do


thank you so much Salmon the 'type method worked perfectEDIT: It was already solved on page two.
I know this an old post but I'm currently expericencing an issue with the script foxidrive
supplied. I'm sure his script is perfect but when i first POSTED this thread i didnt realize there were blank lines sometimes under EMP NAME. and the EMP NAME section must be 4 lines.

The issue:
After the EMP NAME line the next line is sometimes blank and other TIMES has data. I know why its not capturing (there are no spaces in that line so it seem like it doesnt take it to account if there isnt at least a SPACE) i just cant figure out how to capture it. when the new file is created it elimnates the blank line completly. I need the format of the file to be 1:1. any help would be great. thanks.

attached is the new example file which has blank lines after EMP NAME. and below is the script im using.

Code: [Select]@echo on
setlocal EnableDelayedExpansion
set "outputfile=outputfile.txt"
set "flag="
set "print="
for /f "delims=" %%a in (before.txt) do (

set "var=%%a"

if "!var:~0,11!"==" PT NAME" (
set "flag="
set "print="
set "header=%%a"
)


if defined flag (
set "flag="
if "!var:~43,1!"=="9" set print=1
if not "!var:~51,7!"=="X TO BD" set "print="
if defined print >> "%outputfile%" echo.
if defined print >> "%outputfile%" echo.!header!
)

if defined print (
>> "%outputfile%" echo.%%a
)



if "!var:~0,11!"==" PT NAME" (
set flag=1
set "print="
set "header=%%a"
)

)
pause


[recovering disk space, attachment deleted by admin]This preserves all blank lines in the file - and uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855

There are some comments in the code and some simplifications to the original code.

Code: [Select]@echo off
setlocal EnableDelayedExpansion
set "outputfile=outputfile.txt"
del "%outputfile%" 2>nul
set "flag="
set "print="

REM replace blank lines with "|||"
type before.txt |repl "^$" "|||" >before.tmp

for /f "delims=" %%a in (before.tmp) do (

set "var=%%a"

REM check for the first line in a record
if "!var:~0,11!"==" PT NAME" (
set "flag="
set "print="
set "header=%%a"
)

REM on the second line of a record check for the required items
REM and enable printing if found - and print the header
if defined flag (
set "flag="
if "!var:~43,1!"=="9" if "!var:~51,7!"=="X TO BD" set "print=1"
if defined print >> "%outputfile%" echo.
if defined print >> "%outputfile%" echo.!header!
)

REM print every other line in a record that is to be printed
if defined print >> "%outputfile%" echo.%%a


REM turn on the flag that tells the code that the next line is
REM to be checked - to SEE if the record is to be printed
if "!var:~0,11!"==" PT NAME" (
set flag=1
set "print="
)

)

REM remove the "|||" so that the lines are blank again
type "%outputfile%" |repl "^\|\|\|$" "" >tmp.tmp
move /y tmp.tmp "%outputfile%" >nul
del before.tmp
pauseThanks again foxidrive.



Discussion

No Comment Found