Jump to content

command detail for (for /f "delims=: tokens=1" %%i in ("%~


Recommended Posts

any body plz guide me for this command for use in windows post installer.

(for /f "delims=: tokens=1" %%i in ("%~dp0") do set drive=%%i:)

I M RUNNING IT ON WPI.CMD AND SUCCESFULLY RUNNING WPI AFTER WINDOWS INSTALLATION.

BUT I HAVE COPIED IT THROUGH A FORUM.

I NEED TO UNDERSTAND IT PROPERLY HOW IT WORKS

WHAT DOES (/F) (DELIMS=:) (TOKENS=1) %%i (%~dp0) stands for .

plz help maintaing my knowledge

thanks

Link to comment
Share on other sites


for /f "delims=: tokens=1" %%i in ("%~dp0") do set drive=%%i:

delims is short for delimiters, these divide the output into tokens (pieces).

tokens=1 means we only want the first token.

%%i is the variable that gets set to the first token.

%~dp0 is the drive and path of the currently running script.

What the line does is to set the variable %drive% to be just the letter of the drive that the current script is running from, which could probably be simplified to just

set drive=%~d0

For more info try typing

for /?

set /?

call /?

at a command prompt, or try this site.

Link to comment
Share on other sites

thxx 4 ur reply..i have understood it clearly bt u havnt tell what /f stands for ..and will the cd drive will be automatically recognize through this command or we had to add some more text.?? plzzz do reply thxx

Link to comment
Share on other sites

thxx alot uido,5eraph,jaclaz,for ur support and od course firefox99 but i realy didnt understand it clearly yet.ill b thankfull if u explain a little more...thxxx guyzz.

Link to comment
Share on other sites

Let's try playing the game the other way round. :unsure:

You originally asked about this:

for /f "delims=: tokens=1" %%i in ("%~dp0") do set drive=%%i:

Try putting it, slightly modified in a batch like this and sabe the batch as C:\Batches\forf.cmd:


@ECHO OFF
for /f "tokens=1,2,3 delims=:\" %%i in ("%~dp0") do (
ECHO this is "i" i.e. token #1 "%%i"
ECHO this is "j" i.e. token #2 "%%j"
ECHO this is "k" i.e. token #3 "%%k"
ECHO this is parameter 0, i.e. the command line that started the batch "%0"
ECHO this is parameter ~d0, i.e. the Drive of command line that started the batch "%~d0"
ECHO this is parameter ~dp0, i.e. the Drive and Path of command line that started the batch "%~dp0"
set drive=%%i:

)
SET drive

and run it:

C:\batches>forf
this is "i" i.e. token #1 "C"
this is "j" i.e. token #2 "batches"
this is "k" i.e. token #3 ""
this is parameter 0, i.e. the command line that started the batch "forf"
this is parameter ~d0, i.e. the Drive of command line that started the batch "C:
"
this is parameter ~dp0, i.e. the Drive and Path of command line that started the
batch "C:\batches\"
drive=C:

C:\batches>

Now, you should have understood, and you can change the original snippet to:

for /f %%i in ("%~d0") do set drive=%%i

which is a simplified version, or go all the way and use:

SET drive=%~d0

which is the normal way to find out the drive from which a batch is started.

In other words, the original problem posted needs not the use of FOR or of FOR /F.

jaclaz

Link to comment
Share on other sites

o u rock jaclaz i understood it clearly .many regards to u...i have 1 more thing to ask suppose i have a folder on desktop "wpi" containing wpi.hta in it and $oem$ folder beside wpi folder containing runwpi batch file. could the same command will b used for opening the wpi.hta.

for /f "delims=: tokens=1" %%i in ("%~dp0") do set drive=%%i:

set wpipath=%drive%\wpi

SET KEY=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnceEx

REG ADD %KEY%\999 /V 1 /D "%wpipath%\WPI.hta" /f

i m using this to run it from a cd but its not working on a desktop what changes should b applied plzzzzzz help me out a little more.thxxx.May God Bless u.

Link to comment
Share on other sites

for /f "delims=: tokens=1" %%i in ("%~dp0") do set drive=%%i:

set wpipath=%drive%\wpi

If you have it on desktop, it is likely to return something like:

C:\Documents and Settings\User\Desktop\\wpi

which BTW has an additional problem, i.e. contains a space, not a problem in itself but may become one further in the script.

Please also understand that BESIDES THE FACT THAT THERE IS NO NEED WHATSOEVER to use a FOR /F loop to get %~dp0, as already said, the result of

Set drive=%~dp0

which is the recommended way, as well as the overcomplex one you chose, will be a path with a trailing backslash, which, consequently, will result in something like:

drive=C:\whatever\

wpipath=C:\whatever\\wpi

which is invalid.

Of course if the batch is in a folder called wpi on desktop, the result of

Set drive=%~dp0

will be:

drive=C:\whatever\wpi

and

wpipath=C:\whatever\wpi\\wpi

jaclaz

Link to comment
Share on other sites

o u rock jaclaz i understood it clearly .many regards to u...i have 1 more thing to ask suppose i have a folder on desktop "wpi" containing wpi.hta in it and $oem$ folder beside wpi folder containing runwpi batch file. could the same command will b used for opening the wpi.hta.

In order to do that all you need is a batch file containing this:

@"%~dp0..\wpi\wpi.hta"

Link to comment
Share on other sites

hizzz yzowl .can u explain what these 2 dot after dp0 in thiz command represents? @"%~dp0..\wpi\wpi.hta" and plz explain the function of this command.ill b realy grateful.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...