midiboy Posted November 17, 2007 Posted November 17, 2007 Hi guys,Spent hours but can´t get it to work.Here´s the code:@echo offSETLOCAL ENABLEEXTENSIONSset share=\\server\shareset username=server\usernameset password=passwordFOR /f "usebackq tokens=2" %%i IN (`net use * %SHARE% %PASSWORD% /user:%USERNAME% /PERSISTENT:NO`) DO @echo %%iI wish to get the driveletter (and only the driveletter) variable out of the command but the result of the above is the driveletter variable + the second word of the second line that is output by the net use command.In my case (German Windows version) I get the following as output:Z:BefehlThe first is the driveletter, the second is the second word of this output:Laufwerk Z: ist jetzt mit \\server\share verbunden.Der Befehl wurde erfolgreich durchgeführt.Translated this means: Drive Z: is now connected to \\server\share.The command finished successfully.Does anyone know how I can only get the driveletter ? I now experimented hours with various combinations of tokens, delims and eol´s but I could not get it to work.Thanks a lot for any help !Alex
SilverBulletUK Posted November 17, 2007 Posted November 17, 2007 FOR /f "usebackq tokens=1" %%i IN (`net use * %SHARE% %PASSWORD% /user:%USERNAME% /PERSISTENT:NO`) DO @echo %%iorFOR /f "usebackq delims= "" tokens=1" %%i IN (`net use * %SHARE% %PASSWORD% /user:%USERNAME% /PERSISTENT:NO`) DO @echo %%i?maybe
SilverBulletUK Posted November 17, 2007 Posted November 17, 2007 **** it, I should really test this stuff before I post. !! Back to the command line I go..Sorry!
Scr1ptW1zard Posted November 18, 2007 Posted November 18, 2007 Try the following.FOR /f "usebackq tokens=2" %%i IN (`net use * %SHARE% %PASSWORD% /user:%USERNAME% /PERSISTENT:NO^|find /i "%SHARE%"`) DO @echo %%i
Yzöwl Posted November 18, 2007 Posted November 18, 2007 The code you are looking for is more than likely something like this:@echo off&setlocalfor /f "tokens=2" %%? in ( 'net use * \\servername\sharename^|find /i "drive"' ) do echo:%%?or maybe:@echo off&setlocalfor /f "delims=:" %%? in ( 'net use^|find /i "\\servername\sharename"' ) do echo:%%?:I would like to know if you really need the drive letter. If you just wish to perform a task which has UNC path issues then you can use:pushd \\servername\sharename<your code goes here>popdThis would map the drive, typically to Z: allow you to work in it, then it would move you out of the mapping and delete it.
jaclaz Posted November 18, 2007 Posted November 18, 2007 I would use Scr1ptW1zard code , but using^| FIND ":"instead of ^|find /i "%SHARE%"The colon should be present only in the desired line...jaclaz
midiboy Posted November 18, 2007 Author Posted November 18, 2007 Happy Sunday, guys !Thanks for your help, I really, really appreciate it !! @Scr1ptW1zard and jaclaz:Yes, both variations work. Seems to not make a difference if I have find ":" or find "%share%" in there.Why do you think looking for ":" would be better, jaclaz ?@Yzöwl:Thanks, will try your solution too. As for why I need the driveletter. During my unattended installation from DVD I would like to test network connectivity to a server in audit phase (vista) or runonceex phase (XP). If a certain share on a server exists, run all following installations through WPI from the network instead of from DVD.Now the problem with pushd/popd is that it never worked because the accessrights were not set and as far as I know, it is not possible to do that with pushd.So now, I have thought of doing this:@echo offSETLOCAL ENABLEEXTENSIONSset zone=serverset share=\\server\shareset username=server\userset password=passwordFOR /f "usebackq tokens=2" %%i IN (`net use * %SHARE% %PASSWORD% /user:%USERNAME% /PERSISTENT:NO^|find /i "%SHARE%"`) DO SET DRIVE=%%iif exist %DRIVE%\Installation goto servermode:localmodeECHO Running Installation from DVD Drive ...start "" /wait "%DVDRoot%\WPI\WPI.hta"exit:servermodeREG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\%ZONE%" /V "file" /T "REG_DWORD" /D "1" /FREG ADD "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\%ZONE%" /V "file" /T "REG_DWORD" /D "1" /FREG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap" /V "AutoDetect" /T "REG_DWORD" /D "0" /FREG ADD "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap" /V "AutoDetect" /T "REG_DWORD" /D "0" /FREG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1" /V "Flags" /T "REG_DWORD" /D "219" /FREG ADD "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1" /V "Flags" /T "REG_DWORD" /D "219" /FIF EXIST "%DRIVE%\Installation\Programs\Programs_Privat\wpiconfig.js" REG ADD "HKCU\Software\WPI" /V "ConfigFile" /D "%DRIVE%\Installation\Programs\Programs_Privat\wpiconfig.js" /Fif not exist "%DRIVE%\Installation\Programs\Programs_Privat\wpiconfig.js" goto localmodeECHO Running Installation from Server ...start "" /wait "%DRIVE%\Installation\WPI\WPI.hta"exitThis was the only way to successfully solve the problem so far. But I am very open to suggestions. Especially towards hiding the cleartext password from the cmd file. I ´d rather not have it in there but what other way is there ???Thanks,Alex
jaclaz Posted November 18, 2007 Posted November 18, 2007 (edited) Why do you think looking for ":" would be better, jaclaz ?I didn't say it was "better", just what I would have used.Basing myself on Occam's Razor:http://en.wikipedia.org/wiki/Occam's_RazorI endeavour to try and simplify things as much as possible. I avoid using the /I (case insensitive) switch and the %share% variable expansion.Also, you could avoid using the "usebacq" and use "normal" ' (single quote):FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]FOR /F ["options"] %variable IN ("string") DO command [command-parameters]FOR /F ["options"] %variable IN ('command') DO command [command-parameters] or, if usebackq option present:FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]FOR /F ["options"] %variable IN ('string') DO command [command-parameters]FOR /F ["options"] %variable IN (`command`) DO command [command-parameters] jaclaz Edited November 18, 2007 by jaclaz
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now