Jump to content

How do I run a FileInsertText over multiple files?


Go to solution Solved by mikell,

Recommended Posts

Hi all,

I have a bunch of text files that need ":" inserted into line 26, position 19 (each file is the exact same format). The below script works on individual files (thanks to the person who wrote this... I lost your link in my mad researching). However, i have two problems.

1 - i want to paste the script into the folder (it changes) with the text files so users can just run the script and it will run over the files in that folder.

2 - How do i make the script  perform the change in all the *my.hdr files in one operation??? I have tried putting a wildcard in front of the my.hdr but nothing happens. (the my.hdr is constant in all the file names)

3 - which isn't so important... how do i stop the script from putting a ":" if one is already there?

FYI... i am bad at scripting and have researched this for days now and done the tutorial but i still don't get it! Please help.

In the script i don't understand the point of the BUFFER and i don't want the script limited by any admin/owner rights.

FileInsertText("c:tempHDR testrad.hdr", 26,19,":")

; function will insert "$SZTEXT" in file "$SZFILE" at Record "$SZREC" at record position "$SZPOS"

Func FileInsertText($SZFILE,$SZREC,$SZPOS,$SZTEXT)
   If Not FileExists($SZFILE) Then Return
   $SZBUFFER = FileRead($SZFILE,FileGetSize($SZFILE))
   $RECPOS=0          ; holds crlf position
   $RECCNT=0          ; record counter
   $SZBUFFER_New = "" ; new file buffer
   While 1
      $RECPOS = StringInStr($SZBUFFER,@CRLF)
      If $RECPOS > 0 Then
         $RECCNT=$RECCNT+1
         If $RECCNT = $SZREC Then
            If $SZPOS > 0 And $SZPOS < $RECPOS Then                             ; if specified record position is within the record
               $SZBUFFER_New = $SZBUFFER_New & StringLeft($SZBUFFER,$szPOS-1)   ; save bit to $RECPOS in newoutput string
               $SZBUFFER_New = $SZBUFFER_New & $SZTEXT                          ; insert specified text
               $SZBUFFER_New = $SZBUFFER_New & StringTrimLeft($SZBUFFER,$szPOS-1); add remainder of the file
            Else
               $SZBUFFER_New = $SZBUFFER_New & $SZTEXT                          ; insert specified text
               $SZBUFFER_New = $SZBUFFER_New & $SZBUFFER                        ; add remainder of the file
            EndIf
            ExitLoop
         Else
            $SZBUFFER_New = $SZBUFFER_New & StringLeft($SZBUFFER,$RECPOS+1) ; save bit to crlf in newoutput string
            $SZBUFFER = StringTrimLeft($SZBUFFER,$RECPOS+1)           ; trim rest of record after crlf
         EndIf
      Else
         $SZBUFFER_New = $SZBUFFER_New & $SZBUFFER    ; save last record newoutput string
      EndIf  
   Wend
   FileDelete($szFile)
   Return(FileWrite($SZFILE,$SZBUFFER_New))
EndFunc  ;==>FileInsertText

 

[font="'comic sans ms', cursive;"]Philosophical claptrap only adds to the confusion![/font]

Link to comment
Share on other sites

  • Solution

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

$myfolder = FileSelectFolder("folder with hdr files", @scriptdir)  
If $myfolder <> "" Then
   Local $hSearch = FileFindFirstFile($myfolder & "\*.hdr")
   While 1
       $sFileName = FileFindNextFile($hSearch)
       If @error Then ExitLoop
       $line = FileReadLine($myfolder & "\" & $sFileName, 26)
       If StringMid($line, 19, 1) <> ":" Then
            $line = _StringInsert($line, ":", 18)
            _FileWriteToLine($myfolder & "\" & $sFileName, 26, $line, 1)
       EndIf
   Wend
   FileClose($hSearch)
EndIf

You can insert some messageboxes for a more user-friendly execution  :)

Edited by mikell
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...