-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_desktop_shortcut.bat
77 lines (70 loc) · 2.45 KB
/
create_desktop_shortcut.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@echo off
setlocal
set "target=%~dp0run.bat"
set "icon=%~dp0main\icon.ico"
set "shortcut=%USERPROFILE%\Desktop\AutoSubSync.lnk"
set "shortcutCurrent=%~dp0AutoSubSync.lnk"
echo Creating desktop shortcut...
:: Try PowerShell method
powershell -NoProfile -Command ^
"$s = New-Object -ComObject WScript.Shell; " ^
"$sc = $s.CreateShortcut('%shortcut%'); " ^
"$sc.TargetPath = '%target%'; " ^
"$sc.IconLocation = '%icon%'; " ^
"$sc.Save()"
if errorlevel 1 (
echo PowerShell method failed. Trying another method...
goto vbscript
) else (
echo Shortcut created successfully.
goto createCurrent
)
:vbscript
echo Creating desktop shortcut...
echo Set oWS = WScript.CreateObject("WScript.Shell") > "%temp%\create_shortcut.vbs"
echo Set oLink = oWS.CreateShortcut("%shortcut%") >> "%temp%\create_shortcut.vbs"
echo oLink.TargetPath = "%target%" >> "%temp%\create_shortcut.vbs"
echo oLink.IconLocation = "%icon%" >> "%temp%\create_shortcut.vbs"
echo oLink.Save >> "%temp%\create_shortcut.vbs"
cscript //nologo "%temp%\create_shortcut.vbs"
del "%temp%\create_shortcut.vbs"
if exist "%shortcut%" (
echo Shortcut created successfully.
) else (
echo Failed to create shortcut.
)
:createCurrent
echo Creating shortcut in current folder...
:: Try PowerShell method
powershell -NoProfile -Command ^
"$s = New-Object -ComObject WScript.Shell; " ^
"$sc = $s.CreateShortcut('%shortcutCurrent%'); " ^
"$sc.TargetPath = '%target%'; " ^
"$sc.IconLocation = '%icon%'; " ^
"$sc.Save()"
if errorlevel 1 (
echo PowerShell method failed. Trying another method...
goto vbscriptCurrent
) else (
echo Shortcut created successfully.
goto end
)
:vbscriptCurrent
echo Creating shortcut in current folder...
echo Set oWS = WScript.CreateObject("WScript.Shell") > "%temp%\create_shortcut_current.vbs"
echo Set oLink = oWS.CreateShortcut("%shortcutCurrent%") >> "%temp%\create_shortcut_current.vbs"
echo oLink.TargetPath = "%target%" >> "%temp%\create_shortcut_current.vbs"
echo oLink.IconLocation = "%icon%" >> "%temp%\create_shortcut_current.vbs"
echo oLink.Save >> "%temp%\create_shortcut_current.vbs"
cscript //nologo "%temp%\create_shortcut_current.vbs"
del "%temp%\create_shortcut_current.vbs"
if exist "%shortcutCurrent%" (
echo Shortcut created successfully.
) else (
echo Failed to create shortcut.
)
:end
echo.
echo Press any key to exit...
pause >nul
endlocal