WriteLn is great but...
Moderators: JRL, Dorian (MJT support)
- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
WriteLn is great but...
Would it be possible to have a "WriteFile" command that is writing the entire file and not appending a line. I find myself deleting files just to write them again. The problem with this approach is that the OS will know that these are two different files and Dropbox (and other) will treat the updated file as a new one.
- Dorian (MJT support)
- Automation Wizard
- Posts: 1414
- Joined: Sun Nov 03, 2002 3:19 am
Re: WriteLn is great but...
I'll mark this for Marcus, and we'll find out if it's feasible.
Re: WriteLn is great but...
Use the variable WLN_NOCRLF. You can successfully recreate a perfect copy of any file by simply setting WLN_NOCRLF equal to 1. Just remember to set it back to 0 when you're done.
Code: Select all
Readfile>mypath\myexecutable.exe,vData
Let>WLN_NOCRLF=1
Writeln>mynewpath\mynewexecutable.exe,wres,vData
Let>WLN_NOCRLF=0
- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: WriteLn is great but...
Thanks JRL but that is not what I'm asking for. The WLN_NOCRLF=1 will still append the text, with the exception that it will not make it a new line. I want a command that writes the entire file, without the need to delete it.
As shown in this example (what happens with the LN_NOCRLF set to 1):
Will have a file with the contents of:
As shown in this example (what happens with the LN_NOCRLF set to 1):
Code: Select all
IfFileExists>%SCRIPT_DIR%\text_file.txt
DeleteFile>%SCRIPT_DIR%\text_file.txt
Endif>
Let>k=0
While>k<10
Add>k,1
WriteLn>%SCRIPT_DIR%\text_file.txt,,WLN_NOCRLF=0 %k%
EndWhile>
Let>WLN_NOCRLF=1
Let>k=0
While>k<10
Add>k,1
WriteLn>%SCRIPT_DIR%\text_file.txt,,WLN_NOCRLF=1 %k%
EndWhile>
ExecuteFile>%SCRIPT_DIR%\text_file.txt
Code: Select all
WLN_NOCRLF=0 1
WLN_NOCRLF=0 2
WLN_NOCRLF=0 3
WLN_NOCRLF=0 4
WLN_NOCRLF=0 5
WLN_NOCRLF=0 6
WLN_NOCRLF=0 7
WLN_NOCRLF=0 8
WLN_NOCRLF=0 9
WLN_NOCRLF=0 10
WLN_NOCRLF=1 1WLN_NOCRLF=1 2WLN_NOCRLF=1 3WLN_NOCRLF=1 4WLN_NOCRLF=1 5WLN_NOCRLF=1 6WLN_NOCRLF=1 7WLN_NOCRLF=1 8WLN_NOCRLF=1 9WLN_NOCRLF=1 10
Re: WriteLn is great but...
Sorry for being dense but I still don't understand what you want to accomplish. Given your example of output you don't want, what output do you want?
I don't use dropbox. Does it create a whole new file if you add a line to an existing file?
I don't use dropbox. Does it create a whole new file if you add a line to an existing file?
- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: WriteLn is great but...
I want to update the entire file's contents.
Let's say I have file X which got a whole bunch of text in it. I read the file and use regex to search and replace some parts of the text. All is working just fine with current commands in MS. But now that I want to update the file X with the new contents I need to delete the file and write a new one.
Why this is a problem, the new file will not be the old file even if the path and name is the same. Dropbox will not add the updated file to the old file's version history (this is only one example). Another problem is how a webserver might index the files, a delete of a file will make the server remove it from it's memory and the subsequent re-create of a file with the exact name will trigger the server to throw an error. More implications all have similar problems.
I hope I make myself clear
Let's say I have file X which got a whole bunch of text in it. I read the file and use regex to search and replace some parts of the text. All is working just fine with current commands in MS. But now that I want to update the file X with the new contents I need to delete the file and write a new one.
Why this is a problem, the new file will not be the old file even if the path and name is the same. Dropbox will not add the updated file to the old file's version history (this is only one example). Another problem is how a webserver might index the files, a delete of a file will make the server remove it from it's memory and the subsequent re-create of a file with the exact name will trigger the server to throw an error. More implications all have similar problems.
I hope I make myself clear

- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: WriteLn is great but...
Made up code of what I want:
The text_file.txt containing this once done:
But it would have been updated with 1...9 during the iteration...
Code: Select all
Let>k=0
While>k<10
Add>k,1
WriteFile>%SCRIPT_DIR%\text_file.txt,,%k%
EndWhile>
Code: Select all
10
Re: WriteLn is great but...
Very clear, I think
I await Marcus' response.
Again, sorry for being dense but this an issue I've never confronted.
You posted an example since I started typing. It appears that what you want is to have a function that somehow keeps a file but allows for deletion of the contents and lets you then enter new contents
Seems to me to be the functional equivalent of opening an existing file with an application such as notepad, selecting all, pressing delete, then entering new contents and doing a close and save.
I wouldn't know how to do that using current functions. Thank you for clarifying.
I await Marcus' response.
Again, sorry for being dense but this an issue I've never confronted.
You posted an example since I started typing. It appears that what you want is to have a function that somehow keeps a file but allows for deletion of the contents and lets you then enter new contents
Seems to me to be the functional equivalent of opening an existing file with an application such as notepad, selecting all, pressing delete, then entering new contents and doing a close and save.
I wouldn't know how to do that using current functions. Thank you for clarifying.
- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: WriteLn is great but...
Yes exactly how the MS editor is doing it, thanks for the feedbackSeems to me to be the functional equivalent of opening an existing file with an application such as notepad, selecting all, pressing delete, then entering new contents and doing a close and save.

Re: WriteLn is great but...
Just curious if the following does what you want. It eliminates the need to delete but I don't know if the files are perceived as separate distinct files or a single file being edited.
Code: Select all
Let>RP_Wait=1
LEt>RP_Windowmode=0
Let>k=0
While>k<10
Add>k,1
RunProgram>cmd /c echo %k% > %SCRIPT_DIR%\text_file.txt
EndWhile>
- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: WriteLn is great but...
Yeah for that one example it does the job but not for a LaTeX source code document which got 10 000+ characters and special characters etc.
And, the command line is limited to 8191 characters: https://docs.microsoft.com/en-us/troubl ... limitation
And, the command line is limited to 8191 characters: https://docs.microsoft.com/en-us/troubl ... limitation
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Re: WriteLn is great but...
Just DeleteFile and then WriteLn with WLN_NOCRLF set (if you don't want it adding a line break at the end).
Really this is all any WriteFile command would do anyway. So why not make a subroutine:
Call it with:
Really this is all any WriteFile command would do anyway. So why not make a subroutine:
Code: Select all
SRT>WriteFile
DeleteFile>WriteFile_Var_1
Let>WLN_NOCRLF=1
WriteLn>WriteFile_Var_1,wres,WriteFile_Var_2
Let>WLN_NOCRLF=0
END>WriteFile
Code: Select all
GoSub>WriteFile,c:\myfile.txt,data_to_write
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: WriteLn is great but...
Ok but that's not how MS editor is doing it... Plus:
Why this is a problem, the new file will not be the old file even if the path and name is the same.
- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: WriteLn is great but...
Here's a big issue with the delete file vs update a file: viewtopic.php?f=2&t=10852
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Re: WriteLn is great but...
Anything that 'rewrites' a file actually deletes it and creates a new one. There's no such thing as rewriting a file. You either create a new one then delete the old one and rename the new one or you delete the old one and write out the new one with the same name. Whatever way we do this it would still follow the same technique as the subroutine I wrote you. Even if we hid all that from you and did it "internally" it would still work that way - delete old, save new.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?