WriteLn is great but...

Ideas for new features & functions

Moderators: Dorian (MJT support), JRL

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

WriteLn is great but...

Post by Grovkillen » Sat Aug 07, 2021 11:55 am

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.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1348
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: WriteLn is great but...

Post by Dorian (MJT support) » Sat Aug 07, 2021 12:05 pm

I'll mark this for Marcus, and we'll find out if it's feasible.
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: WriteLn is great but...

Post by JRL » Mon Aug 09, 2021 4:27 pm

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

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: WriteLn is great but...

Post by Grovkillen » Mon Aug 09, 2021 6:31 pm

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):

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
Will have a file with the contents of:

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
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: WriteLn is great but...

Post by JRL » Mon Aug 09, 2021 6:50 pm

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?

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: WriteLn is great but...

Post by Grovkillen » Mon Aug 09, 2021 7:07 pm

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>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: WriteLn is great but...

Post by Grovkillen » Mon Aug 09, 2021 7:10 pm

Made up code of what I want:

Code: Select all

Let>k=0
While>k<10
  Add>k,1
  WriteFile>%SCRIPT_DIR%\text_file.txt,,%k%
EndWhile>
The text_file.txt containing this once done:

Code: Select all

10
But it would have been updated with 1...9 during the iteration...
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: WriteLn is great but...

Post by JRL » Mon Aug 09, 2021 7:22 pm

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.

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: WriteLn is great but...

Post by Grovkillen » Mon Aug 09, 2021 7:24 pm

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.
Yes exactly how the MS editor is doing it, thanks for the feedback :)
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: WriteLn is great but...

Post by JRL » Mon Aug 09, 2021 7:44 pm

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>

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: WriteLn is great but...

Post by Grovkillen » Tue Aug 10, 2021 8:53 am

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
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: WriteLn is great but...

Post by Marcus Tettmar » Thu Aug 12, 2021 9:52 pm

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:

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
Call it with:

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?

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: WriteLn is great but...

Post by Grovkillen » Fri Aug 13, 2021 1:25 am

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.
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: WriteLn is great but...

Post by Grovkillen » Fri Aug 13, 2021 11:33 am

Here's a big issue with the delete file vs update a file: viewtopic.php?f=2&t=10852
Let>ME=%Script%

Running: 15.0.24
version history

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: WriteLn is great but...

Post by Marcus Tettmar » Mon Aug 16, 2021 2:55 pm

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?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts