Jump to content

FileWrite Challenge


Recommended Posts

Hello :), I wanna insert a backspace in a file (Sorry, I don't know what to say formally)

I tried this:

#include <Array.au3>

Func FileBackspace($sFile, $iLine, $iCount) ; Pretty straight forward name, isn't it?
    $hFile = FileOpen($sFile, 2) ; Open file in over-write mode
    $aFile = FileReadToArray($hFile) ; File to array
    $aFile[$iLine] = StringTrimRight($aFile[$iLine], $iCount) ; Trim the line (NOTE: Backspace means trim starting from right)
    $sFile = _ArrayToString($aFile, @CRLF) ; Store new modifed file in a variable
    FileWrite($hFile, $hFile) ; Hmmmm... What would FileWrite do?
EndFunc

This is not the best way to do this. This will be OK for small files but for files with size of 1 GB!!! I tried hard but I failed to insert backspace without storing the entire file in the RAM

I want a function which does this but without storing the entire file

 

Thanks in advance, TD :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

So you want to remove the last n characters from every line?
You could process the file line by line using FileReadLine and FileWriteLine.
Or process the file in blocks of e.g. 1MB by using FileRead/FileWrite.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

If all lines in the file have the same length then you could calculate the position of this line, read all the data before this line and write it to the output file, read and process the line and write it to the output file, then the rest of the file.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

#include <Array.au3>
#include <File.au3>

Func FileBackspace($sFile, $iLine, $iCount) ; Pretty straight forward name, isn't it?
    $hFile = FileOpen($sFile) ; Open file in read only mode
    $sTempFile = _TempFile() ; Create a temp file
    $hTempFile = FileOpen($sTempFile) ; Open the temp file
    ; Copy contents of original file to temp file until the line to insert backspace is reached
    For $i = 1 To $iLine - 1
        FileWrite($hTempFile, FileReadLine($hFile, $i) & @CRLF)
    Next
    FileWrite($hTempFile, StringTrimRight(FileReadLine($hFile, $iLine), $iCount)) ; Write the modified line
    ; Copy the remaining contents
    For $i = $iLine + 1 To _FileCountLines($sFile)
        FileWrite($hTempFile, FileReadLine($hFile, $i) & @CRLF)
    Next
    FileMove($sTempFile, $sFile, 1) ; Overwite the original file
EndFunc

Like this?

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

From the help file:

"From a performance standpoint it is a bad idea to read line by line specifying "line" parameter whose value is incrementing by one. This forces AutoIt to reread the file from the beginning until it reach the specified line."

So use

#include <Array.au3>
#include <File.au3>

Func FileBackspace($sFile, $iLine, $iCount) ; Pretty straight forward name, isn't it?
    $hFile = FileOpen($sFile) ; Open file in read only mode
    $sTempFile = _TempFile() ; Create a temp file
    $hTempFile = FileOpen($sTempFile) ; Open the temp file
    ; Copy contents of original file to temp file until the line to insert backspace is reached
    For $i = 1 To $iLine - 1
        FileWriteLine($hTempFile, FileReadLine($hFile))
    Next
    FileWriteLine($hTempFile, StringTrimRight(FileReadLine($hFile), $iCount)) ; Write the modified line
    ; Copy the remaining contents
    While True
        $sTemp = FileReadLine($hFile)
        If @error Then ExitLoop    
        FileWriteLine($hTempFile, $sTemp)
    Next
    FileClose($hTempFile)
    FileMove($sTempFile, $sFile, 1) ; Overwite the original file
EndFunc

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

The file is left open (FileOpen), after you use it or update it, a good practice is to close it.

Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Why use file close?

​Because FileMove won't work if the file to be moved is still open (in use) by another application.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

×
×
  • Create New...