add/delete text in txt files with batch

May 17, 2010 at 13:44:56
Specs: Windows 7
hello, i have a lot of txt files with this layout:

TextTextText
number number
number number

whith a lot of number lines and a bad formatting.
I need a batch program that:
-deletes the line with text (the firs row)
-deletes the 5 spaces before the first number
of each line and replaces the 4 spaces
between the 2 numbers of each line with a
single space character
- add a 0 at the end of each line of numbers, obviously with a space before.
the output (overwrited to the original file)
should be like this:
number number 0
number number 0
...
How can i do that? can anyone help me?
thanks


See More: add/delete text in txt files with batch

Report •


#1
May 17, 2010 at 14:16:48
If you store the following batch as e.g. myedt.bat, at prompt type

myedt "PathName/FileName"

Don't miss the double quotes.

@echo off > "%~dpn1.new"
for /F "skip=1 tokens=1,2" %%a in ('type "%~f1"') do (
  echo.%%a %%b 0>> "%~dpn1.new"
)
del "%~f1"
ren "%~dpn1.new" "%~nx1"


Report •

#2
May 17, 2010 at 14:22:58
@echo off>new & setlocal
for %%a in (*.txt) do (
for /f "skip=1 tokens=1-2" %%b in (%%a) do >> new echo %%b %%c 0
more new
pause
@:: move /y new %%a
)
::----- note that file replacement is disabled pending testing!

ps: oops! crossed in the mail.


Report •

#3
May 17, 2010 at 15:44:23
thanks nbrane. on cmd your code does exactly what i want. how
to overwrite the original txt file with the new one, and this for
each txt file? sorry but i don't know anything about batch
programming:)

Report •

Related Solutions

#4
May 17, 2010 at 15:58:10
@:: move /y new %%a
the above line, make it like this:
move /y new %%a

strongly suggest backing up the files to a temp directory
first, in case of problems.
easy enough, from the directory where the files are:
md .\temp
copy *.* temp


Report •

#5
May 17, 2010 at 16:23:02
ok now it's perfect! thank you:)

Report •

Ask Question