Skip to main content
21-Topaz I
October 9, 2015
Solved

Command Line Program in a mapkey

  • October 9, 2015
  • 6 replies
  • 4224 views

Does anyone know how, in a command prompt, to remove the last character in each line of a .txt file?

I need to open a family table instance, do one operation, close that instance then open the next instance and do the same thing.  And so on.

I am combining a mapkey with a batch file and I am close.  But I have to remove the last character in each line and I can't figure out how.


This thread is inactive and closed by the PTC Community Management Team. If you would like to provide a reply and re-open this thread, please notify the moderator and reference the thread. You may also use "Start a topic" button to ask a new question. Please be sure to include what version of the PTC product you are using so another community member knowledgeable about your version may be able to assist.
Best answer by JLG

I saved each page out as an .mht file; hopefully you can read them.

6 replies

24-Ruby III
October 12, 2015

Steve,

you can do following (for example):

1.] install sed (stream editor) ... available at sed for Windows

2.] use command prompt command ... sed "s/.$//" a.txt > b.txt

a.txt ... source file

b.txt ... removed last character in each line

MH

Chris3
21-Topaz I
October 12, 2015

You don't have to install external sfotware. You might be able to do this with a batch, but vbs has a little more utility. In your config.pro, just change the .bat to .vbs and use the code below (with modifications as required)

Set objFS = CreateObject("Scripting.FileSystemObject")

strFile = "c:\test\file.txt"

strTemp = "c:\test\temp.txt"

Set objFile = objFS.OpenTextFile(strFile)

Set objOutFile = objFS.CreateTextFile(strTemp,True)   

Do Until objFile.AtEndOfStream

    strLine = objFile.ReadLine

    ' Remove last character from strLine

     'This removes the carriage return and the last character and then adds the carriage return back

     strFile = Left(strFile, Len(strLine) - 2) & vbCrLf

    objOutFile.Write(strLine)

Loop

objOutFile.Close

objFile.Close

objFS.DeleteFile(strFile)

objFS.MoveFile strTemp,strFile

STEVEG21-Topaz IAuthor
21-Topaz I
October 14, 2015

Martin,

should have mentioned I can't install external software not approved by out IT dept.  But thank you for it.

Chris,

Thank you for the suggestion.  I might try that but I'd prefer straight command line commands.

2-Explorer
October 14, 2015
STEVEG21-Topaz IAuthor
21-Topaz I
October 14, 2015

Unfortunately I do not have access to some sites from work.  Stack Overflow is one of them.

JLG2-ExplorerAnswer
2-Explorer
October 14, 2015
STEVEG21-Topaz IAuthor
21-Topaz I
October 16, 2015

I DID IT!

I tried many different variations of what was on those pages.  But I never tried the example where they used the CALL command.  This is what worked.  Or at least seems to have worked.  I'l know once I get the mapkey and batch file all set.

Thank you so much.

SETLOCAL
for /f "delims=" %%a in (abc.pro) do (
set str=%%a
CALL :remove "%str%"
)
goto EOF


:remove
set str=%str:~0,-3%
echo %str% >> def.pro

:EOF

STEVEG21-Topaz IAuthor
21-Topaz I
October 21, 2015

And just a little more information.

I also got a space at the end with this code.  It's easy to remove that actually.  I did some searching and found the answer.

Remove the spaces between the ">>" in the line echo %str% >> def.pro.  That space adds the space at the end of the line in def.pro.

So now it's:

echo %str%>>def.pro