Jump to content
Macro Express Forums

Adding a space between characters in a file

Recommended Posts

I have a text file that consists of almost 10,000 lines of text like this:








I need to transform the file into this format:








The approach I took was to use the MEP ASCII File Process command. To begin, I transformed the file into a tab deliminated file by adding a tab to the end of each line. (Easy to do with search and replace.)


After trial and error experimentation, the macro worked. It copies each line to a variable, appends a backslash, and adds a space after each letter.


I am curious to know how other people might add a space after each letter. My code is more convoluted than I expected. Although it was a fun exercise, I now want to learn more elegant ways to accomplish the same thing.

Link to comment
Share on other sites

I wouldn't use ASCII File Process as it's not delimited. And usually I'd use arrays but in this case I think it's more efficient to do it line by line as you have opted. See attached.

// Make special characters and setup
Variable Set to ASCII Char 10 to %LF% // Create a Line Feed
Variable Set to ASCII Char 13 to %CR% // Create a Carrige Return
// Loop thru the file
Variable Set String %FileInput%: Prompt for a filename // Get the file name from user
Text File Begin Process: %FileInput%
  Variable Modify String %Output%: Append Text (%LineInput%\)
  Variable Set Integer %LengthLineInput% to the length of variable %LineInput%
  Repeat Start (Repeat %LengthLineInput% times)
    Variable Modify String: Copy a substring in %LineInput%, starting at %Index% and 1 characters long to %Char%
    If Variable %LengthLineInput% Equals "%Index%"
      Variable Modify String %Output%: Append Text (%Char%%CR%%LF%) // Append the character and EoL
      Variable Modify String %Output%: Append Text (%Char% ) // Append the character and space
    End If
  End Repeat
Text File End Process
// Output the results to file
Variable Set From File path // Break up the input file name
Variable Modify String: Save %Output% to "%FileOutputDrive%%FileOutputPath%%FileOutputName%-Output%FileOutputExtension%"

Space Out.mex

Link to comment
Share on other sites

Cory, thank you for your code! It is, in many respects, similar to what I did: I assigned a line to a text variable, found its length, and used a repeat to build a variable, character by character, inserting a space after each.


I was hoping there might be a string or array manipulating function that would "deconstruct" a variable into characters plus spaces, without the need to loop. But my guess is that MEP does not include this capability.


I outputted each line after it was processed, but MEP froze after processing a few hundred lines. I fixed this by slowing the keystroke rate. But I like your approach of storing everything in a variable. I am going to try it. Any idea of the maximum length of a text variable that MEP can handle? (After performing all the operations on the file, the result consisted of close to 90,000 characters.)

Link to comment
Share on other sites

Here's how I'd do this. I'd write the grunt work in AutoIt as, IMHO, ME is ill-suited to this kind of processing. On my machine it runs in no more than 1 second, while using ME to do this takes many minutes at least (I gave up waiting after a minute).

If you want to use this approach, you edit lines 1 and 2 to contain the name of your input and output files respectively.

Variable Set String %tFileIn% to "D:\Temp\testin.txt"
Variable Set String %tFileOut% to "D:\Temp\testout.txt"
External Script: AutoIt

ME Code

<VARIABLE SET STRING Option="\x00" Destination="%tFileIn%" Value="D:\\Temp\\testin.txt" NoEmbeddedVars="FALSE"/>
<VARIABLE SET STRING Option="\x00" Destination="%tFileOut%" Value="D:\\Temp\\testout.txt" NoEmbeddedVars="FALSE"/>
<EXTERNAL SCRIPT Language="AutoIt" Dest="%tOutput%" Script="#include <Array.au3>\r\n#include <String.au3>\r\n\r\n$filein = FileOpen($CmdLine[1])\r\n$fileout = FileOpen($CmdLine[2], 2)\r\nWhile 1\r\n    $rec = FileReadLine($filein)\r\n    if @error = -1 Then ExitLoop\r\n    $arr = _StringExplode($rec, '')\r\n    $res = _ArrayToString($arr, ' ')\r\n    FileWriteLine($fileout, $rec & \"\\\" & $res & ' ')\r\nWEnd\r\n\r\nFileClose($filein)\r\nFileClose($fileout)" Parameters="%tFileIn% %tFileOut%" Encoding="0"/>

AutoIt code (shown here for completeness, and contained within the ME code above)

#include <Array.au3>
#include <String.au3>

$filein = FileOpen($CmdLine[1])
$fileout = FileOpen($CmdLine[2], 2)
While 1
    $rec = FileReadLine($filein)
    if @error = -1 Then ExitLoop
    $arr = _StringExplode($rec, '')
    $res = _ArrayToString($arr, ' ')
    FileWriteLine($fileout, $rec & "\" & $res & ' ')

Link to comment
Share on other sites

Paul is right, MEP is ill suited. I wanted to include a VB snippet to show how easy it is in VB. I don't use MEP nearly as much as I used to. I taught myself VB.NET and because the majority of the solutions I created were not user interfaced, IE large background grunt programs, MEP's capabilities and performance were lacking. But I would never want them to change. MEP is easy to understand and learn to program and given the target market this is what it should be.


Having said that one reason I didn't use arrays is because they cannot be sized at runtime. IN VB if I split an text into an array the length of the array is set to the number of elements automatically. In MEP one has to guess what the max will ever be and add extra code like counters to process the arrays making rare design flaws impossible to avoid and adds a bunch of inelegant code. I love that MEP has arrays but like many features there seems to be a "yeah but".

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.

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.

  • Create New...