Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

StringGetPos problem creating new file


  • Please log in to reply
7 replies to this topic
newpie
  • Members
  • 231 posts
  • Last active: Oct 17 2011 01:44 AM
  • Joined: 16 Nov 2007
Hello I am using a modified version of Titan's code which is below, which is supposed to delete the first 3 lines of the text file and then make a new test file with the remaining lines:

Loop 1
{
FileRead, wsx, C:\test.txt
StringGetPos, line3, wsx,`n, L3 
StringTrimLeft, new, wsx, line3
FileDelete, C:\test.txt
Sleep, 200
FileAppend, %new%, C:\test.txt
sleep, 200
}
return

The problem comes in using this example text file below:

one
two
three
four
five
six
seven
eight
nine

When I run the script the first time it gives me the following new text file:

[]four
five
six
seven
eight
nine


Well, the little character , [] , disables the script somehow so it doesn't modify the text file the next time I run the script to make:

seven
eight
nine


Any ideas why it is creating the symbol []. Thanks for any help.

evan
  • Guests
  • Last active:
  • Joined: --
i got one similar script:
Loop, read, test.txt
{
    if A_index > 3
    {
    if content = 
    content = %A_LoopReadLine%
    else
    content = %content%`n%A_LoopReadLine%   
    }

}

FileDelete, test.txt
FileAppend, %content%, test.txt


Frankie
  • Members
  • 2930 posts
  • Last active:
  • Joined: 02 Nov 2008
RegExReplace version:
FileRead, File, C:\test.txt

File := RegExReplace(File, "[a-zA-Z0-9]+`n", "", "", 3)

FileAppend, %File%, C:\NewFile.txt


newpie
  • Members
  • 231 posts
  • Last active: Oct 17 2011 01:44 AM
  • Joined: 16 Nov 2007
Thank you for your quick responses.

I tried Frankie's and for some reason it doesn't delete the first three lines, it just copies test ---> newtest, exact duplicate file.

I tried evan's and that does work for the first hotkey push, but not the following ones if because I need to reset A_index to 0 every time it is done with file appending. The reason is I have this bit of code in a bigger script that keeps going.

This is the reason why I like Titan's code, but can't figure out how to get rid of the [] symbol that appears.

Any thoughts. Thanks

Frankie
  • Members
  • 2930 posts
  • Last active:
  • Joined: 02 Nov 2008
FileRead, File, C:\test.txt

File := RegExReplace(File, "[a-zA-Z0-9]+`r`n", "", "", 3)

FileAppend, %File%, C:\NewFile.txt
Sorry forgot the `r. Should work now.

newpie
  • Members
  • 231 posts
  • Last active: Oct 17 2011 01:44 AM
  • Joined: 16 Nov 2007
Thanks Frankie, I think I can make the code work now.
I would it prefer to stay the same name file sort of like this:

FileRead, File, C:\test.txt
File := RegExReplace(File, "[a-zA-Z0-9]+`r`n", "", "", 3)
FileAppend, %File%, C:\test.txt

So since that doesn't work, I will just keep your code as is and after it is done copy the newfile.txt( in the process renaming it to test.txt) and then delete it after I am done with it.

Thanks

Frankie
  • Members
  • 2930 posts
  • Last active:
  • Joined: 02 Nov 2008
FileRead, File, C:\test.txt
;FileDelete, C:\backup.txt
;FileAppend, %File%, C:\backup.txt
File := RegExReplace(File, "[a-zA-Z0-9]+`r`n", "", "", 3)
FileDelete, C:\test.txt
FileAppend, %File%, C:\test.txt
That will completly replace the text file. Uncomment the above lines to backup the file before changing it. Also you can look into FileGetFile to make your script more dynamic.

newpie
  • Members
  • 231 posts
  • Last active: Oct 17 2011 01:44 AM
  • Joined: 16 Nov 2007
Thanks for your help Frankie (and everyone elses).