1
votes

I am trying to create a vbscript macro which would get the folder location in which the macro is stored and create the output files into the same folder. I am using the below code but its not getting the correct location

Set obj1FSO = CreateObject("Scripting.FileSystemObject")
folderLoc = obj1FSO.GetParentFolderName("\Cubes_Macro_V5.zmc") 

It would then use the folderLoc variable and append the output file name to it in order to create the output file in the same folder as where the macro is stored.

Set repFso = CreateObject("Scripting.FileSystemObject")
Set repFile = repFso.CreateTextFile(folderLoc & "RCHT_OPTION4_REPORT.txt", True)

The Macro is stored in a folder in the D:\ drive but when I run the above segment of code it get the location for the desktop.

2

2 Answers

0
votes

https://msdn.microsoft.com/en-us/library/22dyy47c%28v=vs.84%29.aspx

That is not how the method works - it extracts the "parent" from the string provided.

   Option Explicit
   Dim fso,GetTheParent

   Set fso = CreateObject("Scripting.FileSystemObject")
   GetTheParent = fso.GetParentFolderName("C:\Windows")
   wscript.echo "Attempt 1:" & "'" & GetTheParent & "'"

   GetTheParent = fso.GetParentFolderName("\Windows")
   wscript.echo "Attempt 2:" & "'" & GetTheParent & "'"

   GetTheParent = fso.GetParentFolderName("Windows")
   wscript.echo "Attempt 3:" & "'" & GetTheParent & "'"

   GetTheParent = fso.GetParentFolderName("C:\WINDOWS\system32")
   wscript.echo "Attempt 4:" & "'" & GetTheParent & "'"

   GetTheParent = fso.GetParentFolderName("\WINDOWS\system32")
   wscript.echo "Attempt 5:" & "'" & GetTheParent & "'"

Results:

Attempt 1:'C:\'
Attempt 2:''
Attempt 3:''
Attempt 4:'C:\WINDOWS'
Attempt 5:'\WINDOWS'
0
votes

There is a difference between the current directory ('where you are when you start the process') and the script's directory:

>> WScript.Echo 0, goFS.GetAbsolutePathName(".\")
>> WScript.Echo 1, goWS.CurrentDirectory
>> WScript.Echo 2, goFS.GetParentFolderName(WScript.ScriptFullName)
>>
0 C:\Documents and Settings\eh
1 C:\Documents and Settings\eh
2 M:\bin

(I called my REPL/Interactive VBS shell that resides in m:\bin from my home directory)

I don't know if "Cubes_Macro_V5.zmc" is a VBScript file (with a special extension that is loaded by whatever agent you use via w|cscript.exe), but check if WScript.ScriptFullName has the expected content and apply .GetParentFolderName.

If that fails, you need to determine the folder that your agent uses for macros and feed that directory to .BuildPath.

On second thought:

Voodoo but easy to test: Does

WScript.Echo 0, goFS.GetAbsolutePathName(".\Cubes_Macro_V5.zmc")

deliver the desired result?