Solved

Batch file to read target path and set variable

Posted on 2010-09-23
13
1,463 Views
Last Modified: 2012-05-10
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 :)
0
Comment
Question by:Jofnn
13 Comments
 
LVL 8

Expert Comment

by:DocSeltsam
Comment Utility
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?

--TheDoctor
0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
Hi,

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?
0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
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="
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 250 total points
Comment Utility
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.

~bp
0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
Bill,

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...

Jonathon
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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.

~bp
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Author Comment

by:Jofnn
Comment Utility
Bill,

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:

INI_FILE=\\server\share\folder1\file1.ini

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!!
0
 
LVL 5

Expert Comment

by:balasundaram_s
Comment Utility
Can you try like this:

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

Author Comment

by:Jofnn
Comment Utility
Balasundaram_S,

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)
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
==> 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...

~bp
0
 
LVL 3

Author Comment

by:Jofnn
Comment Utility
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)
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Ah, great, glad to know I wasn't completely off, glad that was useful.

~bp
0
 
LVL 3

Author Closing Comment

by:Jofnn
Comment Utility
exactly what I was looking for :o)

thanks again!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now