DesignCAD 3D Max 2027 box
Now available

DesignCAD 2027

Hatches that follow their boundaries, an on-screen dial to turn your model, and a way to find any command by name, in a clean Windows 11 look.

Running batch file from macro

Started by landonairey, July 11, 2013, 05:20:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

landonairey

Hi everyone,

  I have a macro which extracts all text in a certain layer in a drawing and populates a text file (one piece of text per line). I need to now alphabetize that list so I thought and easy solution would be write a one line batch file like so:

sort <filepath>\filename.txt

This should replace the current text file with an alphabetized version. So to call the batch file at the end of the macro I used:

>RunX
{
   <Filename "<filepath>\filename.bat"
}

I can see the cmd.exe window flash on the screen but when I check the text file there hasn't been any change what so ever. So with this I have two questions:

1. What's going wrong when I call the batch file?
2. How can I send the file name string to the batch file from the macro? (right now the batch file calls and will always call a specific text file name)

thank you!


dcadRob

Hard to tell what is happening since you don't show the exact commands you are using.

You could try it this way:
QuoteRunning a BAT file with the RUN statement

To run a DOS batch file from within a macro, you must run COMMAND.COM to interpret the batch file. It is usually best to use the /C command-line switch for command.com, which forces the DOS window to close when the batch file is done. For example:

run "command.com /c myscript.bat"


or, for Windows NT/2000/XP:

run "cmd.exe /c myscript.bat"

Using the new CmdLine statement to pass command line parameters to a macro or program

You may also pass commandline parameters through the Commandline statement. Repeating the above example for Windows NT/2000/XP:

Cmdline "/c myscript.bat"

' Now open a command prompt and run the above commands

Run "cmd.exe"

Copied from the macro reference.

prl

Seems your DOS "sort" command isn't correct.  You are outputting the formatted file to the screen.

Try

sort <filepath>\filename.txt /O <filepath>\filename.txt


prl

In terms of running the above from a macro, do something like this:

'Program: sort.d3m

'assumes you have a text file called dcad.txt located where indicated.

cmdline "/c SORT v:\dcad.txt /O v:\dcad.txt"
run "cmd.exe"

End



landonairey

Thanks for the quick responses.

dcadRob - Thanks, I've got that exact ref man opened up but my version of dcad won't allow the cmdline command. Any other work arounds?

prl - good point I was wrong at first, this line :sort <filepath>\filename.txt /O <filepath>\filename.txt does work.

If I can't pass the name of the file from the macro to the batch file then this won't be too useful

prl

#5
Quote from: landonairey on July 11, 2013, 06:54:06 AM
If I can't pass the name of the file from the macro to the batch file then this won't be too useful

The macro code is given too, two lines, look at the code above your last post.  Note the argument for cmdline is a string and you can put this together as you wish.


Edit:  In case I confused the matter, what I'm saying is you don't need a batch file to do what you are doing.  If you want, post the partial basiccad code and I will edit it to work.

landonairey

prl,

   Thanks for the reply, however maybe I was unclear before. I get an invalid syntax error when using the following:

cmdline "/c SORT v:\dcad.txt /O v:\dcad.txt"
run "cmd.exe"

   My version of dcad complains about the line with cmdline on it

prl

Quote from: landonairey on July 11, 2013, 07:23:49 AM
   My version of dcad complains about the line with cmdline on it

What version are you running? cmdline has been around since version 14.

landonairey

Ha, dcad3000 version 10 or 11 I believe.

prl

Quote from: landonairey on July 11, 2013, 07:29:37 AM
Ha, dcad3000 version 10 or 11 I believe.

Yes, that's version 11.  Maybe Dt knows how basiccad worked back then?  I don't have the references or installed program to check it.  Sorry.

dcadRob

A lot of useful commands were implemented after version 11. I think your best chance is to create the correct command as a string and write it to a .bsc or .d3m file. Then run that macro file. Make sense?

Your string would be something like :
c$="run "+chr$(34)+"cmd.exe /c SORT "+filein$+" /O "+fileout$+chr$(34)

DrollTroll

From the DesignCAD 2000 Developers Guidebook:

Hint: A common question is, "how do I run a batch file from a DesignCAD macro?" Batch files are not executable programs - they are script files that are interpreted by command.com. In order to run a batch file from a macro, DesignCAD requires the help of command.com to interpret the batch file.

Here are two methods to run a batch file c:\batch\mybatch.bat:

Run "cmd.exe /c c:\batch\mybatch.bat", 0

or

com$ = "cmd.exe /c "

bat$ = "c:\batch\mybatch.bat"

Run com$+bat$, 0

This same procedure can be used to run DOS commands:

Run "cmd.exe /c dir *.dcd > flist.txt"

By running "cmd.exe /c", we open a temporary copy of the commandline processor, which remains open only long enough to run the stated command, then closes.
25 years with DesignCAD

prl

Well Dt saved the day.  It couldn't be any simpler and you don't need a batch file, just one line of code in your macro.

run "cmd.exe /c SORT v:\dcad.txt /O v:\dcad.txt"


landonairey

#13
Very helpful! Thank you guys, I have a much better understanding of how designcad can use the command line now.

I've used the line
run "cmd.exe /c SORT c:\TEST.txt /O c:\TEST.txt"
and it works wonderfully , what happens if there's a space in the path name? (for example "Program Files")
run "cmd.exe /c SORT c:\Program Files\TEST.txt /O c:\Program Files\TEST.txt"

typing directly in the command line I can say:
SORT "c:\Program Files\TEST.txt" /O "c:\Program Files\TEST.txt"
and the quotes remedy the situation however desincad cannot interpret a command with nested quotes:
run "cmd.exe /c SORT "c:\Program Files\TEST.txt" /O "c:\Program Files\TEST.txt""

Is there a special character to use instead of the quotes? Or is there a way to have quotes within strings of text in designcad?


UPDATE:
We have lift off! In order to pass in the quotes I used chr$(34) instead of just " within the string (Thank you dcadRob) It now looks like this:

c$ = "cmd.exe /c SORT "+chr$(34)+"c:\Program Files\TEST.txt"+chr$(34)+" /O "+chr$(34)+"c:\Program Files\TEST.txt"+chr$(34)
run c$


Thank you Dt for finding those old commands and prl for summarizing :)