Run all shortcut in the mother path of VBScript [duplicate] - vbscript

This question already has answers here:
How to run several shortcuts (.lnk files) with a script in Windows?
(3 answers)
Closed 2 months ago.
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

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

Related

Delete file within APPDATA folder

I have many PCs that currently have a Personal macro workbook installed. More specifically, they all have a shortcut to a Personal macro workbook on a network drive.
To install that, I went to each PC and ran this VBScript:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData=objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
oFSO.CopyFile "H:\Folder\Folder\Folder\PERSONAL 1.xlam - Shortcut.lnk", strAppData, True
Set objShell = Nothing
Set oFSO = Nothing
Now though, I want to remove that shortcut to PERSONAL 1.xlam from the XLSTART folder and copy over a shortcut to a different macro workbook.
This might be really easy, but I'm new to VBS and I haven't found a way to delete a file without having the the exact path. And since the path is going to be unique to each PC, I can't do that here.
You just need to modify one line from the above script. Try the following:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
'here is the modified line
oFSO.DeleteFile strAppData & "PERSONAL 1.xlam - Shortcut.lnk", True
Set objShell = Nothing
Set oFSO = Nothing

VBS - Getting program files folder path?

I am tring to get program files folder in vbs. Tried this without luck;
SET wsc = CreateObject("WScript.Shell")
SET fso = WScript.CreateObject("Scripting.FileSystemObject")
targetpath = wsc.SpecialFolders("ProgramFiles") & "\Google\Chrome\Application\chrome.exe"
It just get the C:\ dir. What is the correct way to do it ?
This TechNet article shows the list of SpecialFolders. Program Files is not among them. This is a limitation of the Windows Script Host. In the same way that the following shows a blank popup
SET wsc = CreateObject("WScript.Shell")
msgbox wsc.SpecialFolders("Awesome")
So instead you have at least 2 options.
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H26&)
Set objFolderItem = objFolder.Self
msgbox objFolderItem.Path
&H26& - Program Files
&H2A& - Program Files (x86)
The other option that I would offer is to use Environment variables like JosefZ suggests.
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles%") & "..."
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "..."

Copying a file using VBS in to a Windows Directory, getting permission denied

I'm trying to simplify an install for a group of people I work with that have very little computer skills. I have a vbs script that copies various configuration files in to their proper directories. However, there is one file that I cannot get to copy.
I am trying to copy a new file called hosts in to the C:\windows\systems32\drivers\etc folder and I keep getting permission denied no matter what I do.
Const OverWriteExisting = True
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\users\IBM_ADMIN\Desktop\Colgate Socks\hosts", "C:\Windows\System32\drivers\etc\hosts", OverWriteExisting
Any ideas?
Run as administrator?
You can do this through task scheduler easily.
Another option was from How to run scripts as administrator in Windows 7?
Put this at the start of your script
Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
Else
[your code here]
end if
Use this %windir%\Sysnative instead of C:\Windows\System32.

Run command line for each file in folder - quotation mark issue (I think)

I'm trying to run a run the following command line for each specific file type (as example for each .txt file) in the current directory:
"C:\Program Files (x86)\some program\someprogram.exe" "file.txt" "file.txt.mod" -someparameter
When I run this exact command from an open Windows command prompt (including all the quotation marks), it works.
But when I run it through this VB, it does nothing/closes right away.
What am I doing wrong? I have a feeling it has to do with the quotes, but my head can't sort it out.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
strFileName = objFile.Name
If objFSO.GetExtensionName(strFileName) = "txt" Then
RunCommand()
End If
Next
Sub RunCommand
Set oShell = WScript.CreateObject ("WScript.Shell")
oShell.run "cmd.exe /C ""C:\Program Files (x86)\some program\someprogram.exe"" """ & objFile.Path & """ """ & objFile.Path & ".mod"" -someparameter"
Set oShell = Nothing
End Sub
You should
Reduce risk of failures
by
using "Option Explicit"
avoiding clever "roll your own" hacks by using standard methods (.GetParentFolderName) instead
using type prefixes correctly (objStartFolder)
avoiding variables just used once (objFolder, colFiles)
not using globals to pass parameters into Subs/Functions (objFile)
avoiding (unnecessary) stress (.Run without wait, new WScript.Shell for each file, "cmd" instead of "%comspec%")
using cscript in a 'dos box' instead of double click/wscript
and
check your assumptions
by
diagnostic output (.Echo objFile.Name immediately before calling RunCommand, use a variable to store and .Echo the command send to .Run)
Check return values of functions that provide diagnostics (.Run)
sanity checks like:
(just to tame the formatter)
>> WScript.Echo goFS.GetExtensionName("A.TXT")
>>
TXT

windows shell - how to detect the current location where script file is placed?

I am trying the windows shell file which will be inserted in the folder where it will analyze folders content.
Now i would like to know how can i detect which is the current path ? i.e. location where the vbs file is placed using FileSystemObject?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo objFS.GetParentFolderName(WScript.ScriptFullName)
You can get that from WScript.ScriptFullName. Just remove the filename from the end (the bit after the last backslash). I normally use JScript for scripts, but IIRC VBScript has an InStrRev function that will help you find the last backslash. Or: Create a File object for the WScript.ScriptFullName path and then use its ParentFolder property. Something like (untested):
Dim objFSO
Dim objFile
Dim objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(WScript.ScriptFullName)
Set objFolder = objFile.ParentFolder
To get the full path only without the extension I use Replace(WScript.ScriptFullName, WScript.ScriptName, "") to just result in a filepath

Resources