Replacing .txt file header

February 18, 2009 at 12:42:02
Specs: Windows XP
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?


See More: Replacing .txt file header

Report •


#1
February 18, 2009 at 13:36:24
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;
}


Report •

#2
February 18, 2009 at 16:19:57
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


Report •

#3
February 19, 2009 at 12:25:36
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!

Report •

Related Solutions

#4
February 19, 2009 at 14:29:52
Paste in the batch as it now stands.


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

M2


Report •

#5
February 19, 2009 at 15:12:50
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 #*


Report •

#6
February 19, 2009 at 15:24:57
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


Report •

#7
February 20, 2009 at 09:26:12
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 #*

)
)


Report •

#8
February 21, 2009 at 02:42:27
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


Report •

#9
February 21, 2009 at 13:11:44
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!

Report •


Ask Question