# Trim text in a file without storing the contents of anywhere

## Recommended Posts

Hello, I have a simple task today, I am sure that I made a similar post a long time ago... I want trim (delete or remove or wipe) the contents of a file (text or binary) WITHOUT storing the contents of the file anywhere in the script. Here is a text file for the purpose of experimenting: (contents of text.txt)

1234567890

The task is simple, remove "456" from the contents of text.txt WITHOUT storing the contents anywhere! I wanted to post some code but it seems impossible to provide any relevant code this time...

Good luck with the challenge! TD

Edited by TheDcoder
removed "(whole)" from the question

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

##### Share on other sites

What is the purpose of this "challenge"? Why should I use the slow file I/O only?

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

@UEZ Autually it was my problem which I am facing right now...

Why should I use the slow file I/O only?

Because I process *big* binary files (archives to be precise), I need to trim some stuff in it

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

##### Share on other sites

Define "big".

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

Based on the exact criteria for your request.

$f = FileOpen("file.txt", 2) FileWrite($f, "1237890")
FileClose($f) Monkey's are, like, natures humans. #### Share this post ##### Link to post ##### Share on other sites But I always thought this was not possible without direct access to the physical disk and knowing where the file is, on it. Monkey's are, like, natures humans. #### Share this post ##### Link to post ##### Share on other sites J1, helpful as always My UDFs and Tutorials: Spoiler UDFs: Active Directory (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki #### Share this post ##### Link to post ##### Share on other sites Use FileRead in a loop and read the data in chunks by setting parameter "count" to e.g. 64K. Process the variable and write the result to a new file. When finished delete the original file and rename the new one. My UDFs and Tutorials: Spoiler UDFs: Active Directory (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki #### Share this post ##### Link to post ##### Share on other sites Define "big". 10 - 100 MB AutoIt.4.Life Clubrooms - Life is like a Donut (secret key) Spoiler If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!! #### Share this post ##### Link to post ##### Share on other sites You can do this... Local$sEndData = ""
Local $hFile = FileOpen("1.txt", 17) FileSetPos($hFile, 3, 0)
FileSetPos($hFile, 3 + 3, 0)$sEndData = FileRead($hFile) FileSetPos($hFile, 3, 0)
FileSetEnd($hFile) FileWrite($hFile, $sEndData) FileClose($hFile)

Saludos

AutoIt...

##### Share on other sites

100MB isn't too big. You should be able to read and process the whole file in one cycle.

What have you tried so far?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:

##### Share on other sites

Because I process *big* binary files (archives to be precise), I need to trim some stuff in it

What kind of binary archives are you talking about?
Wouldn't changing the content/length destroy the internal structure of the archive?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2019-03-02 - Version 1.3.5.0) - Download - General Help & Support - Example Scripts - Wiki
Outlook Tools (2019-01-22 - Version 0.1.0.0) - Download - General Help & Support
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:

##### Share on other sites

What have you tried so far?

Tried some crazy solutions like passing empty string, Null, Binary 0x000000 to trim the contents, currently I just finished my dinner...

Wouldn't changing the content/length destroy the internal structure of the archive?

I am writing additional information in the header, I tried it with a hex editor, It worked!

I will consider it using if its the most reliable

@Danyfirex Please wait, I am experimenting

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

##### Share on other sites

danyfirex code in form of function

_FileEdit("file.txt", "456")

Func _FileEdit($sFilePath,$sText)
$iPos = StringInStr(FileRead($sFilePath), $sText) -1$iLen = StringLen($sText) Local$sEndData = ""
Local $hFile = FileOpen($sFilePath, 17)
FileSetPos($hFile,$iPos, 0)
FileSetPos($hFile,$iPos + $iLen, 0)$sEndData = FileRead($hFile) FileSetPos($hFile, $iPos, 0) FileSetEnd($hFile)
FileWrite($hFile,$sEndData)
FileClose($hFile) EndFunc ;==>_FileEdit Monkey's are, like, natures humans. #### Share this post ##### Link to post ##### Share on other sites @Danyfirex Did you forgot that you are not allowed to store the contents of the file in a variable? AutoIt.4.Life Clubrooms - Life is like a Donut (secret key) Spoiler If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!! #### Share this post ##### Link to post ##### Share on other sites Unless you're planning on hex editing the disk drive directly, your requirement is never going to be possible. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to. Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude How to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator #### Share this post ##### Link to post ##### Share on other sites @BrewManNH Hmmm.... .... I find water's solution to read the data in chucks now the best. I might reconsider some things tomorrow AutoIt.4.Life Clubrooms - Life is like a Donut (secret key) Spoiler If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!! #### Share this post ##### Link to post ##### Share on other sites When working with big files reading the file in chunks is the way to go. I wrote an AutoIt script at work to manipulate very large (15 to 20 GigaByte) XML files and it works like a charm. It's not difficult and relatively fast. The one case you need to consider that some forget, is where the section of the file that you are interested in falls across the boundary of 2 chunks. The way around this is to save some data from the end of the previous chunck, that is 1 byte shorter than what you are looking for and append the next chunk to this. This will ensure that you have the complete section of interest in one chunk of data. It's also worth experimenting with different chunk sizees to find the optimal size for your particular set up. "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook #### Share this post ##### Link to post ##### Share on other sites Hello, I have a simple task today, I am sure that I made a similar post a long time ago... I want trim (delete or remove or wipe) the contents of a file (text or binary) WITHOUT storing the contents of the file anywhere in the script. When you read data into a variable, that is stored in memory, not the script. Good luck with the challenge! TD If I'm not mistaken this is not a challenge as you're asking for help. #### Share this post ##### Link to post ##### Share on other sites Quote (TheDcoder said): ...in the script. When you read data into a variable, that is stored in memory, not the script. I was referring to that, what is it called?.... uhhhh... .... Ah! Synthetic Sugar!, "in the script" is synthetic sugar for "in the memory" If I'm not mistaken this is not a challenge as you're asking for help. Busted , Yep, I wanted some help AutoIt.4.Life Clubrooms - Life is like a Donut (secret key) Spoiler If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!! #### 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 • ### Similar Content • So basically need to pull text from a .txt file or .sql and use that text to replace something within a different file. First File: jumps Or: Text1 = jumps Text2 = fence Second File: My Dog XXXXX over the YYYYY. After Script: My Dog jumps over the fence I have found a good start, I'm basic programmer at best, I have some experience but this gets out of my wheel house. Any help is appreciated. • By Pike Hey Everyone, I haven't been here in a while and recently picked up my old project. Still an FNG! To the point: I want to be able to push the button and either play an mp3, open a file, or open an exe program (all with the same button). The problem is that I don't know how to code for that at "case 1" Case 1 Local$sRead = IniRead("config.ini", "Config", "One", "Default")
SoundPlay($sRead, @HotKeyPressed) <------------------------------- Run($sRead, @HotKeyPressed)                                                                                 <------------------------------- This 'Run' doesn't work even by itself!
See code below for layout. If you need more information I will attach more code or answer any questions. Any help is greatly appreciated.

Much Respect,
Pike

Func _HandleButton() $iClickCount += 1$iTimer = TimerInit() AdlibRegister("CheckButtonPress", $CheckTime) EndFunc Func CheckButtonPress() If TimerDiff($iTimer) < $CheckTime Or$iTimer = 0 Then Return Switch $iClickCount Case 1 Local$sRead = IniRead("config.ini", "Config", "One", "Default") SoundPlay($sRead, @HotKeyPressed) Run($sRead, @HotKeyPressed) Case 2 MsgBox(0, "INFORMATION:", "Choose the file you want designated to this button") Local $sFileOpenDialog = FileOpenDialog(0, @ScriptDir & "\", "All (*.*)",$FD_FILEMUSTEXIST + $FD_MULTISELECT) If @error Then MsgBox($MB_SYSTEMMODAL, "", "No file(s) were selected.") EndIf Local $sFileName = InputBox("INFORMATION:", "Please Name Your File?" & @CRLF _ & @CRLF _ & "6 Characters or Less", "") IniWrite(@ScriptDir & "\config.ini", "Config", "One",$sFileOpenDialog) IniWrite(@ScriptDir & "\config.ini", "Config", "NameOne", $sFileName) EndSwitch$iClickCount = 0 AdlibUnRegister() EndFunc

• hi dears, i have a question please
am now working for an audio player for the blind users
and i added an option to open a file from the right click context menu.
but here there is a small problem, i hope that you can help me to find a solution for it.
the problem is as follow :
when i added the problem into the context menu it work with successfuly if the user select just one file.
but if the user selected more then one file, here the problem
a multiple sections of the program will open.
my question is :
how i can detect the multiple selection from the context menu?
i know that it so  hard, but i know also that you are harder than it
for that dears please try to give me an solution for that problem.
• By misioooo
Hi!

I am writing some GUI and i need to use images as buttons, but with text on them (text is from INI file).
Part with button looks like this:
$ankieta1 = GUICtrlCreateButton($txtAnk1, 100,350,765,164) GUICtrlSetFont(-1, 22, 800, -1, "Arial") ;GUICtrlSetColor(-1,0xffffff) $hImageBtn_1 = _GUIImageList_Create(765, 164) _GUIImageList_AddBitmap($hImageBtn_1, "c:\path\to\ankieta_clean.bmp") _GUICtrlButton_SetImageList($ankieta1,$hImageBtn_1, 4) GUICtrlSetOnEvent(-1, "On_Ankieta1") The problem - if i uncomment GUICtrlSetColor, color of text changes, but button is just plain gray (no image). How i can change text color not touching image on a button here?
• By vin1
i have a script that selects  a text file and deletes a line (text input required) in the text file selected
i have to make it remove all lines found on a file i name, toRemoveLines.txt
it has to remove lines from all text files found in a folder
this is the script that has to be modified
where it says "select file" it has to be "select folder"
where it says "line text input" it has to be all lines from a text file
#Include <File.au3> Global $success = False$file_name = FileOpenDialog("Select file", @ScriptDir, "All files (*.*)", 1+4) $line_text_input = InputBox("Line's text", "Line must contain following text:", "line contains this text")$file_count_lines = _FileCountLines($file_name) for$i = 0 to $file_count_lines$Lines_text_output = FileReadLine($file_name,$i) if StringInStr($Lines_text_output,$line_text_input) then _FileWriteToLine($file_name,$i, "", 1) $success = True ExitLoop EndIf Next if$success = True Then MsgBox(0, "Success", "Line has been deleted") Else MsgBox(0, "Failure", "Line wasn't found") EndIf
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...