Jump to content

Amending a text document using file name.


Recommended Posts

Hello

Here is what im doing.

Im trying to modify an attribute within a file. Specifically trying to amend it with a language tag based on the file name. This will be done on multiple files at once and all files will have a unique language id "file_*.ts" (this is a text based file)

Example:

File name = "file_es.ts"

The first three lines of the file =

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE TS>

<TS version="2.0">

I need to be able to take the "es" from the file name "file_es.ts" and modify the <TS version="2.0"> tag with=

<TS version="2.0" language="es" sourcelanguage="en">

(sourcelanguage="en") will always be the same. And there is other content in the file which cannot be modified.

What I have tried (and appologies if im commiting blasphamy with the code, im very new but trying to learn)

--------------------------------------------

$file = FileOpen("test.ts", 2)


If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

    $chars = FileRead($file, 1)


FileWriteLine($file, '<?xml version="1.0" encoding="utf-8"?>')
FileWriteLine($file, '<!DOCTYPE TS>' & @CRLF)
FileWriteLine($file, '<TS version="2.0" language="es" sourcelanguage="en">')
FileWriteLine($file, $chars)

FileClose($file)

----------------------------------------------

used a

$file = FileOpen("test.txt", 2)

to open the file whith read/write... that works pretty good... but falls appart quickly..

I tried using the FileWriteLine however this nukes everything else in the file... i only need to modify the one tag and nothing more. Im not sure if its earsier to do some sort of search and replace function or just to replace the first 3 lines of the doc because they will always be the same. And I feel like im using the FileRead completley in the wrong way...

I have yet to get to a point where it pulls the partial file name into the file... as i am unsure what such a command would be called or how it works... I have scowered AutoIt help and have found nothing which looks appropriate. Is this even possible with AutoIt?

Please, any help and or guidance would be greatly appreciated. I have spent 3 days searching AutoIt help (which is really nice) and the forums but have found nothing helpful. If there is an article please let me know ;)

Link to comment
Share on other sites

Hmm I never new you could open a file in read/write mode.

The helpfile for flag 2 says "2 = Write mode (erase previous contents)"

But remarks also say.

When reading and writing via the same file handle, the FileSetPos() function must be used to update the current file position.

I.d have a play around with that if I were you, else it means reading, and replacing the whole file contents.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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...