Jump to content

For Syntax is driving me crazy ...


 Share

Recommended Posts

Hi guys,

Spent hours but can´t get it to work.

Here´s the code:

@echo off
SETLOCAL ENABLEEXTENSIONS

set share=\\server\share
set username=server\username
set password=password

FOR /f "usebackq tokens=2" %%i IN (`net use * %SHARE% %PASSWORD% /user:%USERNAME% /PERSISTENT:NO`) DO @echo %%i

I 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:

Befehl

The 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

Link to comment
Share on other sites


The code you are looking for is more than likely something like this:

@echo off&setlocal
for /f "tokens=2" %%? in (
'net use * \\servername\sharename^|find /i "drive"'
) do echo:%%?

or maybe:

@echo off&setlocal
for /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>
popd

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

Link to comment
Share on other sites

Happy Sunday, guys !

Thanks for your help, I really, really appreciate it !! :hello:

@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 off
SETLOCAL ENABLEEXTENSIONS

set zone=server
set share=\\server\share
set username=server\user
set password=password

FOR /f "usebackq tokens=2" %%i IN (`net use * %SHARE% %PASSWORD% /user:%USERNAME% /PERSISTENT:NO^|find /i "%SHARE%"`) DO SET DRIVE=%%i

if exist %DRIVE%\Installation goto servermode


:localmode
ECHO Running Installation from DVD Drive ...
start "" /wait "%DVDRoot%\WPI\WPI.hta"
exit


:servermode

REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\%ZONE%" /V "file" /T "REG_DWORD" /D "1" /F
REG ADD "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\%ZONE%" /V "file" /T "REG_DWORD" /D "1" /F

REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap" /V "AutoDetect" /T "REG_DWORD" /D "0" /F
REG ADD "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap" /V "AutoDetect" /T "REG_DWORD" /D "0" /F

REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1" /V "Flags" /T "REG_DWORD" /D "219" /F
REG ADD "HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1" /V "Flags" /T "REG_DWORD" /D "219" /F

IF EXIST "%DRIVE%\Installation\Programs\Programs_Privat\wpiconfig.js" REG ADD "HKCU\Software\WPI" /V "ConfigFile" /D "%DRIVE%\Installation\Programs\Programs_Privat\wpiconfig.js" /F
if not exist "%DRIVE%\Installation\Programs\Programs_Privat\wpiconfig.js" goto localmode

ECHO Running Installation from Server ...

start "" /wait "%DRIVE%\Installation\WPI\WPI.hta"
exit

This 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

Link to comment
Share on other sites

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_Razor

I 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 by jaclaz
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.


×
×
  • Create New...