Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for vbsassoc.bat

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

  1. @ECHO OFF
  2. :: Check Windows version and command line arguments
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. IF NOT  "%~1"==""           GOTO Syntax
  5.  
  6. :: Allow delayed variable expansion, which we will
  7. :: (ab)use for string substitution on percent signs.
  8. SETLOCAL ENABLEDELAYEDEXPANSION
  9.  
  10. :: Save the current .VBS file type association in a temporary batch
  11. :: file which we can use later to restore the original settings.
  12. FOR /F "tokens=1* delims==" %%A IN ('ASSOC .vbs')       DO SET VbsAssoc=%%B
  13. FOR /F "tokens=1* delims==" %%A IN ('FTYPE %VbsAssoc%') DO SET VbsFType=%%B
  14. SET VbsFType=!VbsFType:%%=%%%%!
  15. IF NOT EXIST restore_vbs_assoc.bat (
  16. 	>  restore_vbs_assoc.bat ECHO @ECHO OFF
  17. 	>> restore_vbs_assoc.bat ECHO FTYPE %VbsAssoc%=%VbsFType%
  18. )
  19.  
  20. :: Find the location of CSCRIPT.EXE in the current directory or in the PATH variable.
  21. :: Simplified code by David Keller.
  22. FOR %%A IN (cscript.exe) DO SET CScript=%%~f$PATH:A
  23.  
  24. :: Change the file type association for .VBS to CSCRIPT.EXE.
  25. IF DEFINED CScript (
  26. 	FTYPE %VbsAssoc%=%CScript% //NoLogo "%%1" %%*
  27. 	SET Error=0
  28. ) ELSE (
  29. 	ECHO CSCRIPT.EXE was not found.
  30. 	ECHO The file type association for .VBS has been left unaltered.
  31. 	SET Error=1
  32. )
  33.  
  34. :: Done.
  35. ENDLOCAL & EXIT /B %Error%
  36.  
  37.  
  38. :Syntax
  39. ECHO.
  40. ECHO VBSAssoc.bat,  Version 1.10 for Windows NT 4 and later
  41. ECHO Associate .VBS files with CSCRIPT.EXE
  42. ECHO.
  43. ECHO Usage:   VBSASSOC.BAT
  44. ECHO.
  45. ECHO Note:    Use "restore_vbs_assoc.bat", created in the current
  46. ECHO          directory, to restore the original file association.
  47. ECHO.
  48. ECHO Credits: Code optimized by David Keller.
  49. ECHO.
  50. ECHO Written by Rob van der Woude
  51. ECHO http://www.robvanderwoude.com
  52.  

page last uploaded: 2017-07-06, 12:37