-1

I want to have a executable VBS that run all windows shortcut in the current folder of my VBS. My shortcuts are both Excel and PDF. I've made this code, but i have an error on "WshShell.Run objFile.Path" Other line look working correctly

Dim FSO
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
GetCurrentFolder = FSO.GetAbsolutePathName(".")
Set objFolder = FSO.GetFolder(GetCurrentFolder)
For Each objFile in objFolder.Files
   If LCase(Right(objFile.Path, 4)) = ".lnk" Then
      WshShell.Run objFile.Path
   End If
Next

I want the mother path adapt to VBS, so i can copy past VBS and put it in other folders Thanks

user692942
  • 16,398
  • 7
  • 76
  • 175
Legomanfr
  • 9
  • 3

1 Answers1

0

The error is due to spaces in one or more of your file paths. Correct this by placing quotes around the path: WshShell.Run """" & objFile.Path & """"

The posted script is running all shortcuts in the current directory, which is not necessarily the script's directory (e.g. if the script is launched via the command line from another folder). To fix that, use: FSO.GetParentFolderName(WScript.ScriptFullName).

Here is the complete, corrected, script:

Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
GetCurrentFolder = FSO.GetParentFolderName(WScript.ScriptFullName)
Set objFolder = FSO.GetFolder(GetCurrentFolder)
For Each objFile in objFolder.Files
   If LCase(Right(objFile.Path, 4)) = ".lnk" Then
      WshShell.Run """" & objFile.Path & """"
      WScript.Sleep 1000 'adjust as needed 
   End If
Next
LesFerch
  • 1,540
  • 2
  • 5
  • 21
  • Just like [this answer](https://stackoverflow.com/a/36599265/692942) from 6 years ago. – user692942 Dec 15 '22 at 16:47
  • @user692942 I had actually flagged it with that duplicate, but then I noticed the OP's other issue is the current directory versus the script's directory. And yes, there's a duplicate for that too, but then which duplicate to pick? – LesFerch Dec 15 '22 at 17:11
  • I know what you mean but questions that have multiple issues aren’t focused on one problem which again is a flaggable reason in itself. The main issue I would say though is the lack of double quotes which we see three or four times a week. – user692942 Dec 15 '22 at 19:58
  • Hello, thanks for the code. I tested with heavy Excel and it open only 1 shortcut. I fix the problem by adding "WScript.Sleep 4000" so it let the time to open my file between each loops – Legomanfr Dec 19 '22 at 07:30