cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Script to build trail file based on list of files?

davehaigh
11-Garnet

Script to build trail file based on list of files?

Wondering if anyone has a batch file that will work with a trail file segment and a list of files to build a long trail file.

I have the following files:

Trail-1.txt

Trail-2.txt

File-list.txt

In the new file I want:

 

  1. Trail-1.txt
  2. Trail-2.txt with the word FAMILY-INSTANCE replace with the first line of file-list.txt
  3. Trail-2.txt with the word FAMILY-INSTANCE replace with the second line of file-list.txt
  4. Trail-2.txt with the word FAMILY-INSTANCE replace with the third line of file-list.txt
  5. Etc… until you get to the last line of file-list.txt 

  

Essentially a for each command with text replacement and append the new file with the modified trail-2.txt each time.

 


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.
1 ACCEPTED SOLUTION

Accepted Solutions
psobejko
12-Amethyst
(To:davehaigh)

Ok, I see your problems and how the last two for /f statements didn't work.

Try this version:

for /F "tokens=2 skip=1 delims= " %%A IN (mass-parameter-update.idx) DO @echo %%A >> instance-list.txt

call remove-last-line.cmd instance-list.txt

for /F "tokens=3 skip=1 delims= " %%A IN (mass-parameter-update.idx) DO (set generic_name=%%A)

copy trail-1.txt big-trail-test.txt

echo Ready to start the substitution - replacing GENERIC-PART...

call batchsubstitute.bat GENERIC-PART %generic_name% trail-2.txt >> trail-3.txt

echo Ready to start the substitution - replacing FAMILY-INSTANCEs...

for /F "tokens=1" %%A IN (instance-list.txt) DO (call batchsubstitute.bat FAMILY-INSTANCE %%A trail-3.txt >> big-trail-test.txt)

echo Finished...

I also shortened up some of the code that extracts the name of the generic from the .idx file.

And removed the first of the problematic for /f statements - no need for it as you are calling batchsubstitute.bat just once.

But the key problem was that you were missing the CALL keyword - see line 8.

View solution in original post

14 REPLIES 14
Jose_Costa
6-Contributor
(To:davehaigh)

Have you looked at Trailmaker 0.3 ?

Not sure that's really what I was after. I need a function to replace a string in one of the trail files because I'm opening instances of a family table and doing something to each one then backing it up. I don't want to have to pick the instance manually when running the trail file. We could have hundreds or thousands of instances.

Attached is Edwin Muirhead's original Trail Maker.htm, which in reality is just a java script. You can view it in notepad++. I've use this in the past, but it's not working for what I need to do now. Not sure why, I think there's too much hard coded in it to work with the current rev of Creo.

Also attached are my sample files and the resulting file I made manually to test the function of the longer trail file. The file list came from idx file when I backed up the generic to the folder. I just opened it in notepad++ and use alt select to select a box with just the instance names and saved that as file-list.txt

Essentially I think this can all be done in perl, or even a cmd file. The simpler the better.

The core of the script would do the following:

  1. copy Trail-1.txt to a new file.
  2. Then for each line in file-list.txt:
    • Append the new file with Trail-2.txt while replacing the string FAMILY-INSTANCE with the current line of file-list.txt

Ideally the script would have a set command at the top where you could specify the string to search for and replace.

To me the more basic this script is, all text based the better, then it's uber flexible to be modified to any use I want.

I'm sure I could figure this out if I had the time, but right now I don't have the time, and I was hoping someone had done something like this in the past. 

I used autohotkey for this task in the past, because of the clipboard functionality. But i'm sure it is easy to convert to work with files. In the top is the header, in the left text field the trail content and in the right text field you can put the list of files. File list can also be in clipboard. But you need autohotkey (opensource tool) to run or compile it to an executable.

I haven't had a chance to test the other two ideas from Andreas or Martin yet. I was playing with this today. I have commands that work, but they bomb out when I combine them into a full script.

Work flow I'm trying to achieve.

  • User opens a generic and saves to disk in a new folder
    • That folder will contain two files, the generic.prt and the folder-name.idx file
    • I have two trail files. The one that starts the trail file session and the one that does the work.
  • I'd like to create a startup script the user can copy to the folder. That script will first create the trail file, and then start up Creo and run it.

I cobbled together some stuff from the internet today and have the concept working from the command line.

  • I grabbed the attached script from this site to do the find & replace step. DOS Batch - File Examples
  • From another post on some site I wrote the remove-last-line script that is attached. This is needed to clean up the instance list file generated from the .idx file.
  • The attached cmd-commands file are the commands that work from a command shell.

Now I know I need to have call in front of some of these commands in a script and I have to double up the %'s in the for statements.

Eventually I should add a command to figure out what the name of the folder is.

My problem is it's failing on the last two for /f commands when combined into one.

I tried breaking them out into separate scripts and then calling them from a master script that continues to fail on the last two for commands.

The thing is, each script works on it's own.  They are attached.

  • setup-files.cmd creates the two files that contain the instances names and the name of the generic and copies the source start of the trail file to a new name.
  • start-instance-trail-file.cmd creates a new trail file that replaces the string GENERIC-PART with the name of the generic from the generic.txt file
  • complete-instance-trail-file.cmd uses the new trial file segment created by the script above to complete the script that was started by the setup-files script.

The problem, I'm sure, all has to do with the syntax in the for /f command, but I'm unsure how to fix it.

I'm sure this could all be cleaned up with something slicker. But I want to keep it flexible enough so it can be reused on other projects with different trial files. And I want to avoid complied programs. My preference is to stick with native windows scripting.

KevinBrault
5-Regular Member
(To:davehaigh)

Hi David,

I didn't look through your scripts but I do want to help so I attached my batch file that:

This script is just part of our all-in-one release mapkey.

  1. Finds the drawing name based on the print command Creo 3.0 sends to it via the print with command function in Creo.
  2. Unzips the zip file from Creo "Save as Zip" function.
  3. Finds all assembly files that contain the "Drawing Name" in their file name and adds the name to a list.
  4. Finds all part files that contain the "Drawing Name" in their file name and adds the name to a list.
  5. Parses the IDX file and finds all part files that contain the "Drawing Name" in their instance name and determines if the generic is a PRT or ASM file and adds the instance name to the appropriate list.
  6. Loops through the ASM and PRT lists to generate a single (sometimes very large) trail file that will:
    1. Generate eAssembly files of the assembly models (This is now commented out. We no longer use eAssemblies).
    2. Generate IGES and STEP files of the part models.
    3. Note: the batch file requires some "escape" characters to make things work when writing the trail file. Hence, what you see in the script is not what you get in the trail file.

Please let me know if this is of any help.

At least you should find some working (in Windows 7) "for" commands.

Kevin

That's an inspired use of the print function.

psobejko
12-Amethyst
(To:davehaigh)

I've looked at your script files, but frankly I'm confused about your statement about "My problem is it's failing on the last two for /f commands when combined into one", because I don't know where that is, or for that matter, what is failing.  Is the file cmd-commands.txt in an editor and you are copying and pasting its lines into a separate command window?

I'd like to help, but I need to have these:


sample of the input files: trail-1.txt, mass-parameter-update.idx

the "desired" intermediate files: instance-list.txt, generic.txt, trail-2.txt, trail-3.txt

the "desired" output file: Modify-family-table.txt (or is it big-trail-test.txt?)

Per the linked item, you might need a real programmer to help xkcd: Real Programmers

Here, I'll add the script text to the posting.

this is the content of my first script after I broke them up into three separate scripts.

for /F "tokens=2 skip=1 delims= " %%A IN (mass-parameter-update.idx) DO @echo %%A >> instance-list.txt

call remove-last-line.cmd instance-list.txt

for /F "tokens=3 skip=1 delims= " %%A IN (mass-parameter-update.idx) DO @echo %%A >> generic.txt

set /p texte=< generic.txt

echo %texte% > generic.txt

copy trail-1.txt Modify-family-table.txt

This is the content of the second script the first for command that will not run when combined with the script above.

for /F "tokens=1" %%A IN (generic.txt) DO (batchsubstitute.bat GENERIC-PART %%A trail-2.txt >> trail-3.txt)

This is the content of the last script the second for command that will not run when combined into one script.

for /F "tokens=1" %%A IN (instance-list.txt) DO (batchsubstitute.bat FAMILY-INSTANCE %%A trail-3.txt >> Modify-family-table.txt)

Attached are the source files.

If I run each of the scripts above in order it does what it's supposed to do. If I combine the last two commands into the first script it will not run those.

These last two scripts use the batchsubstitue.bat also attached.

I have to run to an all day workshop, so I won't get to respond to any comments today.

psobejko
12-Amethyst
(To:davehaigh)

Ok, I see your problems and how the last two for /f statements didn't work.

Try this version:

for /F "tokens=2 skip=1 delims= " %%A IN (mass-parameter-update.idx) DO @echo %%A >> instance-list.txt

call remove-last-line.cmd instance-list.txt

for /F "tokens=3 skip=1 delims= " %%A IN (mass-parameter-update.idx) DO (set generic_name=%%A)

copy trail-1.txt big-trail-test.txt

echo Ready to start the substitution - replacing GENERIC-PART...

call batchsubstitute.bat GENERIC-PART %generic_name% trail-2.txt >> trail-3.txt

echo Ready to start the substitution - replacing FAMILY-INSTANCEs...

for /F "tokens=1" %%A IN (instance-list.txt) DO (call batchsubstitute.bat FAMILY-INSTANCE %%A trail-3.txt >> big-trail-test.txt)

echo Finished...

I also shortened up some of the code that extracts the name of the generic from the .idx file.

And removed the first of the problematic for /f statements - no need for it as you are calling batchsubstitute.bat just once.

But the key problem was that you were missing the CALL keyword - see line 8.

Paul, thanks for the solution! So obviously without the call statement embedded into the for command it's not going to pass the variable to the other program.

Last week was crazy busy, so I didn't get around to testing this until today.

I've added a few things to make this a bit more automated.

The intended workflow is the user will run a script that will setup a folder inside the normal Creo startup folder. (attached as Setup-Mass-Param-Update-Folder.cmd)

That script prompts the user for a folder name.

12-12-2016 10-57-00 AM.jpg

When the user enters a name it then copies all the required files from a folder on the server to the new folder.

12-12-2016 10-53-52 AM.jpg

The user then opens a generic and does a save backup and saves it to the new folder. That folder now has two additional files. The generic part and an .idx file with the folders name.

12-12-2016 11-01-21 AM.jpg

After this step the user will exit Creo and double click on the Updated-Mass-Parameters.cmd script in the folder.

I modified the script to figure out the name of the folder it resides in and then use that elsewhere in the script. And finally in the last line I have the script startup Creo and run the trail file.

for %%* in (.) do set FOLDER=%%~nx*

for /F "tokens=2 skip=1 delims= " %%A IN (%FOLDER%.idx) DO @echo %%A >> instance-list.txt

call remove-last-line.cmd instance-list.txt

echo Ready to start the substitution - replacing GENERIC-PART...

for /F "tokens=3 skip=1 delims= " %%A IN (%FOLDER%.idx) DO (set GENERIC_NAME=%%A)

copy trail-1.txt Modify-family-table.txt

call batchsubstitute.bat GENERIC-PART %GENERIC_NAME% trail-2.txt >> trail-3.txt

echo Ready to start the substitution - replacing FAMILY-INSTANCEs...

for /F "tokens=1" %%A IN (instance-list.txt) DO (call batchsubstitute.bat FAMILY-INSTANCE %%A trail-3.txt >> Modify-family-table.txt)

echo Ready to start Creo and run the trail file

call "C:\ptc\Creo 2.0\Parametric\bin\parametric0.bat"  Modify-family-table.txt

After Creo is done the user can replace the generic in the library with the latest one in the folder.

Obviously this process can now be modified for many other tasks.

Hi,

just one tip for you .

Example:

You can merge setup-files.cmd and remove-last-line.cmd files into one cmd file. See attached file containing following code.

@echo off

for /F "tokens=2 skip=1 delims= " %%A IN (mass-parameter-update.idx) DO @echo %%A >> instance-list.txt

call:remove-last-line instance-list.txt

for /F "tokens=3 skip=1 delims= " %%A IN (mass-parameter-update.idx) DO @echo %%A >> generic.txt

set /p texte=< generic.txt

echo %texte% > generic.txt

copy trail-1.txt Modify-family-table.txt

goto:eof

::--------------------------------------------------------

::-- remove-last-line

::--------------------------------------------------------

:remove-last-line

setlocal enabledelayedexpansion

set count=

for /f %%x in ('type %1 ^| find /c /v ""') do set /a lines=%%x-1

copy /y nul %tmp%\tmp.zzz > nul

for /f "tokens=*" %%x in ('type %1 ^| find /v ""') do (

  set /a count=count+1

  if !count! leq %lines% echo %%x>>%tmp%\tmp.zzz

)

move /y %tmp%\tmp.zzz %1 > nul

goto:eof

MH


Martin Hanák

Hi,

you can test my gizmo.

  • unzip my 140525_2016-12-02.zip file, it contains following files

list.png

  • basic.exe ... Chipmunk Basic ... Chipmunk Basic Home Page
  • replace my File-list.txt, Trail-1.txt, Trail-2.txt files with yours
  • run combine_trails.bat

MH


Martin Hanák

Additional information: My combine_trails.bas can be modified easily to extract information from mass-parameter-update.idx.

MH


Martin Hanák
Top Tags