Solved

Batch file to read target path and set variable

Posted on 2010-09-23
13
1,507 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
ID: 33744555
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
ID: 33744594
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
ID: 33744618
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 53

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 33744988
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
ID: 33745037
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 53

Expert Comment

by:Bill Prew
ID: 33745165
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
 
LVL 3

Author Comment

by:Jofnn
ID: 33745234
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
ID: 33745272
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
ID: 33745297
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 53

Expert Comment

by:Bill Prew
ID: 33745499
==> 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
ID: 33745575
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 53

Expert Comment

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

~bp
0
 
LVL 3

Author Closing Comment

by:Jofnn
ID: 33751170
exactly what I was looking for :o)

thanks again!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

785 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