Skip to content

Instantly share code, notes, and snippets.

@davidruhmann
Created December 4, 2012 19:08
Show Gist options
  • Save davidruhmann/4207598 to your computer and use it in GitHub Desktop.
Save davidruhmann/4207598 to your computer and use it in GitHub Desktop.
[Batch] Create a shortcut in batch using WSH (WScript)
:: Example Usage for the Shortcut function.
:: Create a shortcut in batch using WSH.
:: by David Ruhmann
:: Hide Commands
@echo off
:: Isolate Scope
setlocal
:: Create a folder to hold the shortcuts created.
set "xTarget=%UserProfile%\Desktop\Shortcuts"
if not exist "%xTarget%" md "%xTarget%"
:: Folder Loop
for /r "%~dp0" %%A in (.) do (
rem Note: Remove the period appended to the variable string, else a file
rem shortcut will be created instead of a directory shortcut. Use "%%~dpfA"
call :Shortcut xResult "%xTarget%\%%~nA" "%%~dpfA"
echo %%~A
)
:: File Loop
for /r "%~dp0" %%A in (*) do (
rem Only create shortcuts for the executable files.
if /i "%%~xA"==".exe" (
call :Shortcut xResult "%xTarget%\%%~nA" "%%~A"
rem echo Example full call
rem call :Shortcut xResult "%%~dpnA" "%%~A" "" "%%~dpA" "%%~nA" "7" "%%~dpA\signerror32b.ico" ""
echo %%~A
)
)
:: Done
pause
goto End
:: COPY THE FUNCTION IMPLEMENTATIONS HERE.
:: ONLY USING Shortcut AND IsDirectory
:: End
:End
endlocal
:: by David Ruhmann
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsDirectory <xReturn> <xPath>
:: Determine if the path specified is a directory.
:: Return true for directories, false on all else.
setlocal
set "xResult=false"
:: This works because only directory paths appended with a backslash will exist.
:: The asterisk is needed to detect symbolic links as directories.
if not "%~2"=="" if exist %2\* set "xResult=true"
endlocal & if not "%~1"=="" set "%~1=%xResult%"
goto :eof
:: by David Ruhmann
:: TODO Add URL support. .url extension instead of .lnk
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Shortcut <xReturn> <xLink> <xTarget> [xArguments] [xDirectory] [xDescription] [xStyle] [xIcon] [xHotKey]
:: Create a Windows shortcut to the target.
:: Return true on shortcut creation, false on failure.
setlocal
:: Setup
set "xVBS=%Temp%\CreateShortcut.vbs"
set "xResult=false"
set "xArguments=%~4"
set "xDescription=%~n3"
set "xDirectory=%~dp3"
set "xHotKey="
set "xIcon=%~3"
set "xStyle=%~7"
:: Validate Required
if "%~2"=="" goto EndShortcut
if "%~3"=="" goto EndShortcut
if exist "%~2" goto EndShortcut
if not exist "%~3" goto EndShortcut
:: Directory Shortcut
call :IsDirectory xDir %3
if "%xDir%"=="true" (
set "xDirectory=%~dpf3"
set "xIcon="
)
:: Set Valid Values
:: Style ^(Normal = 1, Maximized = 3, Minimized = 7^)
:: HotKey TODO
if not "%~5"=="" if exist "%~5" set "xDirectory=%~5"
if not "%~6"=="" set "xDescription=%~6"
if not "%~7"=="1" if not "%~7"=="3" if not "%~7"=="7" set "xStyle=1"
if not "%~8"=="" if exist "%~8" set "xIcon=%~8"
if not "%~9"=="" set "xHotKey=%~9"
:: Create VBS
echo set oWshShell = WScript.CreateObject("WScript.Shell" ) > "%xVBS%"
echo set oShortcut = oWshShell.CreateShortcut("%~2.lnk") >> "%xVBS%"
echo oShortcut.TargetPath = "%~3" >> "%xVBS%"
if not "%xArguments%"=="" echo oShortcut.Arguments = "%xArguments%" >> "%xVBS%"
echo oShortcut.Description = "%xDescription%" >> "%xVBS%"
echo oShortcut.WorkingDirectory = "%xDirectory%" >> "%xVBS%"
echo oShortcut.WindowStyle = %xStyle% >> "%xVBS%"
if not "%xIcon%"=="" echo oShortcut.IconLocation = "%xIcon%" >> "%xVBS%"
if not "%xHotKey%"=="" echo oShortcut.HotKey = "%xHotKey%" >> "%xVBS%"
echo oShortcut.Save >> "%xVBS%"
:: Run VBS
if exist "%xVBS%" (
"%xVBS%"
if exist "%~2.lnk" set "xResult=true"
rem pause
del "%xVBS%"
)
:EndShortcut
endlocal & if not "%~1"=="" set "%~1=%xResult%"
goto :eof
:: by David Ruhmann
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ShortcutFolder <xReturn> <xLink> <xTarget> [xArguments] [xDirectory] [xDescription] [xStyle] [xIcon] [xHotKey]
:: DO NOT USE THIS UNLESS THE REGULAR SHORTCUT FUNCTION WILL NOT WORK FOR DIRECTORIES.
:: Create a windows shortcut to the target folder.
:: Return true on shortcut creation, false on failure.
setlocal
:: Setup
set "xResult=false"
set "xINI=%~2\Desktop.ini"
:: Alternative Folder Shortcut Method
:: 1. Create a folder with the shortcut name.
:: 2. Create a Desktop.ini file inside the new folder.
:: 3. Create a normal shortcut file target.lnk inside the new folder.
:: 4. Change the new folder to a read-only and or system folder.
:: Note: to change the shortcut back into a folder, undo step 4.
:: Validate
if not "%~2"=="" if not exist "%~2" if not "%~3"=="" if exist "%~3" (
md "%~2"
if exist "%~2" (
echo.[.ShellClassInfo] > %xINI%
echo.CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D} >> %xINI%
echo.Flags=2 >> %xINI%
call :Shortcut xResult "%~2\target.lnk" %3 %4 %5 %6 %7 %8 %9
attrib +r +s "%~2"
)
)
endlocal & if not "%~1"=="" set "%~1=%xResult%"
goto :eof
:: by David Ruhmann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment