Doc Symbiosis Posted February 16, 2006 Posted February 16, 2006 (edited) Hi there,I've got a little problem understanding the set command within loops. Here is my script which works fine@ECHO OFFSET wpidir=%~dp0IF %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 February 16, 2006 by Doc Symbiosis
Doc Symbiosis Posted February 16, 2006 Author Posted February 16, 2006 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?
Yzöwl Posted February 17, 2006 Posted February 17, 2006 (edited) how about changing it to this@ECHO OFF &SETLOCAL ENABLEEXTENSIONSSET "wpidir=%~dp0"IF "%~d0" EQU "\\" (FOR /F "DELIMS=" %%? IN ("%~dp0") DO SET "wpipathpart=%%~nx?")IF DEFINED wpipathpart (CALL :GETDRV "%%wpidir:\%wpipathpart%=%%")ECHO %wpidir% &&PAUSEENDLOCAL &GOTO :EOF:GETDRVFOR /F "TOKENS=2" %%? IN ('NET USE * %1 /PERSISTENT:NO ^|FIND "Drive"') DO (SET "wpidir=%%?\%wpipathpart%")GOTO :EOFJust change /remove line 5 to suit your original intention.</Edit> Edited February 17, 2006 by Yzöwl
Doc Symbiosis Posted February 18, 2006 Author Posted February 18, 2006 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 OFFSET "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:setnewpathFOR /F "TOKENS=2" %%i IN ('NET USE * %1 /PERSISTENT:NO ^|FIND "Drive"') DO (SET wpidir=%%i\%2)GOTO :EOFAnyway, I only can say it another time, like so often before: thanks a lot.
Yzöwl Posted February 18, 2006 Posted February 18, 2006 (edited) Thanks Yzöwl for the batch. The IF DEFINED statement doesn't work for meThis may work for you better then!@ECHO OFF &SETLOCAL ENABLEEXTENSIONSSET "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% &&PAUSEGOTO :EOF:GETDRVFOR /F "TOKENS=2" %%? IN ('NET USE * %1 /PERSISTENT:NO ^|FIND "Drive"') DO ( ENDLOCAL SET "wpidir=%%?\%~2")GOTO :EOF Edited February 19, 2006 by Yzöwl
jaclaz Posted February 18, 2006 Posted February 18, 2006 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 ENABLEDELAYEDEXPANSIONhttp://www.robvanderwoude.com/local.htmland here is the details about Why it won't work without delayed expansion disabledhttp://www.robvanderwoude.com/ntset.html#DelayedExpansionWhen you have a doubt about batch files, go here:http://www.robvanderwoude.com/(if there is a solution, normally is there)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