Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for tempnwd.bat

(view source code of tempnwd.bat as plain text)

  1. @ECHO OFF
  2. :: TempNWD.bat,  Version 1.00 for Windows NT
  3. :: Map a network drive temporarily, and only if necessary
  4. :: Written by Rob van der Woude
  5. :: http://www.robvanderwoude.com
  6.  
  7. :: Initialize variables
  8. SET EXISTDRV=
  9. SET UNC=%1
  10. SET SWITCH=%2
  11. IF NOT [%SWITCH%]==[] SET SWITCH=%SWITCH:~0,2%
  12. IF NOT [%SWITCH%]==[] IF /I NOT [%SWITCH%]==[/D] GOTO Syntax
  13. :: Check for forbidden third parameter
  14. IF NOT [%3]==[] GOTO Syntax
  15. :: Check if first parameter is empty
  16. IF [%UNC%]==[] GOTO Syntax
  17. :: Check for 2 leading backslashes
  18. IF NOT %UNC:~0,2%==\\ GOTO Syntax
  19. :: Parse parameter into \\server\share
  20. FOR /F "tokens=1,2* delims=\" %%A IN ('ECHO.%UNC%') DO (
  21. 	SET SERVER=%%A
  22. 	SET SHARE=%%B
  23. 	SET DUMMY=%%C
  24. )
  25. :: Check parsed parameter
  26. IF [%SHARE%]==[] GOTO Syntax
  27. IF NOT [%DUMMY%]==[] GOTO Syntax
  28. :: Check if server is on-line
  29. PING %SERVER% | FIND "TTL=" >NUL
  30. IF ERRORLEVEL 1 GOTO OffLine
  31. :: Check if share exists
  32. DIR \\%SERVER%\%SHARE% >NUL 2>NUL
  33. IF ERRORLEVEL 1 GOTO NoShare
  34.  
  35. :: Check optional second parameter (/D)
  36. IF /I [%SWITCH%]==[/D] GOTO Delete
  37.  
  38. :: Check if a mapping to the specified UNC exists
  39. FOR /F "tokens=2 delims= " %%A IN ('NET USE ^| FIND /I "\\%SERVER%\%SHARE%"') DO (
  40. 	SET EXISTDRV=%%A
  41. 	SET MAPPEDDRV=%%A
  42. 	ECHO Drive %%A already mapped
  43. )
  44. IF [%EXISTDRV%]==[] FOR /F "tokens=2 delims= " %%A IN ('NET USE * \\%SERVER%\%SHARE% /PERSISTENT:NO ^| FIND "Drive"') DO (
  45. 	SET MAPPEDDRV=%%A
  46. 	ECHO Drive %%A is now mapped
  47. )
  48. %MAPPEDDRV%
  49. GOTO End
  50.  
  51. :Delete
  52. :: Remove mapping
  53. IF [%EXISTDRV%]==[] (
  54. 	IF [%MAPPEDDRV%]==[] (
  55. 		ECHO No drive mapping found to remove
  56. 	) ELSE (
  57. 		%SystemDrive%
  58. 		NET USE %MAPPEDDRV% /D
  59. 	)
  60. ) ELSE (
  61. 	ECHO Skipped removal of existing drive %EXISTDRV% mapping
  62. )
  63. GOTO End
  64.  
  65. :NoShare
  66. ECHO.
  67. ECHO Share name invalid or no access to share
  68. GOTO Syntax2
  69.  
  70. :OffLine
  71. ECHO.
  72. ECHO Server name invalid or server is off-line
  73. GOTO Syntax2
  74.  
  75. :Syntax
  76. ECHO.
  77. ECHO TempNwD.bat,  Version 1.00 for Windows NT
  78. ECHO Map a network drive temporarily, if necessary.
  79. ECHO.
  80. ECHO Written by Rob van der Woude
  81. ECHO http://www.robvanderwoude.com
  82.  
  83. :Syntax2
  84. ECHO.
  85. ECHO Usage:  %~n0  \\server\share  [ /D[elete] ]
  86. ECHO.
  87. ECHO If a drive mapping to the specified UNC already exists, then that drive
  88. ECHO mapping is used.
  89. ECHO Used with the /D switch, an existing drive mapping will not be removed,
  90. ECHO only mappings created by this batch file can be removed by this batch
  91. ECHO file.
  92. ECHO.
  93. ECHO Example of usage (assuming yourprog.exe is located in \\server\share\):
  94. ECHO.
  95. ECHO     @echo off
  96. ECHO     call %~n0 \\server\share
  97. ECHO     yourprog.exe
  98. ECHO     call %~n0 \\server\share /D
  99.  
  100. :End
  101.  

page last modified: 2024-04-16; loaded in 0.0082 seconds