Select to view content in your preferred language

Read Data into Bat File from Txt File to Be Used As Variables

2327
5
03-02-2012 08:14 AM
MichaelVolz
Esteemed Contributor
To All Python Users That Use Bat Files to Call Python Scripts:

I am trying to read in values from a txt file in a bat file that will be used as variables when calling python scripts within the bat file.  I have searched the Internet for the proper syntax to accomplish this goal, but I am unable to read from a txt file in the bat file.

Does anyone have experience with this task and could provide some sample code so I can see where I am going wrong?  Any help or hints are greatly appreciated.  Thanks.
Tags (2)
0 Kudos
5 Replies
HemingZhu
Frequent Contributor
To All Python Users That Use Bat Files to Call Python Scripts:

I am trying to read in values from a txt file in a bat file that will be used as variables when calling python scripts within the bat file.  I have searched the Internet for the proper syntax to accomplish this goal, but I am unable to read from a txt file in the bat file.

Does anyone have experience with this task and could provide some sample code so I can see where I am going wrong?  Any help or hints are greatly appreciated.  Thanks.


One approach you could try is to read file directly in python script so you don't have to pass variables from bat file.
0 Kudos
curtvprice
MVP Alum
Presumably you could use I/O redirection to do this -- but I think it would a lot easier to write the driver script in Python instead of the arguably primitive Windows command shell. (Not to knock it - the good old cmd.exe still has its place.)

If your python scripts are set up to only work with command line arguments, you can run them with os.system() or (better) the newer" rel="nofollow" target="_blank">http://docs.python.org/library/subprocess.html#module-subproce... subprocess module.
0 Kudos
MichaelVolz
Esteemed Contributor
Curtis:

I think my original post might be a bit misleading.  The variables I am trying to pass into the bat file are the location of the the python script and the location of the log file generated from the bat file.  When I ran bat files as Scheduled Tasks in Windows Server 2003, I only needed to provide the name of the python script and log file because it understood it was a path relative to the location of the bat file.  Now on Windows Server 2008, the bat file never calls the python script or log file unless I supply the entire path to these files.  I want to store these paths in a text file to simplify the update of my system and the scripts that are used.  I have 2 reasons for this.  I can have multiple scripts that need the same python scripts, so I can use the text file to supply this information to all bat files.  In addition, if the location changes or this bat file is used on a different server, I only need to make the change in the text file and not each bat file.  I hope this clears up why I want to be able to read in information from a txt file in a bat file.
0 Kudos
MichaelVolz
Esteemed Contributor
Does anyone from ESRI have any ideas on how I would accomplish this goal in a bat file?

Or is there another method of calling the python files that can be used?  I currently use the bat file to sequentially call python scripts so they proceed in a specific order making sure the previous task has been completed.

As I said in the original Post, I need to add the full path to the python script and log files in the bat file on a Windows 2008 Server which I did not need to do in Windows 2003 Server.  Also I would like to use the txt file for the paths so it acts as a config file to be used by multiple scripts so 1 file can be changed for multiple scripts instead of needing to change multiple files.
0 Kudos
curtvprice
MVP Alum
As I said in the original Post, I need to add the full path to the python script and log files in the bat file on a Windows 2008 Server which I did not need to do in Windows 2003 Server. 


So Win 2008 Server isn't allowing you to set a "Start in:" folder?

Another approach to run python scripts no matter where they are is add a folder containing your python scripts to the Windows PATH variable. You can do this just inside your .bat script.

Your .bat script can also use pushd to change the current folder after the script starts.

Or is there another method of calling the python files that can be used?


I was suggesting using Python (os.system(), subprocess, etc)...

I'm still a bit confused as to what the problem is.
0 Kudos