Jump to content
Sign in to follow this  
jannlee300

write to a file

Recommended Posts

jannlee300

how do i write a "0" to the first 9 lines of a file

Share this post


Link to post
Share on other sites
Valuater

I am guessing you might be interested in...

IniWrite()

IniRead()

If so, See the help file for examples

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
Authenticity

$sFile = @ScriptDir & '\MyTest.txt'
$sText = FileRead($sFile)
$hFile = FileOpen($sFile, 2)

FileWrite($hFile, StringRegExpReplace($sText, '\r\n', '0'&@CRLF, 9))
FileClose($hFile)

#cs
    #### MyTest.txt content: ####       #### MyTest.txt content (modified): ####
        1                                   10
        2                                   20
        3                                   30
        4                                   40
        5                                   50
        6                                   60
        7                                   70
        8                                   80
        9                                   90
        10                                  10
        11                                  11
        12                                  12
        13                                  13
        14                                  14
        15                                  15
#ce

Not a good idea for big files, if applicable.

Share this post


Link to post
Share on other sites
maqleod

alternatively, filewriteline() with a for/next loop would also work, and would be the simplest, but would only work if you needed to write the first 9 lines in order and didn't have to edit specific lines, otherwise the ini functions would be best


[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites
jannlee300

$sFile = @ScriptDir & '\MyTest.txt'
$sText = FileRead($sFile)
$hFile = FileOpen($sFile, 2)

FileWrite($hFile, StringRegExpReplace($sText, '\r\n', '0'&@CRLF, 9))
FileClose($hFile)

#cs
    #### MyTest.txt content: ####       #### MyTest.txt content (modified): ####
        1                                   10
        2                                   20
        3                                   30
        4                                   40
        5                                   50
        6                                   60
        7                                   70
        8                                   80
        9                                   90
        10                                  10
        11                                  11
        12                                  12
        13                                  13
        14                                  14
        15                                  15
#ce

Not a good idea for big files, if applicable.

ok thanks but i dont want to add a 0 i want to over write say a 20 to a 0

Share this post


Link to post
Share on other sites
jannlee300

alternatively, filewriteline() with a for/next loop would also work, and would be the simplest, but would only work if you needed to write the first 9 lines in order and didn't have to edit specific lines, otherwise the ini functions would be best

ok so for the for/next loop where do i put the file name etc?

Share this post


Link to post
Share on other sites
Authenticity

To replace the first 9 lines with '0':

$sFile = @ScriptDir & '\MyTest.txt'
$sText = FileRead($sFile)
$hFile = FileOpen($sFile, 2)

FileWrite($hFile, StringRegExpReplace($sText, '.*\r\n', '0'&@CRLF, 9)) ; '0'
FileClose($hFile)oÝ÷ Ù:+zZqëayø«²ßew¬Â+aßÝýaxºÚ"µÍÌÍÜÑ[HHØÜ  [È ÌÎNÉÌLÓ^UÝ    ÌÎNÂÌÍÜÕ^H[TXY
    ÌÍÜÑ[JBÌÍÚ[HH[SÜ[   ÌÍÜÑ[KB[UÜ]J   ÌÍÚ[KÝ[ÔYÑ^XÙJ   ÌÍÜÕ^   ÌÎNËÌLÜÌLÛÌÎNË    ÌÎNÉÌÎNËJJHÈÝ[Â[PÛÜÙJ   ÌÍÚ[JoÝ÷ ÙK"¢³^ÆÙh¢®¶­sb6æ6ÇVFRfÇC´fÆRæS2fwC° ¤FÒb33c·4fÆRÒ67&DF"fײb33²b3#´×FW7BçGBb33²Âb33c¶ÆæW0¥ôfÆU&VEFô'&b33c·4fÆRÂb33c¶ÆæW2  ¤f÷"b33c¶ÒFò b33c¶ÆæW5²b33c¶ÒÒb33³b33°¤æW@ ¥ôfÆUw&FTg&öÔ'&b33c·4fÆRÂb33c¶ÆæW2Â

Share this post


Link to post
Share on other sites
maqleod

ok so for the for/next loop where do i put the file name etc?

this would require that you have an array setup with the contents, so it may not be ideal for you, but if it is, this is how it would work:

For $i = 1 to 9
FileWriteLine("yourfilename.txt",$linecontents[$i])
Next

[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites
jannlee300

To replace the first 9 lines with '0':

$sFile = @ScriptDir & '\MyTest.txt'
$sText = FileRead($sFile)
$hFile = FileOpen($sFile, 2)

FileWrite($hFile, StringRegExpReplace($sText, '.*\r\n', '0'&@CRLF, 9)) ; '0'
FileClose($hFile)oÝ÷ Ù:+zZqëayø«²ßew¬Â+aßÝýaxºÚ"µÍÌÍÜÑ[HHØÜ  [È ÌÎNÉÌLÓ^UÝ    ÌÎNÂÌÍÜÕ^H[TXY
    ÌÍÜÑ[JBÌÍÚ[HH[SÜ[   ÌÍÜÑ[KB[UÜ]J   ÌÍÚ[KÝ[ÔYÑ^XÙJ   ÌÍÜÕ^   ÌÎNËÌLÜÌLÛÌÎNË    ÌÎNÉÌÎNËJJHÈÝ[Â[PÛÜÙJ   ÌÍÚ[JoÝ÷ ÙK"¢³^ÆÙh¢®¶­sb6æ6ÇVFRfÇC´fÆRæS2fwC° ¤FÒb33c·4fÆRÒ67&DF"fײb33²b3#´×FW7BçGBb33²Âb33c¶ÆæW0¥ôfÆU&VEFô'&b33c·4fÆRÂb33c¶ÆæW2  ¤f÷"b33c¶ÒFò b33c¶ÆæW5²b33c¶ÒÒb33³b33°¤æW@ ¥ôfÆUw&FTg&öÔ'&b33c·4fÆRÂb33c¶ÆæW2Â
thanks for all your help

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.