VBScript Macro getParentFolder Name

前端 未结 2 346
清歌不尽
清歌不尽 2021-01-24 17:06

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 co

相关标签:
2条回答
  • 2021-01-24 17:26

    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?

    0 讨论(0)
  • 2021-01-24 17:48

    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 讨论(0)
提交回复
热议问题