Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.Bat Question: Setting Current Folder Variable 3

Status
Not open for further replies.

GGleason

Technical User
Mar 22, 2001
321
US
I have a batch file where I want it to set a variable from the current folder path.

For example, if the current folder the batch is running in is:

C:\Temp\Applications

A variable would be set to equal “Applications” via the “set” command.

Is this possible?

tia,
GGleason
 
I'm not sure if this is the direction you mean, but you can set a variable in the Environment Variabled, you can add a new System Variable (or User Variable for just the user).

Example:

- Add the variable: TEST with the value "Applications"
- Open a DOS window and do "ECHO %TEST%", it will answer "Applications"
- You can use this in a batch file,
example:
MD C:\TEMP\%TEST%
COPY BLAH.EXE C:\TEMP\%TEST%

You CAN add variable threw the Group Policies also, so you can change the settings and variables at will if your in an Active Directory environement.



"In space, nobody can hear you click..."
 
PUSHD saves the current directory and changes to a new current directory that you specify. Later, you can execute POPD to retrieve the directory that PUSHD saved, restoring it as the current directory. These commands are especially useful in batch files but can be used at the command line as well.

Although the names imply that a stack is being maintained, you can’t nest these commands. Only one directory is stored by PUSHD, and it’s overwritten by the next PUSHD. Moreover, POPD clears the saved directory.
 
Hello GGleason,

This is a way you can adopt to do without interrupting the flow of your existing .bat. Just adding these lines where you want to establish the environment variable.

Some caveat:
[1] You said if the current .bat located at c:\temp\applications, the environment variable takes on a value "applications". Then, I would say if it is at d:\temp\install\applications, the environment variable will still be "applications". Is that what you want?

[2] If the .bat located at root, say, c:\, then the environment variable will take on "c:\" itself in my script. This may not what you want. But, you have to be more precise.

[3] I establish an environment variable of "user" type. It will be persistent with the "user" upon changed. The variable will be called "applications" as well---you have not said what you want it called.

[4] There will be a very delicate problem that I won't bother to tell the story except you discover/raise...
Code:
:: some other lines above in your existing .bat
:: insert the lines below at where you want to make env-var
@echo off
set vbsf=}temp{.vbs
echo 'dynamically built >%vbsf%
echo set wshshell=createobject("wscript.shell") >>%vbsf%
echo set oEnv=wshshell.environment("process") >>%vbsf%
echo sCDir=wshshell.currentdirectory >> %vbsf%
echo sCDir=Right(sCDir,InstrRev(sCDir,"\")+1) >> %vbsf%
echo oEnv("applications")=sCDir >> %vbsf%
echo set oEnv=nothing >> %vbsf%
echo set wshshell=nothing >> %vbsf%
echo set wscript.quit >> %vbsf%
call cscript %vbsf% //B //nologo
del %vbsf%
set vbsf=
:: continue with some other lines in your existing .bat
regards - tsuji
 
tsuji,

Thanks for responding and please forgive my late response. Here are the answers/questions for your numbered points:

[1] Yes

[2] That is ok

[3] Is the environment variable in this code "applications"?

[4] ok

In the code, why do you have the last line as

set vbsf=

Why is it incomplete? Also, is this VB Script code (just curious)?

How should I set the environment variable?

Thanks,
GGleason

[reading]
 
GGleason,

It is "incomplete" because it is this way to eliminate an enivronment variable at commandprompt.

regards - tsuji
 
>>..I want it to set a variable from the current folder path.

>>For example:

>>C:\Temp\Applications

>>A variable would be set to equal “Applications” via the “set” command.

You can capture the current path and assign it to a variable using the set command:

=== begin tst.bat ===
@echo off
set x=%cd%
echo Path var: %x%
=== end tst.bat ====
Where in your example, x would equal c:\Temp\Applications
To only capture the current 'leaf' (Applications) would require some program parsing.

This would be trivial in JD Powers 4dos 4nt command replacement program. I don't think it can be done in a 'pure' batch file
=== begin 4nt tst.btm ===
@echo off
set x=%_cwd
set leafpos=%@words[\,%x]
echo Path leaf: %@word["\",%leafpos,%x]
=== end 4nt tst.btm ===
A heck of a lot shorter than some of the vbscript I saw posted by another user....

[] FWIW, I'm not affiliated with JPSoft, just have been a fan for a while.
 
Hello jhawklyn,

I will certainly not argue about the shortness/conciseness of scripting. I just want to point out one important technical issue: namely making an environment variable visible process-wide or system-wide if not persistent thereafter pertaining to the namespace as an application can read and see. The set would fail to deliver this functionality since win9x. I am not 100% confident my vbs rendering can deliver as it depends very much on the application as well. But, as GGleason does not feedback I would guess my solution did not stand up to his/her scrutiny as well.

regards - tsuji
 
tsuji,

You stated that I did not give any feedback, so let me clarify my first response to your post. I gave boxed responses (i.e., “[x]”) to your questions and then posited 3 questions of my own. The first question you answered and that was regarding on setting the environment variable to blank.

The 2 remaining unanswered questions are:

1. Is this VB Script? (I assume that it is)
2. What environment variable is being set so the rest of the batch file can use it?

Thanks for the opportunity to clarify.

GGleason
 
GGleason,

Q >[3] Is the environment variable in this code "applications"?
A >>The variable will be called "applications" as well---you have not said what you want it called.

The environment variable name will be called "applications". It values will be determined according to where the user actually run its batch file. If the .bat file is at d:\test\abc, then the equivalent set (but differ in practice) will be:
set applications = abc

Q >Also, is this VB Script code (just curious)?
A It is in latent. The vbs is made on-the-fly without interrupting your other instructions in the presumed .bat file you have. The vbs will subsequently be deleted after it has run its course.

I take this opportunity to clarify or amend my posting
>>3] I establish an environment variable of "user" type. It will be persistent with the "user" upon changed.
Actually, I implemented a "process" type environment variable as reflected in the line:

echo set oEnv=wshshell.environment("process") >>%vbsf%

It is a less persistent type. (I was eventually decided to try out process-type before user-type because that would have less impact on your system. If it doesn't work out with your application, I would switch to user-type by only changing the line to:

echo set oEnv=wshshell.environment("user") >>%vbsf%

and it would be set.

- tsuji


 
tsuji,

Thank you for the above response.

Here is the script file created via the code you suplied:

'dynamically built
set wshshell=createobject("wscript.shell")
'set oEnv=wshshell.environment("process")
set oEnv=wshshell.environment("user")
sCDir=wshshell.currentdirectory
sCDir=Right(sCDir,InstrRev(sCDir,"\")+1)
oEnv("applications")=sCDir
set oEnv=nothing
set wshshell=nothing
set wscript.quit

However, when I try to echo out the environment variable applications , the value is not set. I am trying to see what the value is by having the following line of code in my batch file after the script file is called:

echo applications=%applications%

Do you have any suggestions?

Thanks for your time,
GGleason
 
GGleason,

I have not time enough this moment to verify (I'll have.)

But an immediate correction to make, thank to your follow up, is the line
set wscript.quit [_wrong_]
It was meant to be
wscript.quit

Hence, the original .bat line should be read:
Code:
    echo wscript.quit >> %vbsf%
- tsuji
 
Further note:

Other than the above typo, cannot confirm your observation. The variable set just fine.

- tsuji
 
tsuji,

The revision to the code was key. However, the syntax was not quite right for the result I was looking for. I changed the following line of code:

echo sCDir=Right(sCDir,InstrRev(sCDir,"\")+1) >> %vbsf%

to

echo sCDir=Right(sCDir,Len(sCDir)-InstrRev(sCDir,"\")-1) >> %vbsf%

which gave me the result I was looking for.

Thanks for all the help!
GGleason
[reading]
 
A correction to my above post, the following corrected line should be as follows:

echo sCDir=Right(sCDir,Len(sCDir)-InstrRev(sCDir,"\")) >> %vbsf%

Here is my revised code adapting tsuji’s code:

set vbsf=%cd%\}temp{.vbs
echo 'dynamically built >%vbsf%
echo set wshshell=createobject("wscript.shell") >>%vbsf%
echo 'set oEnv=wshshell.environment("process") >>%vbsf%
echo set oEnv=wshshell.environment("user") >>%vbsf%
echo set oDir=wshshell.environment("user") >>%vbsf%
echo sCDir=wshshell.currentdirectory >> %vbsf%
echo oDir("OriDir")=sCDir >> %vbsf%
echo sCDir=Right(sCDir,Len(sCDir)-InstrRev(sCDir,"\")) >> %vbsf%
echo oEnv("SubDir")=sCDir >> %vbsf%
echo set oEnv=nothing >> %vbsf%
echo set oDir=nothing >> %vbsf%
echo set wshshell=nothing >> %vbsf%
echo wscript.quit >> %vbsf%
call cscript %vbsf% //B //nologo
echo %vbsf%
del %vbsf%
set vbsf=
echo SubDir=%SubDir%
echo OriDir=%OriDir%

The SubDir environment variable is the subdirectory and the OriDir environment variable is the full path.

GGleason
[reading]
 
GGleason,

Thanks for sharing.

Just a side remark. Putting %cd%\ would just change the location of the temporary script, you know well. If the .bat is launch by double-click, without %cd%\ will be where the .bat locate. The only thing you have to control eventually is the .currentdirectory. It must be absolutely right matching your expectation and it would be fine.

Your followups deserve a star. Thanks.

regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top