Batch file to read target path and set variable

I can't appear to find the "Windows Batch Scripting" zone to assign to!!!

Hi (me again!),

I've got a shortcut which has the INI file location within the target path... current value would be:

"C:\progra~1\applic~1\folder\file.exe" inifile=\\server\share\folder\file.ini

Open in new window

I want my batch file to read the target path of the defined shortcut and set a variable dependant on it... therefore for the above example, %inifile% should equal the link after "inifile=" in the target path?

Does that make sense? Any help is appreciated :)
Who is Participating?
Bill PrewConnect With a Mentor Commented:
Why not just run it like this:

"C:\progra~1\applic~1\folder\file.exe" "\\server\share\folder\file.ini"

and then in the BAT file grab the value of the first parm like this:

if "%~1" NEQ "" (
  set IniFile=%~1
) else (
  set IniFile=\\server\share\folder\file.ini

This will assign the path to the IniFile variable if it is passed in on the command line, or default it to some value if it isn't passed in.

Hi there,

does it have to be a batch? Can't you just use WSH to do the job?

Do you want to feed the shortcut to your script on the comandline?

JofnnAuthor Commented:

Yes - must be batch as the program/script we're amending is in batch.

The program/script currently loads and has a hardcoded link to an INI file, which is where it gets all it's relevant options.  Problem is - the batch file is converted to EXE everytime that it is updated... To save us having to do this, we would rather add the INI location into the shortcut that the users use... the batch should then read that, set the variable, and read it like it currently does.

Does that make sense?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

JofnnAuthor Commented:
To clarify -

If we move the INI file for any reason (i.e. to an alternate server, or onto a non-networked standalone PC) we need to change the INI variable within the program so it continues to get it's settings.

But rather than re-compile each time, we would have the INI location in the target path (which we could change whenever we wanted without recompiling).  The script would set the variable dependant on what is in the target path next to "INIFILE="
JofnnAuthor Commented:

Thanks for that...

That would be a good idea, but the program is compiled as an exe and all the user should have to do is click on the icon and it works... Therefore, best option I can think of, is controlling this in the target path...

Bill PrewCommented:
Sorry, don't mean to be dense, but when you said:

==> Problem is - the batch file is converted to EXE everytime that it is updated... To save us having to do
==> this, we would rather add the INI location into the shortcut that the users use... the batch should then
==> read that, set the variable, and read it like it currently does.

I took this to mean that you were using a tool to compile your BAT file into this EXE, which was then run.  Is that not the case?  If it is the case, then why can't the logic I proposed be added to the BAT file being compiled?

Sorry if I'm missing the obvious.

JofnnAuthor Commented:

Could be my poor explaining:

We have a .bat file which is compiled into EXE which our users use.  If we move the INI file or it needs to point to a different ini file, we have to edit and recompile the EXE each time...

So - within the bat there is a line that says:


Some users may not need the settings that are in that INI file and they may need file3.ini from the folder2 folder... we would have to edit this line in the batch script and then recompile it...

What I'm aiming for, is for the users to not need to call IT anymore to request a change to the file it reads (which could also change location at any point)... Therefore if they could just edit the properties of the file target, this could be picked up by the EXE (or compiled batch... ) and it will run ok!

The users should never have to open command prompt or anything like that to pass the 2nd variable through as you advised above.

Hope that makes a little more sense!!
Can you try like this:

"C:\progra~1\applic~1\folder\file.exe" inifile=file:\\server\share\folder\file.ini
JofnnAuthor Commented:

Thanks... as explained in my part to BP - I'm looking for that to be in the Target Path of the relevant shortcut that the users will be using.  The script should then extract the INI section and set the variable accordingly.

(Therefore the user will get the correct settings dependant on the INI file)
Bill PrewCommented:
==> Jofnn

Okay, it sounds like I did understand originally, so now help me understand how adding the logic I mentioned to your BAT file doesn't work for you?  I think it is what you are looking for, the BAT file can pick up the name of the ini file from the command line in the shortcut properties and set the internal variable for the BAT file to use.

I think we're close...

JofnnAuthor Commented:
Bill - complete misunderstanding on my part!!!

I read your previous comments as adding it to the command line (i.e. launching command prompt... typing the path to the EXE and then the INI)...   We appeared to be calling "command line" and "target path" different things, when we both meant the exact same thing!

To summarise - I've tested and it works perfectly!!!

Thanks very much (again)
Bill PrewCommented:
Ah, great, glad to know I wasn't completely off, glad that was useful.

JofnnAuthor Commented:
exactly what I was looking for :o)

thanks again!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.