Sign in to follow this  
Followers 0
Jat421

Delete Line from text File

5 posts in this topic

Hi,

I have a function that reads a line from a text file. Now the issue I am having is that once that line is read I want to delete that line from the file. Below is what I have so far, but it doesn't seem to work. I am using the WinAPI because I want to lock the text file while reading it. Thanks for any help!!

Func ReadWrite()            
    $tBuffer = DllStructCreate("byte[16]")
    $hFile = _WinAPI_CreateFile($File1, 2, 6, 0)
    _WinAPI_SetFilePointer($hFile, 0)
    _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 16, $nBytes)    
    
    
    $sText = ""
    $wBuffer = DllStructCreate("byte[" & StringLen($sText) & "]")
    DllStructSetData($wBuffer, 1, $sText)
    $hFile = _WinAPI_CreateFile($hFile, 2, 6, 0)
    _WinAPI_WriteFile($hFile, DllStructGetPtr($wBuffer), StringLen($sText) , $nBytes)
    
    _WinAPI_CloseHandle($hFile)
    $sText = BinaryToString(DllStructGetData($tBuffer, 1))
    ClipPut($sText)                                         
    $c = @UserName                                          
    $k = _FileWriteLog($File2, " " & $stext & " " & $c)     
    MsgBox(0, "Number is", $sText)                      
EndFunc

The File1 looks like this

1234567891234567
1234567891234568
1234567891234569
1234567891234566
1234567891234562

Share this post


Link to post
Share on other sites



See this post it can help you

for doing same thing by another way ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Hi, Thanks for the link. But I believe I will not be able to pass the handle to that _FileLineDelete() in the UDF. I can probably do it by writing a different function with this UDF but then the text file will not be locked when I am writing to it. I would like to lock the text file while it's been read and written to. Anything else you can suggest. Thanks for your help!!

Share this post


Link to post
Share on other sites

Is the _ReplaceStringInFile( ) function could help you ?


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

I will give that a try but I have a feeling that might not work. I will I need a WinAPI call to get the handle. Thanks!

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