从批处理文件创建exe的快捷方式

时间:2008-12-06 08:59:56

标签: batch-file

如何从批处理文件创建exe的快捷方式。

我试过

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"

但它没有用。

link.bat可以在 http://www.robvanderwoude.com/amb_shortcuts.html

9 个答案:

答案 0 :(得分:23)

您的链接指向Windows 95/98版本,我猜您至少拥有Windows 2000或XP。您应该尝试NT版本here

或者使用一些可以从命令行调用的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

将文件另存为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" 

那就是说我建议你不要使用像“开始菜单”这样的硬编码路径,因为它们在Windows的本地化版本中是不同的。修改脚本而不是使用special folders

答案 1 :(得分:12)

这是 PowerShell 非常擅长的事情,因此是避免批处理文件并使PowerShell成为潮流的理由。

PowerShell可以与.NET通信。例如,您可以像这样获取桌面的位置:

[Environment]::GetFolderPath("Desktop")

PowerShell可以与COM 对象进行通信,包括可以创建快捷方式的WScript.Shell

New-Object -ComObject WScript.Shell).CreateShortcut( ... )

所以你的脚本可能如下:

$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()

快捷方式有许多WScript.Shell无法操作的设置,例如“以管理员身份运行”选项。这些只能通过Win32接口IShellLinkDataList访问,这真的很难用,但可以做到。

答案 2 :(得分:10)

使用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 

参考:http://www.tomshardware.com/forum/52871-45-creating-desktop-shortcuts-command-line

如果做不到这一点,快速谷歌搜索会显示有许多第三方工具可以为应用程序快捷方式创建.lnk文件。我假设您需要坚持在Windows上原生可用的东西吗? VBscript可能是你最好的选择,否则我建议你尝试从你的机器上复制.lnk文件或使用它作为样本来查看快捷方式文件的正确格式。

答案 3 :(得分:3)

在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

它只需要4个args,所以可以通过使后两个可选来改进它。我只发布因为它的回声用法,这可能对某些人有用。我喜欢WS的soln使用特殊文件夹和ExpandEnvironmentStrings

答案 4 :(得分:1)

替代方法,使用第三方实用程序:

Creating a Shortcut from the command line (batch file)

  

XXMKLINK:

     

使用XXMKLINK,您可以为软件编写批处理文件      通过专门安装完成的安装      程式。基本上,XXMKLINK是一个收集各种各样的工具      来自命令行的信息并将其打包成快捷方式。

xxmklink spath opath 

where 

  spath     path of the shortcut (.lnk added as needed)
  opath     path of the object represented by the shortcut

答案 5 :(得分:1)

附加说明:您使用的link.bat仅适用于Windows 95/98:

  

此批处理文件仅适用于Windows 95/98。我将发布NT等价物   很快就会成为新闻组的新闻组。

NT版本发布在http://www.robvanderwoude.com/amb_shortcutsnt.html。如果优先于vbscript,你可以尝试使用.bat方法。

答案 6 :(得分:1)

编辑24.6.14 - 增加了以下功能: - 编辑快捷方式 - 快捷方式的列表属性 - 设置/删除&#34;以管理员身份运行&#34;厚

Here可以找到维护版本的脚本

使用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);
   }

答案 7 :(得分:0)

这对我来说在Windows XP上运行了ms-dos,我还没有在Windows 7上尝试过它。就像在Linux中创建一个符号链接一样。

shortcut -T source.exe destination.lnk

答案 8 :(得分:0)

最后我决定编写正确的脚本,因为没有解决方案适合我 你需要两个文件本地设置\ 第一次

  

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%

和第二

  

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