Computing.Net > Forums > Programming > Replacing .txt file header

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Replacing .txt file header

Reply to Message Icon

Name: krhodes5
Date: February 18, 2009 at 12:42:02 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

Hello. I have a bunch of data files that need I need to reformat the header to. What I need is a batch file that can read through all of the .txt files in a folder, delete the 9 lines of header, and add two rows containing numbers to right above the data. The first of these two numbers is specified in the original header as the 'sample interval' and the second is the number of data points in the file.

Here is the original format:


SOURCE FILE NAME: C:\file.DTA
DATE: Wednesday
TIME: 13:41:45
SAMPLE INTERVAL (Seconds): 0.0000001000
UNITS: volts
CHANNEL NUMBER: 1
HIT NUMBER: 6
TIME OF TEST: 55.8792662500

0.00152588
0.00152588
0.00183105
0.00152588
0.00152588
0.00152588


And this is how I need it to end up:

0.0000001000
6
0.00152588
0.00152588
0.00183105
0.00152588
0.00152588
0.00152588


Each folder I will use the batch file with will have a different number of files and each file will have a different number of data. Can anyone help?



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: February 18, 2009 at 13:36:24 Pacific
Reply:

One of the batch experts (which I'm not) I'm sure will be along shortly to help, but if you're interested in seeing an alternative, here's a Perl solution.

I'm assuming the header is the same format in all files. If it isn't this would need a little adjustment.

#!perl

use strict;
use warnings;
use Tie::File;

my $dir = $ARGV[0];
die "Usage: $0 <directory>\n" unless $dir and -d $dir;

for my $file ( <$dir/*.DTA> ) {
    tie my @file, 'Tie::File', $file or die "can't tie '$file' $!";
    my ($interval) = $file[3] =~ /(\S+)$/;
    my ($hit) = $file[6] =~ /(\d+)$/;
    splice(@file, 0, 9, $interval, $hit);
    untie @file;
}


0

Response Number 2
Name: Mechanix2Go
Date: February 18, 2009 at 16:19:57 Pacific
Reply:

I highly recommend you back up your files first.

You can preview the result in the file #### in each directory.

=============================================
@echo off & setLocal EnableDelayedExpansion

:: edit this line to: pushd d:\files [wherever you files are]

for /f "tokens=* delims= " %%a in ('dir/s/b/ad') do (
pushd "%%a"

for /f "tokens=* delims= " %%t in ('dir/b/od *.txt') do (
set N=0

for /f "tokens=2 delims=:" %%i in (%%t) do (
set /a N+=1
if !N! equ 4 set int=%%i
)

set int=!int:~1,99!
echo !int!> #
@echo off > ###

for /f "skip=8 tokens=* delims= " %%d in (%%t) do (
echo %%d >> ###
)

find /c /v "" < ### > ##
copy /b #+##+### #### > nul

echo copy #### %%t
echo del #*

)
)
)

:: remove the ECHO from the 2 lines above to activate.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 3
Name: krhodes5
Date: February 19, 2009 at 12:25:36 Pacific
Reply:

I tried running the batch but it didn't reformat the files. I changed the second line to the directory where the files are stored and removed the "echo" on the last two lines. The batch runs but nothing seems to happen. Am I missing something? Thanks a lot for the help!


0

Response Number 4
Name: Mechanix2Go
Date: February 19, 2009 at 14:29:52 Pacific
Reply:

Paste in the batch as it now stands.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 5
Name: krhodes5
Date: February 19, 2009 at 15:12:50 Pacific
Reply:

I pasted it in and added my file directory but still no change to the .txt files. I get the creation of the four temporary files in the directory with the .txt files. Here are the temp file contents:

For #

,99

For ##

0


File ### is empty

For ####

,99
0


When I run the batch file the command window displays the following which is repeated for each different file in my targe folder until the scrip is done:


The system can not find the file Run.
The system can not find the file Run.
Copy #### Run with file1.txt
del #*


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: February 19, 2009 at 15:24:57 Pacific
Reply:

Please paste the batch you are using into a reply. By itself; no comments or output.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: krhodes5
Date: February 20, 2009 at 09:26:12 Pacific
Reply:

Ok on my last post I had forgotten to delete the echos at the end. I have been playing with it trying to make it work but no luck yet. I removed the outermost for loop because there are no directories in the ..\Waveforms folder, only text files.


@echo off & setLocal EnableDelayedExpansion

pushd C:\Documents and Settings\k8e\Desktop\Format Test\Waveforms

for /f "tokens=* delims= " %%t in ('dir/b/od *.txt') do (
set N=0

for /f "tokens=2 delims=:" %%i in (%%t) do (
set /a N+=1
if !N! equ 4 set int=%%i
)

set int=!int:~1,99!
echo !int!> #
@echo off > ###

for /f "skip=8 tokens=* delims= " %%d in (%%t) do (
echo %%d >> ###
)

find /c /v "" < ### > ##
copy /b #+##+### #### > nul

copy #### %%t
del #*

)
)


0

Response Number 8
Name: Mechanix2Go
Date: February 21, 2009 at 02:42:27 Pacific
Reply:

I created a directory like yours and it worked.

=============================================
my.txt before
SOURCE FILE NAME: C:\file.DTA
DATE: Wednesday
TIME: 13:41:45
SAMPLE INTERVAL (Seconds): 0.33301111
UNITS: volts
CHANNEL NUMBER: 1
HIT NUMBER: 6
TIME OF TEST: 55.8792662500

0.00152588
0.00152588
0.00183105
0.00152588
0.00152588
==================================
my.txt after
0.33301111
5
0.00152588
0.00152588
0.00183105
0.00152588
0.00152588


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 9
Name: krhodes5
Date: February 21, 2009 at 13:11:44 Pacific
Reply:

I figured out the problem. The files I was trying to reformat with the script are text files but there are spaces in the file names. When I replaced the spaces with underscores everything worked fine. Thanks!


0

Sponsored Link
Ads by Google
Reply to Message Icon

MFC and CListCtrl and add... Need Help



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Replacing .txt file header

batch to delete text file header www.computing.net/answers/programming/batch-to-delete-text-file-header/15107.html

Delete 1st 4 lines & last 8 Lines in TXT File www.computing.net/answers/programming/delete-1st-4-lines-last-8-lines-in-txt-file/19676.html

change values in a txt file www.computing.net/answers/programming/change-values-in-a-txt-file/19842.html