How do I set Dynamic File Paths in VBScript Specifically for FilesystemObject - vbscript

I have spent several hours trying to get this to work. For some reason, my file paths register just fine in command prompt, but if I try to reference the file path using a FileSystemObject it doesn't work and tells me the file can't be found.
Dim g_shell
Set g_shell = CreateObject("WScript.Shell")
strCurDir = g_shell.CurrentDirectory
strValue = someName 'This is actually passed from a function argument
Set objFSO = CreateObject("Scripting.FileSystemObject")
configPath = strCurDir & "\maintLogs\" & strValue & "\MaintGuy_" & strValue & ".config"
Set objStream = objFSO.OpenTextFile(configPath) ' This throws a file not found error
g_shell.Run "cmd /c" & configPath & "& pause" 'This opens the file with no problem
'Let's Try something else:
Set objFSO = CreateObject("Scripting.FileSystemObject")
aPath = objFSO.BuildPath(g_shell.CurrentDirectory, "maintLogs")
bPath = objFSO.BuildPath(aPath, strValue)
cPath = objFSO.BuildPath(bPath, "MaintGuy_" & strValue & ".config")
Set objStream = objFSO.OpenTextFile(cPath) ' still doesn't work
g_shell.Run "cmd /c " & cPath & "& pause" ' This works (opens the file)
So my question is, why does this give me a file not found, when it works in command prompt and is an existing file. What am I missing?
In the example I am trying to read a text file, but I also could not get these file paths to work when I was trying to create a folder, move files into folders, and other file manipulation techniques. I accomplished it through launching the command prompt but could not get them to work using Scripting.FileSystemObject service.
Operating System: Windows 10
Scripting Language: VisualBasic Script
Application: SecureCRT

I figured it out. It turns out, because I was moving a file into the location I was trying to read from, it was trying to read the file BEFORE the file was there. I added a sleep function to the code, and it worked!

Related

Run batch file with parameters

I would like to run a batch file with some extra character at the end.
If I write it directly to cmd it is working, like change directory to "c:\Users\Public\Uploader\" and write the following: start.bat "cmd:file.import c:\Users\tom\Desktop\a.xml"
I do not know how to write it in VBScript, because the following script is not working:
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory="C:\Users\Public\Uploader\"
WshShell.Run "start.bat" & "cmd:file.import C:\Users\tom\Desktop\a.xml"
The a.xml should dynamically change based on the last modified file in the folder.
Thank you for your help!
If you're trying to duplicate what you're doing in the command prompt, you need to add a space between the batch file name and its arguments. You also need to add the quotes.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory="C:\Users\Public\Uploader\"
WshShell.Run "start.bat " & chr(34) & "cmd:file.import C:\Users\tom\Desktop\a.xml" & chr(34)

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)%") & "..."

How to Create Folders and Subfolders in %AppData%

First of all thank you very much for all the help I’ve found over 3 Vbscript that has save my life during this last six months.
I’m new to Vbscripting and I’m currently working on getting a Vbscript that create folders and copy a file at the same time overwrite that folder and file if they already exist
Folder and subfolders to be created (Avaya) C:\Users\My Username\AppData\Roaming\Avaya\ Avaya\one-X Agent\2.5\
File from (Myfile.txt) C:\Myfile.txt to C:\Users\My Username\AppData\Roaming\Avaya\one-X Agent\2.5\
I get “Path not found” error, but If I leave the path till (Avaya) it creates Avaya Folder but not it’s subfolders C:\Users\My Username\AppData\Roaming\Avaya\
Here’s what I have and thank you in advance
Dim fso, vFolder
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")
usrName = objWShell.expandEnvironmentStrings("%USERNAME%")
Set fso = CreateObject("Scripting.FileSystemObject")
Set vFolder = fso.CreateFolder("C:\Users\" & usrName & "\AppData\Roaming\Avaya\one-X Agent\2.5\")
CreateFolderDemo = vFolder.Path
The problem is that CreateFolder does not create intermediate folders. The FSO doesn't have a method that does that. It might be easier to use mkdir like this:
Option Explicit
Dim shl
Set shl = CreateObject("WScript.Shell")
Call shl.Run("%COMSPEC% /c mkdir ""%APPDATA%\Avaya\one-X Agent\2.5""",0,true)
Some errors:
You declare fso, but you use objFso
You use %USERNAME% but you should consider %APPDATA% instead
You should use OPTION EXPLICIT to detect typos and undefined variables
You should make your code easier to read by dimming one variable at a time
CreateFolder doesn't create the entire tree, so you need to use FolderExists
For example:
Option Explicit
Dim objWShell
Set objWShell = WScript.CreateObject("WScript.Shell")
Dim appData
appData = objWShell.expandEnvironmentStrings("%APPDATA%")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(appData + "\Avaya") Then
objFso.CreateFolder appData + "\Avaya"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent\2.5") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent\2.5"
End If
Lastly, it's not clear why your solution needs to be in VBScript. It appears that your requirements are creating folders and copying files, which means, batch files would probably be a far more simpler.

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

Resources