Solved Batch file that replaces a filename in a txt file

infinitevox

New member
Local time
5:17 PM
Messages
5
I've got a task that I need to repeat 100 times in a CAD program.

I've written a small script in the CAD program that takes a model and does a bunch of things to it, saves it, and closes it. (That part is taken care of, but I want to automate it)
That script is written in a text file. But it's specific to that model.

What I want to do is write a Batch file that will:
1. Look into a folder and put the list of files in a variable or list.txt
2. Replace "filename" in my script with the first file in the variable/list.txt
3. Run my script
4. Select the next "filename" in the variable/list.txt
5. Repeat 2-4 until there are no more files left

Thank you very much for your time

I've never written a batch before, so please bear with me.
What I've got so far, and commented to finish what I want to do but don't know how:
Code:
@ECHO OFF
REM Prompts user for the directory
set /p pathName=Enter the File Path of the folder:

REM Get file names and store in a text file
dir /s /b "pathName*.*">"list.txt"

REM Count number of files in list="numfile", set number of loops
REM SET i=1, For i to "numfile" DO

REM Take 1st file from list and store in variable "filename"

REM Put "filename" in script

REM Run script
C:\fileLocation\script

REM Close script
Close C:\fileLocation\script

REM Goto "filename" i+1

REM Repeat Put/Run/Close/Goto until done
 

My Computer My Computer

At a glance

Windows 7 Enterprise x64
Computer type
PC/Desktop
OS
Windows 7 Enterprise x64
Hi and Welcome to SevenForums,

Not sure what you want to do with the filenames so i have put an example together here that may help.

I can see you are counting the files and then trying to repeat until all files have been done, i think this task would be better using a FOR LOOP, this may help understand it a little - For /f - Loop through text | Windows CMD | SS64.com

Here is the code i have put put together quickly, towards the bottom is where you see the FOR Loop, essentially what it is doing is looping through the stated file (list.txt) and setting them to the variable %%a then i am telling it to echo %%a, you need to change that bit... rather than echo %%a you need to make it do what you want, depending on what that is the script may need more work.

Disclaimer - Always test the code on dummy data before using on live data
Code:
@ECHO OFF
REM Prompts user for the directory
set /p pathName=Enter the File Path of the folder:

REM 1. Change to the location set above
REM 2. List all files in that directory and store in a text file on desktop
cd %pathName%
dir /s /b *.* > c:\users\%username%\desktop\list.txt


REM Runs 'for loop' to run script against each file in list.txt

REM Currently it just echos the names of the files to the screen and pauses
REM so you can see them on screen, you need to replace the "ECHO %%A" with your command
for /f %%a in (c:\users\%username%\desktop\list.txt) do (echo %%a)

Pause
 

My Computer My Computer

At a glance

Windows 8.1 Pro x64Intel Core i5-2500K @ 3.30GHz - S11558GB Corsair DDR3 XMS3, PC3-12800NVIDIA GeForce GTX 650
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
Thanks for your help so far.

Is there an Openwith command?
Testing the code you gave me so far seems like it's working. I put 3 test files in a location and attempted to run the batch after replacing the "Echo %%a" with the location of my script, and putting "%%a" into the correct location of my script. It runs 3 separate instances, so that part seems to be working.

However, it now gives me the "Open this with what?" screen when it gets to my script :p
Is there a way to do:
for /f %%a in (c:\users\%username%\desktop\list.txt) do (Openwith "program" C:\Users\%username%\Desktop\Script %%a)

Otherwise I think another way I can achieve this is just have the batch file open the program before the FOR Loop, and see if it'll do it that way.

Either way, thank you again for your time.
 

My Computer My Computer

At a glance

Windows 7 Enterprise x64
Computer type
PC/Desktop
OS
Windows 7 Enterprise x64
I'm glad that code i gave you is helping to get further...

Is your script doing something within the actual program?
If yes then i'm afraid i'm not sure what to do.

What CAD program is it? maybe there are some switches within the program that can be used such as:

This is just an example and will not work...
Code:
"c:\Program Files\CAD\CAD.exe" /s script.bat
I would hope the code above would do something like run the program with the /s command which may stand for script and then you tell it the script to run etc...

Regards,
Jamie
 

My Computer My Computer

At a glance

Windows 8.1 Pro x64Intel Core i5-2500K @ 3.30GHz - S11558GB Corsair DDR3 XMS3, PC3-12800NVIDIA GeForce GTX 650
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
The CAD program is PRO/E (Creo2)

And yes, my script is doing something inside the actual program. I've basically automated a task that I'll have to repeat 100 times inside the program.
Once I've got it working, my plan was to run the program with windows mode turned off as well.
 

My Computer My Computer

At a glance

Windows 7 Enterprise x64
Computer type
PC/Desktop
OS
Windows 7 Enterprise x64
Okay, I've done some digging, and it's apparently possible to do.
Edit: Not working for some reason, I'm trouble shooting now.
This is the code so far:
Code:
@ECHO OFF
::  Prompts user for the directory
set /p pathName=Enter the File Path of the folder:
set /p username=Enter your username:

::  1. Change to the location set above
::  2. List all files in that directory and store in a text file on desktop
cd %pathName%
dir /s /b *.* > c:\users\%username%\desktop\list.txt

::  3. Opens Creo the runs 'for loop' to run script against each file in list.txt
::     This sets and populate parameters
for /f %%a in (c:\users\%username%\desktop\list.txt) do ("C:\Program Files\PTC\Creo 2.0\Parametric\bin\parametric.bat" -g:no_graphics C:\Users\%username%\Script.txt %%a)

Pause
Not sure where to go from here, but trial and error is my friend.
I learned some good stuff though. Thank you very much!

Additional questions:
1. How do I have the batch file Delete the list.txt it created and placed on the desktop once the whole thing is finished?
2. I'm assuming that the /f For Loop implicitly iterates the files in the list, yes?
3. I also assume there is a way to have the batch "wait" a predetermined amount of time?

I really appreciate the help!

Edited
 
Last edited:

My Computer My Computer

At a glance

Windows 7 Enterprise x64
Computer type
PC/Desktop
OS
Windows 7 Enterprise x64
If you post a screenshot of the errors your are getting i will see if i can help.

You are doing a very good job and i like the fact you don't necessarily wan't the script writing for you but are putting a lot of effort and research into this too.

Additional questions:
1. How do I have the batch file Delete the list.txt it created and placed on the desktop once the whole thing is finished?
2. I'm assuming that the /f For Loop implicitly iterates the files in the list, yes?
3. I also assume there is a way to have the batch "wait" a predetermined amount of time?

Edited

1. del list.txt
Code:
del c:\users\%username%\desktop\list.txt

2. Correct, it will go through each line in the text file, in your case that is the full path to a file including the file name.

3. This isn't as straight forward as it sounds... you would expect a command like "wait 10" to wait 10 seconds but that isn't the case, there are 3rd party scripts you can call to create a wait but i tend to ping an invalid IP address:

Code:
PING 1.1.1.1 -w 100 -n 2 >NUL

This would make the script wait for about 2 seconds... the >NUL hides the actual ping commands, this way it doesn't output the ping to the screen.

Regards,
Jamie
 

My Computer My Computer

At a glance

Windows 8.1 Pro x64Intel Core i5-2500K @ 3.30GHz - S11558GB Corsair DDR3 XMS3, PC3-12800NVIDIA GeForce GTX 650
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
I finally figured it out. It was a syntax error on my part XD
Just a pair of "" keeping it from working.

You are doing a very good job and i like the fact you don't necessarily wan't the script writing for you but are putting a lot of effort and research into this too.
Thank you, I don't know how someone learns by having others do it for them. Now that I know what I can do with batch files, I'll want to utilize them in the future for repetitive tasks.

1. del list.txt
Code:
del c:\users\%username%\desktop\list.txt
2. Correct, it will go through each line in the text file, in your case that is the full path to a file including the file name.

3. This isn't as straight forward as it sounds... you would expect a command like "wait 10" to wait 10 seconds but that isn't the case, there are 3rd party scripts you can call to create a wait but i tend to ping an invalid IP address:

Code:
PING 1.1.1.1 -w 100 -n 2 >NUL
This would make the script wait for about 2 seconds... the >NUL hides the actual ping commands, this way it doesn't output the ping to the screen.

Regards,
Jamie
Ahhhhh, thank you very much for that. Quite helpful!

Here's the final code:
:: -----------------------------------------------------------------------------------------------
:: Created by: ME - Date: 7/29/2014
:: Updated: 7/31/2014
:: -----------------------------------------------------------------------------------------------


@ECHO OFF
:: Prompts user for the directory
set /p pathName=Enter the File Path of the folder:
set /p username=Enter your username:

:: 1. Change to the location set above
:: 2. List all files in that directory and store in a text file on desktop
cd %pathName%
dir /s /b *.* > C:\users\%username%\desktop\list.txt

:: 3. Opens Creo the runs 'for loop' to run script against each file in list.txt
:: This sets and populate parameters
for /f %%a in (c:\users\%username%\desktop\list.txt) do ("C:\Program Files\PTC\Creo 2.0\Parametric\bin\parametric.bat" -g:no_graphics "C:\Users\%username%\Desktop\Automate\Script.txt %%a")

:: 4. Cleanup
del C:\users\%username%\desktop\list.txt
C:\Users\%username%\Desktop\Automate\creopurge.bat

Pause

Where the last line runs a separate batch file that cleans up the file directory and removes stuff that doesn't need to be there.

Couldn't have done this without your help!
 

My Computer My Computer

At a glance

Windows 7 Enterprise x64
Computer type
PC/Desktop
OS
Windows 7 Enterprise x64
You are very welcome and kudos to you for sticking it out and pushing yourself to learn and getting the end product!

:thumbsup:

If you like this sort of stuff then it maybe worth checking Powershell out, this is the new dos so to speak.
It's more powerful and you tend to be able write the same script in much less writing.

Just a thought if you are interested but i myself you batch file a lot, if i have to do the same thing again and again... i write a script :)

Regards,
Jamie
 

My Computer My Computer

At a glance

Windows 8.1 Pro x64Intel Core i5-2500K @ 3.30GHz - S11558GB Corsair DDR3 XMS3, PC3-12800NVIDIA GeForce GTX 650
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
Back
Top