使用批处理和 vbs 创建快捷方式
我正在尝试制作一个简单的(或至少我认为的)批处理文件来查找特定文件类型的文件,然后创建快捷方式以使用特定程序打开它们。
经过大量挖掘后,我发现最好使用 VBScript 来完成此操作,因此我创建了一个小脚本来为我创建快捷方式。这不起作用,所以我希望有人能告诉我原因。谢谢。
MakeShortcuts.bat:
SET ShortcutPath=c:\shortcuts\
SET ProgramPath="c:\windows\notepad.exe"
SET SearchBaseDir="C:\Documents and Settings\"
FOR /R %SearchBaseDir% %%i IN (*.gba) DO (
createShortcut "%ShortcutPath%Open %%~ni.lnk" "%ProgramPath%" "%%i"
)
createShortcut.vbs:
set obShell = CreateObject("WScript.Shell")
sShortcut = obShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
sTargetPath = obShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sArgument = obShell.ExpandEnvironmentStrings(WScript.Arguments.Item(2))
set shortcut = obShell.CreateShortcut(sShortcut)
shortcut.TargetPath = sTargetPath
shortcut.Arguments = sArgument
shortcut.Save
好的,所以当我运行 MakeShortcuts.bat 时,它会在命令行中显示消息 [系统找不到指定的驱动器。]。
它按预期创建快捷方式,只是目标参数周围没有引号,这会阻止快捷方式正常运行。我认为这也可能与上述错误消息有关。
我对 VBScript 很陌生,所以我可能错过了一些非常愚蠢的东西。请帮忙。
谢谢。
I am trying to make a simple (or at least I thought) batch file to find files of a specific file type and then create shortcuts to open them using a specific program.
After a lot of digging I found this is best done with VBScript so I created a little script to make shortcuts for me. This isn't working so I was hoping someone could tell me why. Thank you.
MakeShortcuts.bat:
SET ShortcutPath=c:\shortcuts\
SET ProgramPath="c:\windows\notepad.exe"
SET SearchBaseDir="C:\Documents and Settings\"
FOR /R %SearchBaseDir% %%i IN (*.gba) DO (
createShortcut "%ShortcutPath%Open %%~ni.lnk" "%ProgramPath%" "%%i"
)
createShortcut.vbs:
set obShell = CreateObject("WScript.Shell")
sShortcut = obShell.ExpandEnvironmentStrings(WScript.Arguments.Item(0))
sTargetPath = obShell.ExpandEnvironmentStrings(WScript.Arguments.Item(1))
sArgument = obShell.ExpandEnvironmentStrings(WScript.Arguments.Item(2))
set shortcut = obShell.CreateShortcut(sShortcut)
shortcut.TargetPath = sTargetPath
shortcut.Arguments = sArgument
shortcut.Save
Ok, so when I ran MakeShortcuts.bat it give me the message [The system cannot find the drive specified.] in the command line.
It creates the shortcuts as expected except that the target has no quotes around the argument which is preventing the shortcuts from functioning properly. I think this also may be related to the above error message as well.
I am very new to VBScript so I am probably missing something pretty stupid. Please help.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑问题出在VBScript中,它可能出在批处理文件中。您分配给 SearchBaseDir 的值应该用双引号括起来,如下所示:
然后,您需要从传递给 createShortcut 的参数中删除引号。
您还需要决定是使用 %%i 还是 %%f,因为使用两者是不一致的。将 %%i 重命名为 %%f,或将 %%f 重命名为 %%i。
最后,我对此不太确定,但是最后一个 %%f 可能必须替换为 %%~f 以便摆脱双引号,如果它的值有可能包含双引号的话。
I doubt that the problem is in the VBScript, it is probably in the batch file. The value that you assign to SearchBaseDir should be enclosed within double quotes, like this:
Then, you need to remove the quotes from the parameters that you pass to createShortcut.
You also need to decide whether you are going to be using %%i or %%f, because using both is inconsistent. Either rename %%i to %%f, or %%f to %%i.
Finally, I am not absolutely sure about this, but the last %%f might have to be replaced with %%~f so as to get rid of the double quotes, if by any chance its value contains double quotes.