简体   繁体   中英

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM