Jump to content

Recommended Posts

Posted (edited)

Hi there,

I've got a little problem understanding the set command within loops. Here is my script which works fine

@ECHO OFF
SET wpidir=%~dp0

IF %wpidir:~0,2%!==\\! (
REM Get the name of the share, assuming that it is not longer than 50 characters
FOR /F "tokens=1,2* delims=\" %%i IN ('ECHO %~dp0') DO (
SET share=\\%%i\%%j
SET wpipathpart=%%k
)
)
IF %wpidir:~0,2%!==\\! (
REM connect to the last free drive letter and write it to drvlet
FOR /f "usebackq tokens=2* eol=" %%i IN (`net use * %share% /PERSISTENT:NO`) DO (
SET drvlet=%%i
GOTO endfor
)
:endfor
SET wpidir=%drvlet%\%wpipathpart%
)
)
ECHO %wpidir%
Set drvlet=
Set share=
Set wpipathpart=

This script just checks, if the batch is called with UNC path and in this case connects the share to last free drive letter and sets the directory adequate. Problem is, that when I remove the lines 11 and 12 ( cause two if statements with same condition aren't that nice )

)
IF %wpidir:~0,2%!==\\! (

the %share% variable isn't set within the loop. Anyone got an idea, how I can fix this and what's going wrong?

Edited by Doc Symbiosis

Posted

Just found a way, which is before calling the batch, running the %COMSPEC% /V:ON command.

I wonder, if it is possible to activate delayed environment variable expansion directly in the batch?

Posted (edited)

how about changing it to this

@ECHO OFF &SETLOCAL ENABLEEXTENSIONS
SET "wpidir=%~dp0"
IF "%~d0" EQU "\\" (FOR /F "DELIMS=" %%? IN ("%~dp0") DO SET "wpipathpart=%%~nx?")
IF DEFINED wpipathpart (CALL :GETDRV "%%wpidir:\%wpipathpart%=%%")
ECHO %wpidir% &&PAUSE
ENDLOCAL &GOTO :EOF
:GETDRV
FOR /F "TOKENS=2" %%? IN ('NET USE * %1 /PERSISTENT:NO ^|FIND "Drive"') DO (SET "wpidir=%%?\%wpipathpart%")
GOTO :EOF

Just change /remove line 5 to suit your original intention.

</Edit>

Edited by Yzöwl
Posted

Thanks Yzöwl for the batch. The IF DEFINED statement doesn't work for me, but your batch helped me to modify my own batch with using the call command. It now looks like this( I removed the setlocal, because I really want to set wpidir as environment variable

@ECHO OFF
SET "wpidir=%~dp0"
IF "%~d0" EQU "\\" (
REM Get the name of the share, assuming that it is not longer than 50 characters
FOR /F "tokens=1,2* delims=\" %%i IN ('ECHO %~dp0') DO (
IF NOT %%k!==! (
call :setnewpath \\%%i\%%j "%%k"
) ELSE (
call :setnewpath \\%%i\%%j
)
)
)
ECHO %wpidir%
GOTO :EOF

:setnewpath
FOR /F "TOKENS=2" %%i IN ('NET USE * %1 /PERSISTENT:NO ^|FIND "Drive"') DO (SET wpidir=%%i\%2)
GOTO :EOF

Anyway, I only can say it another time, like so often before: thanks a lot.

Posted (edited)
Thanks Yzöwl for the batch. The IF DEFINED statement doesn't work for me
This may work for you better then!
@ECHO OFF &SETLOCAL ENABLEEXTENSIONS
SET "wpidir=%~dp0" &SET "wpipathpart="
IF "%~d0" EQU "\\" (
FOR /F "DELIMS=" %%? IN ("%wpidir%") DO SET "wpipathpart=%%~nx?")
IF "%wpipathpart%" NEQ "" CALL :GETDRV "%%wpidir:\%wpipathpart%=%%" "%wpipathpart%"
ECHO %wpidir% &&PAUSE
GOTO :EOF
:GETDRV
FOR /F "TOKENS=2" %%? IN ('NET USE * %1 /PERSISTENT:NO ^|FIND "Drive"') DO (
ENDLOCAL
SET "wpidir=%%?\%~2")
GOTO :EOF

Edited by Yzöwl
Posted
Just found a way, which is before calling the batch, running the %COMSPEC% /V:ON command.

I wonder, if it is possible to activate delayed environment variable expansion directly in the batch?

Yep, just put a statement in it like this:

SETLOCAL ENABLEDELAYEDEXPANSION

http://www.robvanderwoude.com/local.html

and here is the details about Why it won't work without delayed expansion disabled

http://www.robvanderwoude.com/ntset.html#DelayedExpansion

When you have a doubt about batch files, go here:

http://www.robvanderwoude.com/

(if there is a solution, normally is there)

jaclaz

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