Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for desktop.bat

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

  1. @ECHO OFF
  2. :: Check command line and operating system
  3. IF NOT [%1]==[] GOTO Syntax
  4. VER | FIND "Windows NT" >NUL
  5. IF NOT ERRORLEVEL 1 GOTO Go
  6. VER | FIND "Windows 2000" >NUL
  7. IF ERRORLEVEL 1 GOTO Syntax
  8.  
  9. :Go
  10. :: Keep all variables local
  11. SETLOCAL
  12.  
  13. :: Just for the sake of readability
  14. SET tab=	% Place TAB between equal sign and first percent sign %
  15.  
  16. :: Read Desktop directory from registry
  17. FOR /F "tokens=2* delims=%tab% " %%A IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop"') DO SET desktopfolder=%%B
  18.  
  19. :: Change current directory to Desktop directory
  20. PUSHD %desktopfolder%
  21.  
  22. :: Create a batch file that will recreate the current Desktop's shortcuts
  23. ECHO Saving the current desktop to %~dp0recreate.cmd
  24. ECHO @ECHO OFF>%~dp0recreate.cmd
  25.  
  26. :: Save all Desktop shortcuts' properties in this batch file
  27. FOR %%A IN (*.lnk) DO CALL :Read "%%A"
  28.  
  29. :: Check if the batch file was actually created
  30. IF EXIST %~dp0recreate.cmd (ECHO Use %~dp0recreate.cmd to restore the current desktop) ELSE (ECHO Error creating %~dp0recreate.cmd)
  31.  
  32. :: Restore current directory
  33. POPD
  34. ENDLOCAL
  35. GOTO:EOF
  36.  
  37.  
  38. :Read
  39. SETLOCAL
  40. SET shortcut=%*
  41. :: Strip leading space for NT 4 only
  42. VER | FIND "Windows NT" >NUL
  43. IF NOT ERRORLEVEL 1 SET shortcut=%shortcut:~1%
  44. :: Read a shortcuts' properties . . .
  45. FOR /F "tokens=1* delims=:" %%a IN ('SHORTCUT.EXE -u all %shortcut% 2^>^&1') DO SET %%a=%%b
  46. :: . . . and save them in the batch file
  47. CALL :Store
  48. ENDLOCAL
  49. GOTO:EOF
  50.  
  51.  
  52. :Store
  53. :: Check if an error message was generated by SHORTCUT.EXE
  54. IF DEFINED Error GOTO :Error
  55. ::
  56. :: Check all possible shortcut properties:
  57. :: LinkName
  58. IF "%LinkName%"==" <none>" SET LinkName=
  59. IF NOT DEFINED LinkName GOTO :Error
  60. SET ExecParams=-n%LinkName%
  61. ::
  62. :: Arguments
  63. IF "%Arguments%"==" <none>" SET Arguments=
  64. IF NOT DEFINED Arguments GOTO :Target
  65. SET ExecParams=%ExecParams% -a%Arguments%
  66. ::
  67. :Target
  68. IF "%Target%"==" <none>" SET Target=
  69. IF NOT DEFINED Target GOTO :Error
  70. SET ExecParams=%ExecParams% -t%Target%
  71. ::
  72. :: Working Directory
  73. IF "%Working Directory%"==" <none>" SET Working Directory=
  74. IF "%Working Directory%"=="" GOTO :IconFile
  75. SET ExecParams=%ExecParams% -d%Working Directory%
  76. ::
  77. :IconFile
  78. IF "%Icon File%"==" <none>" SET Icon File=
  79. IF "%Icon File%"=="" GOTO :IconIndex
  80. SET ExecParams=%ExecParams% -i%Icon File%
  81. ::
  82. :IconIndex
  83. IF "%Icon Index%"==" <none>" SET Icon Index=
  84. IF "%Icon Index%"=="" GOTO :CreateCommand
  85. SET ExecParams=%ExecParams% -x%Icon Index%
  86. ::
  87. :CreateCommand
  88. ECHO SHORTCUT.EXE -s %ExecParams% >>%~dp0recreate.cmd
  89. GOTO:EOF
  90. ::
  91. :Error
  92. ECHO Unable to save settings for shortcut %Shortcut%
  93. IF DEFINED Error SET Error
  94. ECHO.
  95. PAUSE
  96. GOTO:EOF
  97.  
  98.  
  99. :Syntax
  100. ECHO.
  101. ECHO Desktop,  Version 2.00 for Windows NT 4 / 2000
  102. ECHO Creates an emergency batch file that will recreate the current Desktop
  103. ECHO Written by Rob van der Woude
  104. ECHO http://www.robvanderwoude.com
  105. ECHO.
  106. ECHO Usage:  %~n0
  107. ECHO.
  108. ECHO Creates a batch file, recreate.cmd, that recreates the current Desktop's
  109. ECHO shortcuts with the correct properties.
  110. ECHO The file recreate.cmd will be saved in the directory where %~nx0 is
  111. ECHO located.
  112. ECHO The batch file will pause with a message if it is unable to recreate one
  113. ECHO or more shortcuts, but will continue after any key is pressed.
  114. ECHO This batch file will only handle shortcuts on the desktop itself, not in
  115. ECHO subfolders.
  116.  

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