Delete lines form file using batch

Nikon Sf-210 batch scanning auto slide...
July 30, 2009 at 06:52:33
Specs: Windows XP
I need to delete lines from the text file using DOS batch command starting the line containing text "ABC". I saw several postings here, but none of them will do what I need. Can you please help with the code? Thank you very much.
Yelena

See More: Delete lines form file using batch

Report •


#1
July 30, 2009 at 07:00:33
Do you want to delete all lines starting with "ABC" or the lines that follow the row starting with "ABC"?

Report •

#2
July 30, 2009 at 07:49:03
I want to delete all lines following and including line containing "ABC"

Report •

#3
July 30, 2009 at 12:38:29
:: MYDEL.BAT Usage: mydel PathName\FileName
@echo off & setlocal EnableDelayedExpansion
type nul > output.txt
for /F "delims=" %%j in ('type "%*"') do (
  set line=%%j
  set line=!line:ABC=!
  if not "!line!"=="%%j" set Flag=ON
  if not defined Flag echo.%%j>> output.txt
)
:: End_Of_Batch

Example: mydel C:\Documents and Setings\My Data\Myfile.txt
returns the filtered output in output.txt in the current folder.

Report •

Related Solutions

#4
July 30, 2009 at 13:41:34
Thanks a lot. It works just fine. I just realized that I need to remove first 41 lines from the file as well. Plus there will be more than one file and I have to loop through them. Would you be so kind to help me with this?
Many thanks for your help,
Yelena

Report •

#5
July 30, 2009 at 14:02:18
Glad to help. To remove the first 41 lines replace

for /F "delims=" %%j in ('type "%*"') do (
  with
for /F "skip=41 delims=" %%j in ('type "%*"') do (

Pay attention the For /F loop skips the blank lines while reading, so take into account that in your processing.

About processing multiplr files, that can be done, but I need a rule to select the files to be filtered.


Report •

#6
July 31, 2009 at 05:08:59
Thanks again for your help. The rules for the loop are as follows:
The files with extention "*.rep" (text files) are generated in the directory "Dir1". I need to re-format each file and move to "Dir2" and save original in "Dir3".

Many thanks,
Yelena


Report •

#7
July 31, 2009 at 05:49:01
Well Yelena,

stay in touch and I will post the script you need as soon as possible.

Ciao
IVO


Report •

#8
July 31, 2009 at 06:49:14
I'm back, Yelena, and here the script you need.

I named it "myfilter" as it seems more appropriate; set the values for DIR1, DIR2 and DIR3 to your needs and run. I did not test it, so please report if anything goes wrong.

:: MYFILTER.BAT Usage: myfilter
@echo off & setlocal EnableDelayedExpansion

set dir1=Dir1
set dir2=Dir2
set dir3=Dir3

if not exist "%dir2%" md "%dir2%"
if not exist "%dir3%" md "%dir3%"

echo.
echo.  Formatting .rep files
echo.    from "%dir1%"
echo.    to   "%dir2%"
echo.  Please wait...

set N=0
pushd %dir1%
for %%j in (*.rep) do call :FILTER %%j
popd

echo.
echo.  DONE. %N% files processed
goto :EOF

:FILTER
  set /A N+=1
  copy "%*" "%dir3%" > nul
  type nul > "%dir2%\%*"
  for /F "skip=41 delims=" %%k in ('type "%*"') do (
    set line=%%k
    set line=!line:ABC=!
    if not "!line!"=="%%k" goto :EOF
    echo.%%k>> "%dir2%\%*"
  )
goto :EOF
:: End_Of_Batch


Report •

#9
July 31, 2009 at 07:31:28
Ivo, Thanks it works just fine. But when I tryed to modify it a little to do exactly what I need, I've got an error.
please look at modified script below. Could you please tell me what I did wrong?
I did not mention it, but what I need is to loop through the files in Dir1, filter and create a uniform file in Dir2 that will be immediately fetched from Dir2 directory by other software. If file is still there, it means that software is not running and script stops. I do not want to overwrite test.txt created in Dir2.
many thatnks for your help.
Yelena
:: MYFILTER.BAT Usage: myfilter
@echo off & setlocal EnableDelayedExpansion

set dir1=C:\infinity-in
set dir3=C:\infinity-originals
set dir2=C:\infinity-out

if not exist "%dir2%" md "%dir2%"
if not exist "%dir3%" md "%dir3%"

echo.
echo. Formatting .rep files
echo. from "%dir1%"
echo. to "%dir2%"
echo. Please wait...

set N=0
pushd %dir1%
for %%j in (*.rep) do
IF EXIST "%dir2%\test.txt" (
echo "Can not move file to the directory. RTI may not running"
) ELSE (
call :FILTER %%j
popd

echo.
echo. DONE. %N% files processed
)

goto :EOF

:FILTER
set /A N+=1
type nul > "%dir2%\test.txt"
for /F "skip=41 delims=" %%k in ('type "%*"') do (
set line=%%k
set line=!line:Media=!
if not "!line!"=="%%k" goto :EOF
echo.%%k>> "%dir2%\test.txt"
)
move "%*" "%dir3%" > nul
goto :EOF
:: End_Of_Batch


Report •

#10
July 31, 2009 at 08:46:29
Salve Yelena,

the following should behave as you like. The MOVE must be in front of the For /F loop otherwise the command will be never executed due to the GOTO :EOF escape statement.

Report the result, I'm at your side.

set N=0
pushd %dir1%
for %%j in (*.rep) do call :FILTER %%j
popd

echo.
echo. DONE. %N% files processed
goto :EOF

:FILTER
if exist "%dir2%\test.txt" (
  echo "Can not move file "%*" to the directory. RTI may not running"
  goto :EOF
)
set /A N+=1
move "%*" "%dir3%"
type nul > "%dir2%\test.txt"
for /F "skip=41 delims=" %%k in ('type "%*"') do (
  set line=%%k
  set line=!line:Media=!
  if not "!line!"=="%%k" goto :EOF
  echo.%%k>> "%dir2%\test.txt"
)
goto :EOF
:: End_Of_Batch


Report •

#11
July 31, 2009 at 09:21:37
If you need to set up a delay between files processing to be sure the draining routine catches the output, code the following statement as the first line after the label :FILTER

  ping 1.0.0.0 -n 1 -w 5000 > nul

where -w is the time delay in millisecs, i.e. 5000 = 5 secs.

Report •

#12
July 31, 2009 at 10:00:03
It worked exactly as I wanted after I moved
move "%*" "%dir3%"

to after

echo.%%k>> "%dir2%\test.txt"

However it produces messages:
"The system cannot find the file specified"


C:\>myfilter.bat

Formatting .rep files
from "C:\infinity-in"
to "C:\infinity-out"
Please wait...
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.

DONE. 1 files processed

C:\>

Thanks again for your help and time.
Yelena


Report •

#13
July 31, 2009 at 12:39:50
Hi Yelena,

replace the following piece of code as you see below; if you code the MOVE after the ECHO.%%k it is executed for each line copied so after the first time the file is moved and never found again. Carefully evaluate if you need to add the PING statement,as I suggested in post #11, to synchronize your script with the draining routine when multiple files are processed from DIR1.

 if not "!line!"=="%%k" goto :DONE
  echo.%%k>> "%dir2%\test.txt"
)
:DONE
move "%*" "%dir3%"
goto :EOF
:: End_Of_Batch


Report •

#14
August 18, 2009 at 14:40:03
Well, it took me two weeks to realize that I need more filtering on these files. Now on what is left from the file after deleting header and footer, I need:
1. to delete all leading spaces
2. to replace all spaces with ','
2. replace '----------' witth '0'

To change text file like this one:


1 35627.52 0.60 ---------- 245.65 ----------
2 36176.54 1.27 ---------- 249.44 ----------
3 35669.11 1.64 325.62 245.94 2.25
4 35928.33 3.03 35124.00 247.73 242.18
5 35490.21 4.85 33769.26 244.70 232.84


to be like this:


1,35627.52,0.60,0,245.65,0
2,36176.54,1.27,0,249.44,0
3,35669.11,1.64,325.62,245.94,2.25
4,35928.33,3.03,35124.00,247.73,242.18
5,35490.21,4.85,33769.26,244.70,232.84

Could you please help?

Yelena


Report •


Ask Question