Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Batch file that replaces a filename in a txt file

30 Jul 2014   #1
infinitevox

Windows 7 Enterprise x64
 
 
Batch file that replaces a filename in a txt file

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 System SpecsSystem Spec
.
30 Jul 2014   #2
JDobbsy1987

Windows 8.1 Pro 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 System SpecsSystem Spec
31 Jul 2014   #3
infinitevox

Windows 7 Enterprise x64
 
 

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
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 System SpecsSystem Spec
.

31 Jul 2014   #4
JDobbsy1987

Windows 8.1 Pro 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 System SpecsSystem Spec
31 Jul 2014   #5
infinitevox

Windows 7 Enterprise x64
 
 

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 System SpecsSystem Spec
31 Jul 2014   #6
infinitevox

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
My System SpecsSystem Spec
31 Jul 2014   #7
JDobbsy1987

Windows 8.1 Pro 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.

Quote   Quote: Originally Posted by infinitevox View Post
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 System SpecsSystem Spec
01 Aug 2014   #8
infinitevox

Windows 7 Enterprise x64
 
 

I finally figured it out. It was a syntax error on my part XD
Just a pair of "" keeping it from working.

Quote   Quote: Originally Posted by JDobbsy1987 View Post
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.

Quote   Quote: Originally Posted by JDobbsy1987 View Post
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:
Quote:
:: -----------------------------------------------------------------------------------------------
:: 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 System SpecsSystem Spec
01 Aug 2014   #9
JDobbsy1987

Windows 8.1 Pro x64
 
 

You are very welcome and kudos to you for sticking it out and pushing yourself to learn and getting the end product!



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 System SpecsSystem Spec
Reply

 Batch file that replaces a filename in a txt file




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Downloading file from IE8 replaces spaces with underscores
Hi All, When I download a file from IE8, if the file name has spaces, the spaces turn to underscores. IE 8 is fully updated, and so is my Windows 7 Pro. Couldn't find a solution any where, any help would be great. Thanks
Browsers & Mail
I need a batch file to open a random file within a certain folder/path
I want a batch to select and open a random link file in a folder i have. All the links are music videos I like to listen to, and I kind of want to just have an option for 'surprise me!'. I can change the file names if I have to, and I have a basic batch file ability. I know the simple commands,...
General Discussion
Batch File that grabs file names from folder to insert into batch file
Ok I'm totally at a loss on how to word this so I'm just going to write it out the best that I can... :confused: First I have a batch file that I'm writing that will convert mp4 files to mp3 files (I have a lot) for a buddies band using ffmpeg. Here's the code that I'm using: ffmpeg -i...
General Discussion
batch file open file for program in the startup folder
I have a computer that is a drone specifically for an outside LED sign. This computer is designed to never shut off and doesn't have any other program on it besides Operating System and the sign software. I need to create a batch file that I can place in the "start-up" folder that will open the...
General Discussion
Batch file , change filename with system date and time infor
I wrote a bat file, to change a file name, test.txt to testMonthDayYearTime.txt here is what i wrote: ren c:\example\test.txt test%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%%.txt But after running, the file name is test12182010 093012.txt, a space is ...
General Discussion
How to create a batch file that will move files based on the file type
How to create a batch file that will move files based on the file type? My Requirement : I need a batch file to copy all .doc files in all my HardDisk to a removable disk say (Z: drive) . Can anyone make it possible using a batch file?Please...Its urgent...
General Discussion


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 15:31.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App