Jump to content
Macro Express Forums

Delete Last Character at the end of the file


Recommended Posts

I can think of several ways to do this. Here are three.

 

If I remember correctly, the EOF character is 1A hex or 26 decimal. If the EOF character only exists at the end of the file then this technique will work. It deletes the EOF character by replacing it with nothing.

// Method 1 - Replace EOF character with nothing.
Variable Set to ASCII Char 26 to %EOF%
Variable Set String set %FileContent% to the contents of c:\InFile.txt
Variable Modify String: Replace "%EOF%" in %FileContent% with ""
Variable Modify String: Save %FileContent% to "c:\OutFile.txt"

If you are not sure of the EOF character or whether it exists more than once in the file you may want to try Method 2 or 3. Method 2 deletes the last character.

// Method 2 - Copy all but last character using Delete
Variable Set String set %FileContent% to the contents of c:\InFile.txt
Variable Set Integer %Length% to the length of variable %FileContent%
Variable Modify Integer %Length%: Decrement
Variable Modify String %FileContent%: Delete a substring starting at %Length% and 1 characters long
Variable Modify String: Save %FileContent% to "c:\OutFile.txt"

Method 3 copies all but the last character.

// Method 3 - Copy all but last character using Copy
Variable Set String set %FileContent% to the contents of c:\InFile.txt
Variable Set Integer %Length% to the length of variable %FileContent%
Variable Modify Integer %Length%: Decrement
Variable Modify String: Copy a substring in %FileContent%, starting at 1 and %Length% characters long to %FileContentOut%
Variable Modify String: Save %FileContentOut% to "c:\OutFile.txt"

To experiment with each of these copy the following code and paste it into a macro:

<COMMENT/>
<COMMENT/>
<COMMENT Value="Method 1 - Replace EOF character with nothing."/>
<VARIABLE SET TO ASCII CHAR Value="26" Destination="%EOF%"/>
<VARIABLE SET STRING Option="\x03" Destination="%FileContent%" Filename="c:\\InFile.txt" Strip="FALSE"/>
<VARIABLE MODIFY STRING Option="\x0F" Destination="%FileContent%" ToReplace="%EOF%" All="FALSE" IgnoreCase="FALSE"/>
<VARIABLE MODIFY STRING Option="\x11" Destination="%FileContent%" Filename="c:\\OutFile.txt" CRLF="FALSE"/>
<COMMENT/>
<COMMENT/>
<COMMENT Value="Method 2 - Copy all but last character using Delete"/>
<VARIABLE SET STRING Option="\x03" Destination="%FileContent%" Filename="c:\\InFile.txt" Strip="FALSE"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%Length%" Text_Variable="%FileContent%"/>
<VARIABLE MODIFY INTEGER Option="\x08" Destination="%Length%"/>
<VARIABLE MODIFY STRING Option="\x0A" Destination="%FileContent%" Start="%Length%" Count="1"/>
<VARIABLE MODIFY STRING Option="\x11" Destination="%FileContent%" Filename="c:\\OutFile.txt" CRLF="FALSE"/>
<COMMENT/>
<COMMENT/>
<COMMENT Value="Method 3 - Copy all but last character using Copy"/>
<VARIABLE SET STRING Option="\x03" Destination="%FileContent%" Filename="c:\\InFile.txt" Strip="FALSE"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%Length%" Text_Variable="%FileContent%"/>
<VARIABLE MODIFY INTEGER Option="\x08" Destination="%Length%"/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%FileContentOut%" Variable="%FileContent%" Start="1" Count="%Length%"/>
<VARIABLE MODIFY STRING Option="\x11" Destination="%FileContentOut%" Filename="c:\\OutFile.txt" CRLF="FALSE"/>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...