【问题标题】:How to run several shortcuts (.lnk files) with a script in Windows?如何在 Windows 中使用脚本运行多个快捷方式(.lnk 文件)?
【发布时间】:2016-08-04 03:21:04
【问题描述】:

我正在尝试创建一个脚本来运行我在特定项目中使用的所有工具(dor 实例、gulp、phpstorm、php 服务器等)

我想运行快捷方式而不是可执行文件。我可以在 Windows 中看到两种可能的实现方式:

  1. VbScript
  2. cmd.exe

很遗憾,我都失败了:

1) WshShell 触发 .lnk 文件,但我找不到替代方法:

为此,我正在尝试编写 .cmd 文件或 .vbs 但是,我在各个方面都遇到了问题。

Dim WshShell

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe" ' Works alright
WshShell.Run "C:\Users\eugene\Desktop\gulp EngMe.lnk" ' got an Exception: Unknown Exception

2) .cmd 脚本等待关闭之前启动的应用程序:

@rem urls and filesystem shortcuts start alright:
"C:\Users\eugene\Desktop\8000.url"
"C:\Users\eugene\Desktop\project_folder.lnk"

@rem the following two commands are starting sequentially, when the previous one has closed.
"notepad.exe"
@rem shortcut to %windir%\system32\notepad.exe:
"C:\Users\eugene\Desktop\Notepad_.lnk"

我做错了什么,还有其他方法可以做我正在尝试的事情吗?

【问题讨论】:

    标签: batch-file vbscript cmd wsh


    【解决方案1】:

    当输入到.Run 时,带有空格的路径必须是"quoted" 所以:

    WshShell.Run """C:\Users\eugene\Desktop\gulp EngMe.lnk"""
    

    会正常工作。

    【讨论】:

      【解决方案2】:

      尝试使用 powershell 编写脚本。

      看问题-Execute shortcuts like programs

      查看 Mark Schill 的回答,使用 invoke-item 启动 .lnk 文件,如程序。

      这可能是您应对脚本挑战的最佳方式。

      【讨论】:

      • 非常感谢!我还不知道powershell。我试过了,invoke-item 工作正常。
      【解决方案3】:

      您可以查看shortcutjs.bat,它能够读取 .lnk 文件属性。有了这个,您将在正确的工作目录中执行当前目录中的所有.lnk 文件:

      @echo off
      
      for %%# in (*.lnk) do (
          call :executeShortcut "%%#"
      )
      
      exit /b %errorlevel%
      :executeShortcut - %1 - shortcut to execute
      setlocal enableDelayedExpansion
      for /f "tokens=1,* delims=:" %%a in ('call shortcutjs.bat -examine %1') do (
          if /i "%%a" equ "Target" (
              set "target=%%b"
              set "target=!target:~1!"
          )
      
          if /i "%%a" equ "Working Directory" (
              set "workdir=%%b"       
              set "workdir=!workdir:~1!"
          )
      )
      
      start "%target%" /d "%workdir%" /wait /b "%target%"
      exit /b %errorlevel%
      

      【讨论】:

        猜你喜欢
        • 2012-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-04
        • 2010-09-25
        • 1970-01-01
        • 2014-03-01
        • 1970-01-01
        相关资源
        最近更新 更多