how to create a shortcut for a exe from a batch file.
如何从批处理文件创建exe的快捷方式。
i tried
call link.bat "c:\program Files\App1\program1.exe" "C:\Documents and Settings\%USERNAME%\Desktop" "C:\Documents and Settings\%USERNAME%\Start Menu\Programs" "Program1 shortcut"
but it did not worked.
但它没有奏效。
link.bat can be found at http://www.robvanderwoude.com/amb_shortcuts.html
link.bat可以在http://www.robvanderwoude.com/amb_shortcuts.html找到
9 个解决方案
#1
23
Your link points to a Windows 95/98 version and I guess you have at least Windows 2000 or XP. You should try the NT version here.
你的链接指向Windows 95/98版本,我猜你至少有Windows 2000或XP。你应该在这里尝试NT版本。
Alternatively use a little VBScript that you can call from the command line:
或者使用一些可以从命令行调用的VBScript:
set objWSHShell = CreateObject("WScript.Shell")
set objFso = CreateObject("Scripting.FileSystemObject")
' command line arguments
' TODO: error checking
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sWorkingDirectory = objFso.GetAbsolutePathName(sShortcut)
set objSC = objWSHShell.CreateShortcut(sShortcut)
objSC.TargetPath = sTargetPath
objSC.WorkingDirectory = sWorkingDirectory
objSC.Save
Save the file as createLink.vbs and call it like this to get what you originally tried:
将文件另存为createLink.vbs并像这样调用以获得您最初尝试的内容:
cscript createLink.vbs "C:\Documents and Settings\%USERNAME%\Desktop\Program1 shortcut.lnk" "c:\program Files\App1\program1.exe"
cscript createLink.vbs "C:\Documents and Settings\%USERNAME%\Start Menu\Programs\Program1 shortcut.lnk" "c:\program Files\App1\program1.exe"
That said I urge you not to use hardcoded paths like "Start Menu" since they're different in localized versions of windows. Modify the script instead to use special folders.
这就是说我敦促你不要使用像“开始菜单”这样的硬编码路径,因为它们在Windows的本地化版本中是不同的。修改脚本而不是使用特殊文件夹。
#2
12
This is the kind of thing that PowerShell is really good at, and is therefore a reason to eschew batch files and get on PowerShell the bandwagon.
这是PowerShell真正擅长的事情,因此是避免批处理文件并使PowerShell成为潮流的理由。
PowerShell can talk to .NET. For example, you can get the location of the Desktop like this:
PowerShell可以与.NET通信。例如,您可以像这样获取桌面的位置:
[Environment]::GetFolderPath("Desktop")
PowerShell can talk to COM objects, including WScript.Shell
, which can create shortcuts:
PowerShell可以与COM对象通信,包括可以创建快捷方式的WScript.Shell:
New-Object -ComObject WScript.Shell).CreateShortcut( ... )
So your script might look like:
所以你的脚本可能看起来像:
$linkPath = Join-Path ([Environment]::GetFolderPath("Desktop")) "MyShortcut.lnk"
$targetPath = Join-Path ([Environment]::GetFolderPath("ProgramFiles")) "MyCompany\MyProgram.exe"
$link = (New-Object -ComObject WScript.Shell).CreateShortcut( $linkpath )
$link.TargetPath = $targetPath
$link.Save()
Shortcuts have a lot of settings that WScript.Shell can't manipulate, like the "run as administrator" option. These are only accessible through the Win32 interface IShellLinkDataList
, which is a real pain to use, but it can be done.
快捷方式有很多WScript.Shell无法操作的设置,例如“以管理员身份运行”选项。这些只能通过Win32接口IShellLinkDataList访问,这是一个真正的痛苦,但它可以做到。
#3
10
Using vbscript:
set WshShell = WScript.CreateObject("WScript.Shell" )
strDesktop = WshShell.SpecialFolders("AllUsersDesktop" )
set oShellLink = WshShell.CreateShortcut(strDesktop & "\shortcut name.lnk" )
oShellLink.TargetPath = "c:\application folder\application.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "c:\application folder\application.ico"
oShellLink.Description = "Shortcut Script"
oShellLink.WorkingDirectory = "c:\application folder"
oShellLink.Save
Ref: http://www.tomshardware.com/forum/52871-45-creating-desktop-shortcuts-command-line
Failing that, a quick google search shows there's a number of third party tools that can create .lnk files for application shortcuts. I'm assuming you need to stick to stuff that's available natively on Windows though? VBscript is probably your best bet, otherwise I'd suggest trying copying the .lnk file from your machine or using it as a sample to see the correct format for a shortcut file.
如果做不到这一点,快速谷歌搜索会显示有许多第三方工具可以为应用程序快捷方式创建.lnk文件。我假设您需要坚持在Windows上原生可用的东西吗? VBscript可能是你最好的选择,否则我建议尝试从你的机器上复制.lnk文件或使用它作为样本来查看快捷方式文件的正确格式。
#4
3
On XP I wrote makeshortcut.vbs
在XP上我写了makehortcut.vbs
Set oWS = WScript.CreateObject("WScript.Shell")
If wscript.arguments.count < 4 then
WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath arguments workingDir "
WScript.Quit
end If
shortcutPath = wscript.arguments(0) & ".LNK"
targetPath = wscript.arguments(1)
arguments = wscript.arguments(2)
workingDir = wscript.arguments(3)
WScript.Echo "Creating shortcut " & shortcutPath & " targetPath=" & targetPath & " arguments=" & arguments & " workingDir=" & workingDir
Set oLink = oWS.CreateShortcut(shortcutPath)
oLink.TargetPath = targetPath
oLink.Arguments = arguments
' oLink.Description = "MyProgram"
' oLink.HotKey = "ALT+CTRL+F"
' oLink.IconLocation = "C:\Program Files\MyApp\MyProgram.EXE, 2"
' oLink.WindowStyle = "1"
oLink.WorkingDirectory = workingDir
oLink.Save
It takes exactly 4 args, so it could be improved by making the later 2 optional.I only post because it echo's usage, which might be useful to some. I like WS's soln using special folders and ExpandEnvironmentStrings
它只需要4个args,所以可以通过使后两个可选来改进它。我只发布因为它的回声用法,这可能对某些人有用。我喜欢WS的soln使用特殊文件夹和ExpandEnvironmentStrings
#5
1
Additional note: the link.bat you're using is for Windows 95/98 only:
附加说明:您使用的link.bat仅适用于Windows 95/98:
This batch file is for Windows 95/98 only. I will post the NT equivalent in the NT newsgroup soon.
此批处理文件仅适用于Windows 95/98。我很快就会在NT新闻组中发布NT等价物。
NT version is posted at http://www.robvanderwoude.com/amb_shortcutsnt.html instead. You might try that for a .bat approach if preferred over vbscript.
NT版本发布在http://www.robvanderwoude.com/amb_shortcutsnt.html上。如果优于vbscript,您可以尝试使用.bat方法。
#6
1
Alternative method, using a third party utility:
替代方法,使用第三方实用程序:
Creating a Shortcut from the command line (batch file)
从命令行创建快捷方式(批处理文件)
XXMKLINK:
With XXMKLINK, you can write a batch file for software installation which has been done by specialized installation programs. Basically, XXMKLINK is a tool that gathers various information from a command line and packages it into a shortcut.
使用XXMKLINK,您可以编写用于软件安装的批处理文件,该文件由专门的安装程序完成。基本上,XXMKLINK是一种从命令行收集各种信息并将其打包成快捷方式的工具。
xxmklink spath opath
where
spath path of the shortcut (.lnk added as needed)
opath path of the object represented by the shortcut
#7
1
EDIT 24.6.14 - Following functionalities added: -Edit shortcut -List properties of a shortcut -Set/Remove "Run as administrator" thick
编辑24.6.14 - 添加了以下功能: - 编辑快捷方式 - 快捷方式的列表属性 - 设置/删除“以管理员身份运行”厚
Here a maintained version of the script can be found
这里可以找到脚本的维护版本
When using Windows Script Host I prefer jscript as it allows creating hybrid files with neither toxic messages nor temporary files.Here's my shortcutJS.bat (you can named it as you like ) allowing you to use all shortcut properties:
使用Windows脚本主机时,我更喜欢使用jscript,因为它允许创建既没有有毒消息也没有临时文件的混合文件。这是我的shortcutJS.bat(您可以根据需要命名),允许您使用所有快捷方式属性:
@if (@X)==(@Y) @end /* JScript comment
@echo off
cscript //E:JScript //nologo "%~f0" "%~nx0" %*
exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */
var args=WScript.Arguments;
var scriptName=args.Item(0);
//var adminPermissions= false;
var edit= false;
function printHelp() {
WScript.Echo(scriptName + " -linkfile link -target target [-linkarguments linkarguments] "+
" [-description description] [-iconlocation iconlocation] [-hotkey hotkey] "+
" [-windowstyle 1|3|7] [-workingdirectory workingdirectory] [-adminpermissions yes|no]");
WScript.Echo();
WScript.Echo(scriptName + " -edit link [-target target] [-linkarguments linkarguments] "+
" [-description description] [-iconlocation iconlocation] [-hotkey hotkey] "+
" [-windowstyle 1|3|7] [-workingdirectory workingdirectory] [-adminpermissions yes|no]");
WScript.Echo();
WScript.Echo(scriptName + " -examine link");
WScript.Echo();
WScript.Echo(" More info: http://msdn.microsoft.com/en-us/library/xk6kst2k%28v=vs.84%29.aspx ");
}
// reads the given .lnk file as a char array
function getlnkChars(lnkPath) {
// :: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855&start=15&p=28898 ::
var ado = WScript.CreateObject("ADODB.Stream");
ado.Type = 2; // adTypeText = 2
ado.CharSet = "iso-8859-1"; // code page with minimum adjustments for input
ado.Open();
ado.LoadFromFile(lnkPath);
var adjustment = "\u20AC\u0081\u201A\u0192\u201E\u2026\u2020\u2021" +
"\u02C6\u2030\u0160\u2039\u0152\u008D\u017D\u008F" +
"\u0090\u2018\u2019\u201C\u201D\u2022\u2013\u2014" +
"\u02DC\u2122\u0161\u203A\u0153\u009D\u017E\u0178" ;
var fs = new ActiveXObject("Scripting.FileSystemObject");
var size = (fs.getFile(lnkPath)).size;
var lnkBytes = ado.ReadText(size);
ado.Close();
var lnkChars=lnkBytes.split('');
for (var indx=0;indx<size;indx++) {
if ( lnkChars[indx].charCodeAt(0) > 255 ) {
lnkChars[indx] = String.fromCharCode(128 + adjustment.indexOf(lnkChars[indx]));
}
}
return lnkChars;
}
function hasAdminPermissions(lnkPath) {
return (getlnkChars(lnkPath))[21].charCodeAt(0) == 32 ;
}
function setAdminPermissions(lnkPath , flag) {
lnkChars=getlnkChars(lnkPath);
var ado = WScript.CreateObject("ADODB.Stream");
ado.Type = 2; // adTypeText = 2
ado.CharSet = "iso-8859-1"; // right code page for output (no adjustments)
//ado.Mode=2;
ado.Open();
// setting the 22th byte to 32
if (flag) {
lnkChars[21]=String.fromCharCode(32);
} else {
lnkChars[21]=String.fromCharCode(0);
}
ado.WriteText(lnkChars.join(""));
ado.SaveToFile(lnkPath, 2);
ado.Close();
}
function examine(lnkPath) {
var fs = new ActiveXObject("Scripting.FileSystemObject");
if (!fs.FileExists(lnkPath)) {
WScript.Echo("File " + lnkPath + " does not exist");
WScript.Quit(2);
}
var oWS = new ActiveXObject("WScript.Shell");
var oLink = oWS.CreateShortcut(lnkPath);
WScript.Echo("");
WScript.Echo(lnkPath + " properties:");
WScript.Echo("");
WScript.Echo("Target: " + oLink.TargetPath);
WScript.Echo("Icon Location: " + oLink.IconLocation);
WScript.Echo("Description: " + oLink.Description);
WScript.Echo("Hotkey: " + oLink.HotKey );
WScript.Echo("Working Directory: " + oLink.WorkingDirectory);
WScript.Echo("Window style: " + oLink.WindowStyle);
WScript.Echo("Admin Permissions: " + hasAdminPermissions(lnkPath));
WScript.Quit(0);
}
if (WScript.Arguments.Length==1 || args.Item(1).toLowerCase() == "-help" || args.Item(1).toLowerCase() == "-h" ) {
printHelp();
WScript.Quit(0);
}
if (WScript.Arguments.Length % 2 == 0 ) {
WScript.Echo("Illegal arguments ");
printHelp();
WScript.Quit(1);
}
if ( args.Item(1).toLowerCase() == "-examine" ) {
var linkfile = args.Item(2);
examine(linkfile);
}
if ( args.Item(1).toLowerCase() == "-edit" ) {
var linkfile = args.Item(2);
edit=true;
}
if(!edit) {
for (var arg = 1;arg<5;arg=arg+2) {
if ( args.Item(arg).toLowerCase() == "-linkfile" ) {
var linkfile = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-target") {
var target = args.Item(arg+1);
}
}
}
if (typeof linkfile === 'undefined') {
WScript.Echo("Link file not defined");
printHelp();
WScript.Quit(2);
}
if (typeof target === 'undefined' && !edit) {
WScript.Echo("Target not defined");
printHelp();
WScript.Quit(3);
}
var oWS = new ActiveXObject("WScript.Shell");
var oLink = oWS.CreateShortcut(linkfile);
if(typeof target === 'undefined') {
var startIndex=3;
} else {
var startIndex=5;
oLink.TargetPath = target;
}
for (var arg = startIndex ; arg<args.Length;arg=arg+2) {
if (args.Item(arg).toLowerCase() == "-linkarguments") {
oLink.Arguments = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-description") {
oLink.Description = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-hotkey") {
oLink.HotKey = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-iconlocation") {
oLink.IconLocation = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-windowstyle") {
oLink.WindowStyle = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-workdir") {
oLink.WorkingDirectory = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-adminpermissions") {
if(args.Item(arg+1).toLowerCase() == "yes") {
var adminPermissions= true;
} else if(args.Item(arg+1).toLowerCase() == "no") {
var adminPermissions= false;
} else {
WScript.Echo("Illegal arguments (admin permissions)");
WScript.Quit(55);
}
}
}
oLink.Save();
if (!(typeof adminPermissions === 'undefined')) {
setAdminPermissions(linkfile ,adminPermissions);
}
#8
0
This worked for me on Windows XP ms-dos, I still haven't tried it on Windows 7. It's just like creating a symbolic link in Linux.
这在Windows XP ms-dos上对我有用,我还没有在Windows 7上尝试过它。就像在Linux中创建一个符号链接一样。
shortcut -T source.exe destination.lnk
#9
0
In the end I decided to write the correct script, because no solution works for me You will need two fileLocal Settings\ first
最后我决定编写正确的脚本,因为没有解决方案适合我。你需要两个fileLocal Settings \ first
createSCUT.bat
@echo on
set VBS=createSCUT.vbs
set SRC_LNK="shortcut1.lnk"
set ARG1_APPLCT="C:\Program Files\Google\Chrome\Application\chrome.exe"
set ARG2_APPARG="--profile-directory=QuteQProfile 25QuteQ"
set ARG3_WRKDRC="C:\Program Files\Google\Chrome\Application"
set ARG4_ICOLCT="%USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data\Profile 25\Google Profile.ico"
cscript %VBS% %SRC_LNK% %ARG1_APPLCT% %ARG2_APPARG% %ARG3_WRKDRC% %ARG4_ICOLCT%
and second
createSCUT.vbs
Set objWSHShell = WScript.CreateObject("WScript.Shell")
set objWSHShell = CreateObject("WScript.Shell")
set objFso = CreateObject("Scripting.FileSystemObject")
If WScript.arguments.count = 5 then
WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath arguments workingDir IconLocation"
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
set objSC = objWSHShell.CreateShortcut(sShortcut)
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sArguments = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(2))
sWorkingDirectory = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(3))
sIconLocation = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(4))
objSC.TargetPath = sTargetPath
rem http://www.bigresource.com/VB-simple-replace-function-5bAN30qRDU.html#
objSC.Arguments = Replace(sArguments, "QuteQ", Chr(34))
rem http://msdn.microsoft.com/en-us/library/f63200h0(v=vs.90).aspx http://msdn.microsoft.com/en-us/library/267k4fw5(v=vs.90).aspx
objSC.WorkingDirectory = sWorkingDirectory
objSC.Description = "Love Peace Bliss"
rem 1 restore 3 max 7 min
objSC.WindowStyle = "3"
rem objSC.Hotkey = "Ctrl+Alt+e";
objSC.IconLocation = sIconLocation
objSC.Save
WScript.Quit
end If
If WScript.arguments.count = 4 then
WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath arguments workingDir "
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
set objSC = objWSHShell.CreateShortcut(sShortcut)
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sArguments = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(2))
sWorkingDirectory = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(3))
objSC.TargetPath = sTargetPath
objSC.Arguments = Replace(sArguments, "QuteQ", Chr(34))
objSC.WorkingDirectory = sWorkingDirectory
objSC.Description = "Love Peace Bliss"
objSC.WindowStyle = "3"
objSC.Save
WScript.Quit
end If
If WScript.arguments.count = 2 then
WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath"
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
set objSC = objWSHShell.CreateShortcut(sShortcut)
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sWorkingDirectory = objFso.GetAbsolutePathName(sShortcut)
objSC.TargetPath = sTargetPath
objSC.WorkingDirectory = sWorkingDirectory
objSC.Save
WScript.Quit
end If
#1
23
Your link points to a Windows 95/98 version and I guess you have at least Windows 2000 or XP. You should try the NT version here.
你的链接指向Windows 95/98版本,我猜你至少有Windows 2000或XP。你应该在这里尝试NT版本。
Alternatively use a little VBScript that you can call from the command line:
或者使用一些可以从命令行调用的VBScript:
set objWSHShell = CreateObject("WScript.Shell")
set objFso = CreateObject("Scripting.FileSystemObject")
' command line arguments
' TODO: error checking
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sWorkingDirectory = objFso.GetAbsolutePathName(sShortcut)
set objSC = objWSHShell.CreateShortcut(sShortcut)
objSC.TargetPath = sTargetPath
objSC.WorkingDirectory = sWorkingDirectory
objSC.Save
Save the file as createLink.vbs and call it like this to get what you originally tried:
将文件另存为createLink.vbs并像这样调用以获得您最初尝试的内容:
cscript createLink.vbs "C:\Documents and Settings\%USERNAME%\Desktop\Program1 shortcut.lnk" "c:\program Files\App1\program1.exe"
cscript createLink.vbs "C:\Documents and Settings\%USERNAME%\Start Menu\Programs\Program1 shortcut.lnk" "c:\program Files\App1\program1.exe"
That said I urge you not to use hardcoded paths like "Start Menu" since they're different in localized versions of windows. Modify the script instead to use special folders.
这就是说我敦促你不要使用像“开始菜单”这样的硬编码路径,因为它们在Windows的本地化版本中是不同的。修改脚本而不是使用特殊文件夹。
#2
12
This is the kind of thing that PowerShell is really good at, and is therefore a reason to eschew batch files and get on PowerShell the bandwagon.
这是PowerShell真正擅长的事情,因此是避免批处理文件并使PowerShell成为潮流的理由。
PowerShell can talk to .NET. For example, you can get the location of the Desktop like this:
PowerShell可以与.NET通信。例如,您可以像这样获取桌面的位置:
[Environment]::GetFolderPath("Desktop")
PowerShell can talk to COM objects, including WScript.Shell
, which can create shortcuts:
PowerShell可以与COM对象通信,包括可以创建快捷方式的WScript.Shell:
New-Object -ComObject WScript.Shell).CreateShortcut( ... )
So your script might look like:
所以你的脚本可能看起来像:
$linkPath = Join-Path ([Environment]::GetFolderPath("Desktop")) "MyShortcut.lnk"
$targetPath = Join-Path ([Environment]::GetFolderPath("ProgramFiles")) "MyCompany\MyProgram.exe"
$link = (New-Object -ComObject WScript.Shell).CreateShortcut( $linkpath )
$link.TargetPath = $targetPath
$link.Save()
Shortcuts have a lot of settings that WScript.Shell can't manipulate, like the "run as administrator" option. These are only accessible through the Win32 interface IShellLinkDataList
, which is a real pain to use, but it can be done.
快捷方式有很多WScript.Shell无法操作的设置,例如“以管理员身份运行”选项。这些只能通过Win32接口IShellLinkDataList访问,这是一个真正的痛苦,但它可以做到。
#3
10
Using vbscript:
set WshShell = WScript.CreateObject("WScript.Shell" )
strDesktop = WshShell.SpecialFolders("AllUsersDesktop" )
set oShellLink = WshShell.CreateShortcut(strDesktop & "\shortcut name.lnk" )
oShellLink.TargetPath = "c:\application folder\application.exe"
oShellLink.WindowStyle = 1
oShellLink.IconLocation = "c:\application folder\application.ico"
oShellLink.Description = "Shortcut Script"
oShellLink.WorkingDirectory = "c:\application folder"
oShellLink.Save
Ref: http://www.tomshardware.com/forum/52871-45-creating-desktop-shortcuts-command-line
Failing that, a quick google search shows there's a number of third party tools that can create .lnk files for application shortcuts. I'm assuming you need to stick to stuff that's available natively on Windows though? VBscript is probably your best bet, otherwise I'd suggest trying copying the .lnk file from your machine or using it as a sample to see the correct format for a shortcut file.
如果做不到这一点,快速谷歌搜索会显示有许多第三方工具可以为应用程序快捷方式创建.lnk文件。我假设您需要坚持在Windows上原生可用的东西吗? VBscript可能是你最好的选择,否则我建议尝试从你的机器上复制.lnk文件或使用它作为样本来查看快捷方式文件的正确格式。
#4
3
On XP I wrote makeshortcut.vbs
在XP上我写了makehortcut.vbs
Set oWS = WScript.CreateObject("WScript.Shell")
If wscript.arguments.count < 4 then
WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath arguments workingDir "
WScript.Quit
end If
shortcutPath = wscript.arguments(0) & ".LNK"
targetPath = wscript.arguments(1)
arguments = wscript.arguments(2)
workingDir = wscript.arguments(3)
WScript.Echo "Creating shortcut " & shortcutPath & " targetPath=" & targetPath & " arguments=" & arguments & " workingDir=" & workingDir
Set oLink = oWS.CreateShortcut(shortcutPath)
oLink.TargetPath = targetPath
oLink.Arguments = arguments
' oLink.Description = "MyProgram"
' oLink.HotKey = "ALT+CTRL+F"
' oLink.IconLocation = "C:\Program Files\MyApp\MyProgram.EXE, 2"
' oLink.WindowStyle = "1"
oLink.WorkingDirectory = workingDir
oLink.Save
It takes exactly 4 args, so it could be improved by making the later 2 optional.I only post because it echo's usage, which might be useful to some. I like WS's soln using special folders and ExpandEnvironmentStrings
它只需要4个args,所以可以通过使后两个可选来改进它。我只发布因为它的回声用法,这可能对某些人有用。我喜欢WS的soln使用特殊文件夹和ExpandEnvironmentStrings
#5
1
Additional note: the link.bat you're using is for Windows 95/98 only:
附加说明:您使用的link.bat仅适用于Windows 95/98:
This batch file is for Windows 95/98 only. I will post the NT equivalent in the NT newsgroup soon.
此批处理文件仅适用于Windows 95/98。我很快就会在NT新闻组中发布NT等价物。
NT version is posted at http://www.robvanderwoude.com/amb_shortcutsnt.html instead. You might try that for a .bat approach if preferred over vbscript.
NT版本发布在http://www.robvanderwoude.com/amb_shortcutsnt.html上。如果优于vbscript,您可以尝试使用.bat方法。
#6
1
Alternative method, using a third party utility:
替代方法,使用第三方实用程序:
Creating a Shortcut from the command line (batch file)
从命令行创建快捷方式(批处理文件)
XXMKLINK:
With XXMKLINK, you can write a batch file for software installation which has been done by specialized installation programs. Basically, XXMKLINK is a tool that gathers various information from a command line and packages it into a shortcut.
使用XXMKLINK,您可以编写用于软件安装的批处理文件,该文件由专门的安装程序完成。基本上,XXMKLINK是一种从命令行收集各种信息并将其打包成快捷方式的工具。
xxmklink spath opath
where
spath path of the shortcut (.lnk added as needed)
opath path of the object represented by the shortcut
#7
1
EDIT 24.6.14 - Following functionalities added: -Edit shortcut -List properties of a shortcut -Set/Remove "Run as administrator" thick
编辑24.6.14 - 添加了以下功能: - 编辑快捷方式 - 快捷方式的列表属性 - 设置/删除“以管理员身份运行”厚
Here a maintained version of the script can be found
这里可以找到脚本的维护版本
When using Windows Script Host I prefer jscript as it allows creating hybrid files with neither toxic messages nor temporary files.Here's my shortcutJS.bat (you can named it as you like ) allowing you to use all shortcut properties:
使用Windows脚本主机时,我更喜欢使用jscript,因为它允许创建既没有有毒消息也没有临时文件的混合文件。这是我的shortcutJS.bat(您可以根据需要命名),允许您使用所有快捷方式属性:
@if (@X)==(@Y) @end /* JScript comment
@echo off
cscript //E:JScript //nologo "%~f0" "%~nx0" %*
exit /b %errorlevel%
@if (@X)==(@Y) @end JScript comment */
var args=WScript.Arguments;
var scriptName=args.Item(0);
//var adminPermissions= false;
var edit= false;
function printHelp() {
WScript.Echo(scriptName + " -linkfile link -target target [-linkarguments linkarguments] "+
" [-description description] [-iconlocation iconlocation] [-hotkey hotkey] "+
" [-windowstyle 1|3|7] [-workingdirectory workingdirectory] [-adminpermissions yes|no]");
WScript.Echo();
WScript.Echo(scriptName + " -edit link [-target target] [-linkarguments linkarguments] "+
" [-description description] [-iconlocation iconlocation] [-hotkey hotkey] "+
" [-windowstyle 1|3|7] [-workingdirectory workingdirectory] [-adminpermissions yes|no]");
WScript.Echo();
WScript.Echo(scriptName + " -examine link");
WScript.Echo();
WScript.Echo(" More info: http://msdn.microsoft.com/en-us/library/xk6kst2k%28v=vs.84%29.aspx ");
}
// reads the given .lnk file as a char array
function getlnkChars(lnkPath) {
// :: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855&start=15&p=28898 ::
var ado = WScript.CreateObject("ADODB.Stream");
ado.Type = 2; // adTypeText = 2
ado.CharSet = "iso-8859-1"; // code page with minimum adjustments for input
ado.Open();
ado.LoadFromFile(lnkPath);
var adjustment = "\u20AC\u0081\u201A\u0192\u201E\u2026\u2020\u2021" +
"\u02C6\u2030\u0160\u2039\u0152\u008D\u017D\u008F" +
"\u0090\u2018\u2019\u201C\u201D\u2022\u2013\u2014" +
"\u02DC\u2122\u0161\u203A\u0153\u009D\u017E\u0178" ;
var fs = new ActiveXObject("Scripting.FileSystemObject");
var size = (fs.getFile(lnkPath)).size;
var lnkBytes = ado.ReadText(size);
ado.Close();
var lnkChars=lnkBytes.split('');
for (var indx=0;indx<size;indx++) {
if ( lnkChars[indx].charCodeAt(0) > 255 ) {
lnkChars[indx] = String.fromCharCode(128 + adjustment.indexOf(lnkChars[indx]));
}
}
return lnkChars;
}
function hasAdminPermissions(lnkPath) {
return (getlnkChars(lnkPath))[21].charCodeAt(0) == 32 ;
}
function setAdminPermissions(lnkPath , flag) {
lnkChars=getlnkChars(lnkPath);
var ado = WScript.CreateObject("ADODB.Stream");
ado.Type = 2; // adTypeText = 2
ado.CharSet = "iso-8859-1"; // right code page for output (no adjustments)
//ado.Mode=2;
ado.Open();
// setting the 22th byte to 32
if (flag) {
lnkChars[21]=String.fromCharCode(32);
} else {
lnkChars[21]=String.fromCharCode(0);
}
ado.WriteText(lnkChars.join(""));
ado.SaveToFile(lnkPath, 2);
ado.Close();
}
function examine(lnkPath) {
var fs = new ActiveXObject("Scripting.FileSystemObject");
if (!fs.FileExists(lnkPath)) {
WScript.Echo("File " + lnkPath + " does not exist");
WScript.Quit(2);
}
var oWS = new ActiveXObject("WScript.Shell");
var oLink = oWS.CreateShortcut(lnkPath);
WScript.Echo("");
WScript.Echo(lnkPath + " properties:");
WScript.Echo("");
WScript.Echo("Target: " + oLink.TargetPath);
WScript.Echo("Icon Location: " + oLink.IconLocation);
WScript.Echo("Description: " + oLink.Description);
WScript.Echo("Hotkey: " + oLink.HotKey );
WScript.Echo("Working Directory: " + oLink.WorkingDirectory);
WScript.Echo("Window style: " + oLink.WindowStyle);
WScript.Echo("Admin Permissions: " + hasAdminPermissions(lnkPath));
WScript.Quit(0);
}
if (WScript.Arguments.Length==1 || args.Item(1).toLowerCase() == "-help" || args.Item(1).toLowerCase() == "-h" ) {
printHelp();
WScript.Quit(0);
}
if (WScript.Arguments.Length % 2 == 0 ) {
WScript.Echo("Illegal arguments ");
printHelp();
WScript.Quit(1);
}
if ( args.Item(1).toLowerCase() == "-examine" ) {
var linkfile = args.Item(2);
examine(linkfile);
}
if ( args.Item(1).toLowerCase() == "-edit" ) {
var linkfile = args.Item(2);
edit=true;
}
if(!edit) {
for (var arg = 1;arg<5;arg=arg+2) {
if ( args.Item(arg).toLowerCase() == "-linkfile" ) {
var linkfile = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-target") {
var target = args.Item(arg+1);
}
}
}
if (typeof linkfile === 'undefined') {
WScript.Echo("Link file not defined");
printHelp();
WScript.Quit(2);
}
if (typeof target === 'undefined' && !edit) {
WScript.Echo("Target not defined");
printHelp();
WScript.Quit(3);
}
var oWS = new ActiveXObject("WScript.Shell");
var oLink = oWS.CreateShortcut(linkfile);
if(typeof target === 'undefined') {
var startIndex=3;
} else {
var startIndex=5;
oLink.TargetPath = target;
}
for (var arg = startIndex ; arg<args.Length;arg=arg+2) {
if (args.Item(arg).toLowerCase() == "-linkarguments") {
oLink.Arguments = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-description") {
oLink.Description = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-hotkey") {
oLink.HotKey = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-iconlocation") {
oLink.IconLocation = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-windowstyle") {
oLink.WindowStyle = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-workdir") {
oLink.WorkingDirectory = args.Item(arg+1);
}
if (args.Item(arg).toLowerCase() == "-adminpermissions") {
if(args.Item(arg+1).toLowerCase() == "yes") {
var adminPermissions= true;
} else if(args.Item(arg+1).toLowerCase() == "no") {
var adminPermissions= false;
} else {
WScript.Echo("Illegal arguments (admin permissions)");
WScript.Quit(55);
}
}
}
oLink.Save();
if (!(typeof adminPermissions === 'undefined')) {
setAdminPermissions(linkfile ,adminPermissions);
}
#8
0
This worked for me on Windows XP ms-dos, I still haven't tried it on Windows 7. It's just like creating a symbolic link in Linux.
这在Windows XP ms-dos上对我有用,我还没有在Windows 7上尝试过它。就像在Linux中创建一个符号链接一样。
shortcut -T source.exe destination.lnk
#9
0
In the end I decided to write the correct script, because no solution works for me You will need two fileLocal Settings\ first
最后我决定编写正确的脚本,因为没有解决方案适合我。你需要两个fileLocal Settings \ first
createSCUT.bat
@echo on
set VBS=createSCUT.vbs
set SRC_LNK="shortcut1.lnk"
set ARG1_APPLCT="C:\Program Files\Google\Chrome\Application\chrome.exe"
set ARG2_APPARG="--profile-directory=QuteQProfile 25QuteQ"
set ARG3_WRKDRC="C:\Program Files\Google\Chrome\Application"
set ARG4_ICOLCT="%USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data\Profile 25\Google Profile.ico"
cscript %VBS% %SRC_LNK% %ARG1_APPLCT% %ARG2_APPARG% %ARG3_WRKDRC% %ARG4_ICOLCT%
and second
createSCUT.vbs
Set objWSHShell = WScript.CreateObject("WScript.Shell")
set objWSHShell = CreateObject("WScript.Shell")
set objFso = CreateObject("Scripting.FileSystemObject")
If WScript.arguments.count = 5 then
WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath arguments workingDir IconLocation"
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
set objSC = objWSHShell.CreateShortcut(sShortcut)
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sArguments = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(2))
sWorkingDirectory = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(3))
sIconLocation = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(4))
objSC.TargetPath = sTargetPath
rem http://www.bigresource.com/VB-simple-replace-function-5bAN30qRDU.html#
objSC.Arguments = Replace(sArguments, "QuteQ", Chr(34))
rem http://msdn.microsoft.com/en-us/library/f63200h0(v=vs.90).aspx http://msdn.microsoft.com/en-us/library/267k4fw5(v=vs.90).aspx
objSC.WorkingDirectory = sWorkingDirectory
objSC.Description = "Love Peace Bliss"
rem 1 restore 3 max 7 min
objSC.WindowStyle = "3"
rem objSC.Hotkey = "Ctrl+Alt+e";
objSC.IconLocation = sIconLocation
objSC.Save
WScript.Quit
end If
If WScript.arguments.count = 4 then
WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath arguments workingDir "
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
set objSC = objWSHShell.CreateShortcut(sShortcut)
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sArguments = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(2))
sWorkingDirectory = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(3))
objSC.TargetPath = sTargetPath
objSC.Arguments = Replace(sArguments, "QuteQ", Chr(34))
objSC.WorkingDirectory = sWorkingDirectory
objSC.Description = "Love Peace Bliss"
objSC.WindowStyle = "3"
objSC.Save
WScript.Quit
end If
If WScript.arguments.count = 2 then
WScript.Echo "usage: makeshortcut.vbs shortcutPath targetPath"
sShortcut = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
set objSC = objWSHShell.CreateShortcut(sShortcut)
sTargetPath = objWSHShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sWorkingDirectory = objFso.GetAbsolutePathName(sShortcut)
objSC.TargetPath = sTargetPath
objSC.WorkingDirectory = sWorkingDirectory
objSC.Save
WScript.Quit
end If