Sign in to follow this  
Followers 0
gpence

Batch file concatenation?

7 posts in this topic

Newbie question: I'm trying to automate a log file creation process. I have a number of batch files which each have records containing .tif paths. What I need to do is open each file and write all of the .tif entries into the log. In an old DOS-based world, I'd use "copy *.txt filedir.log to get each of the .tifs listed in each file. I can also recreate that using:

RunWait(@COMSPEC & " /c copy C:\progra~1\autoit3\examples\helpfile\test\*.txt C:\progra~1\autoit3\examples\helpfile\test\v2_daily.log") to get a file listing.

Now my stumbling block -- I really need to prefix each record with the filename (which is numeric, and exclude the suffix) and date/time stamp (or file create date) Here's a crude diagram:

Currently 2 .txt files result in:

c:\path\to\the\tiffs\1010101.tif <-- from file 2345, 3 .tif entries

c:\path\to\the\tiffs\1010102tif

c:\path\to\the\tiffs\1010103.tif

c:\path\to\the\tiffs\1010104.tif <--- from file 9876, 2 .tif entries

c:\path\to\the\tiffs\1010105.tif

It should now read:

<file> <create date/time> <.tif path>

2345 200507050235959 c:\path\to\the\tiffs\1010101.tif

2345 200507050235959 c:\path\to\the\tiffs\1010102tif

2345 200507050235959 c:\path\to\the\tiffs\1010103.tif

9876 200507060000159 c:\path\to\the\tiffs\1010104.tif

9876 200507060000159 c:\path\to\the\tiffs\1010105.tif

It seems to me I need to read in each filename (into an array?) and store the create date, then somehow concatenate it into something like

$file & " " & $createdate & " " & record_data

Unfortunately, I'm having little luck searching because I don't think I'm using the right terminology...

Any help would be greatly appreciated!

Thanks,

gpence

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I did not test this but..... it should be real close... no syntax errors

#include <GUIConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <String.au3>


    $oFile = FileOpen(@TempDir & "\dir.txt", 0);location of the name of the batch file (2345 from your example)
    $hFile = FileOpen(@TempDir & "\dir1.txt", 0);location of your list file you created with tifs
    $nFile = FileOpen(@TempDir & "\dir2.txt", 1);location of your new file with the file/date/tif info 
    
; Check if file opened for reading OK
    If $hFile = -1 Or $oFile = -1 Or $nFile = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    
; Read in lines of text until the EOF is reached
    While 1
        $sLine = FileReadLine($hFile)
        If @error = -1 Then ExitLoop
        If $sLine <> "" Then
            $result = StringTrimRight( $oFile, 3)
            $time =  FileGetTime( $sLine, 1, 1)
            FileWriteLine ( $nFile, $result & "  " & $time & "  " & $sLine)
            $Msg = GUIGetMsg()
            If $Msg = $GUI_EVENT_CLOSE Then ExitLoop
        EndIf
    WEnd
    FileClose($hFile)
    FileClose($nFile)
    FileClose($oFile)

hope that works for you!!

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

I did not test this but..... it should be real close... no syntax errors

$oFile = FileOpen(@TempDir & "\dir.txt", 0);location of the name of the batch file (2345 from your example)
    $hFile = FileOpen(@TempDir & "\dir1.txt", 0);location of your list file you created with tifs
    $nFile = FileOpen(@TempDir & "\dir2.txt", 1);location of your new file with the file/date/tif info
Thanks Valuater!  

I see one change I'll need to make -- $oFile points to a single batch file, but I'll need to recurse the directory for all of the .txt files, so I'll nest your process inside another While loop to read each batch file and the records inside. I think this is doable! Thanks again!

gpence

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

OK, another minor glitch which I'm not understanding re: FileGetTime

I'm reading in a list of .txt files. In each .txt file is a single line of code (which could conceivably change in the future). I want to create a log file which includes each of the records from each file in the directory. So I created dirlist.dat which has each file name in it. For example:

00011111.txt

00012222.txt

00013333.txt

00014444.txt

00015555.txt

00016666.txt

00017777.txt

00018888.txt

00019999.txt

I have the following:

$i = 1
While $i <> 0
; read the input file's first line    
  $iFile = FileReadLine("test_control\dirlist.dat", $i)    
  If $iFile <> "" Then
    $fName = StringTrimRight($iFile, 4)
    $fTime = FileGetTime($iFile,1,1)
   MsgBox(0,"debug", $fTime)                    ; used only for debugging 
  ; 
  ;  <other lines omitted to save space>
  ;
  EndIf
; if the EOF is reached, exit loop else continue with next record in "dirlist.dat"
  If @error = -1 Then 
      $i = 0
      ExitLoop
  EndIf
  $i = $i + 1
WEnd

In the code snippet above, I'm using

1) a while loop to read each line in dirlist.dat into $iFile

2) StringTrimRight() to trim off the ".txt" and store it in $fName

3) FileGetTime to get the file creation time and store it in $fTime.

4) then I'm concatenating $fName and $fTime to create $fPrefix

In the portion I've omitted, I'm going to open each file in dirlist.dat (using a nested while loop) and concatenate the $fPrefix onto the record in said file to create a record which now looks something like this:

00011111_20050707142100_ABCDEFGHIJKLMNOPQRSTUVWXYZ

^^^^^^^^^^^^^^^^^^^

where this is the $fPrefix

_________________________^^^^^^^^^^^^^^^^^^^^^^

________________________and this is the contents of 00011111.txt record #1

I'll use the nest WHILE loop so that in the future I can handle .txt files with multiple records (lines) in each file.

Unfortunately, my $fPrefix isn't cooperating (because of my ignorance, I'm sure), but I get

00011111_1_

instead of

00011111_20050707142100 (which is YYYYMMDDHHMMSS)

Is the "1" a filehandle? If so, how can I get $fTime to work correctly??

TIA,

gpence

Edited by gpence

Share this post


Link to post
Share on other sites

in your post, I do not see your #4... to check your approach

In the code snippet above, I'm using

1) a while loop to read each line in dirlist.dat into $iFile

2) StringTrimRight() to trim off the ".txt" and store it in $fName

3) FileGetTime to get the file creation time and store it in $fTime.

4) then I'm concatenating $fName and $fTime to create $fPrefix

so, (just guessing) try this

Dim $fPrefix

( under you message box place this line)

$fPrefix = $fName & " " & $fTime & " "

I think thats it

Much Later in your loop, you will need

$Final_Name = $fPrefix & $Your_file_contents

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Hi Valuater,

Sorry, I snipped the code relating to #4 by accident. It read:

$fPrefix = $fName & "_" & $fTime & "_"

With a fresh start today, I discovered the problem. I put in a msgbox to help me in debugging the problem and found that $iFile was pulling correctly, but that FileGetTime was still coming back as "1". Upon re-reading the documentation, I recognized that 1 is a failure to read the time which made me think "hey maybe I need the path!" (DUH) I went back and added a new variable named $fPath and added:

$fPath = "test/" & $iFile

then called

FileGetTime ($fPath,1,1)

and everything worked like a charm!

Thanks again for your help!

gpence

Share this post


Link to post
Share on other sites

*** wooosh **

Yours was a tough one!!!

but, you explained yourself very well

and glad I could help

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0