user1113827
user1113827

Reputation: 69

Creating Shortcut with batch and vbs

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.

Upvotes: 3

Views: 2150

Answers (1)

Mike Nakis
Mike Nakis

Reputation: 61949

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:

SET SearchBaseDir="C:\Documents and Settings\"

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.

Upvotes: 1

Related Questions