Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

For Syntax is driving me crazy ...

Featured Replies

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

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

or

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

?

maybe :blushing:

Try the following.

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

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.

I would use Scr1ptW1zard code , but using

^| FIND ":"

instead of

^|find /i "%SHARE%"

The colon should be present only in the desired line...:unsure:

jaclaz

  • Author

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

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

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.