No announcement yet.

VBS - passing variable to shell namespace

  • Filter
  • Time
  • Show
Clear All
new posts

  • VBS - passing variable to shell namespace


    First things first, I'm a bit of a novice with this so please bear with me if I don't explain very well.

    I'm trying to write a .VBS which passes a variable to the Namespace method of the Shell application.

    Essentially, the steps are:

    1) Use WMI query to return a list of folders from CIM_DATAFILE, set as colFiles
    2) For each folder returned by the query, do the following:

    For Each objFile in colFiles
    drivepath = objFile.Drive & objFile.Path
    filename = objFile.Name

    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(drivepath)
    Set objFolderItem = objFolder.ParseName(filename)

    For i = 0 to 20
    WScript.Echo objFolder.GetDetailsOf(objFolder.Item, i)

    I cannot get this to pass the drivepath variable to Namespace method.
    I know that one folder returned by the WMI query is C:\temp

    If I set drivepath = "C:\temp" and filename = "file.txt" inside the first FOR loop, the script passes "C:\temp" and "file.txt" to the Set objFolder and Set objFolderItem commands (although this then processes the GetDetailsOf method once each for the count of folders returned by the WMI query) & returns the properties of file.txt

    I'm at a loss... I kinda need Variable A = Variable B where variable B changes each time and dynamically updates Variable A - is this possible?
    I'm a bit stuck

  • #2
    Re: VBS - passing variable to shell namespace

    Originally posted by rebuildthepier View Post
    drivepath = objFile.Drive & objFile.Path
    Try printing dirvepath with wscript.echo method.
    wscript.echo "Drivepath: " & drivepath
    In my quick test it used to return a path like c:c:\users\vonpryz\foo\files.vbs. The extra drive letter obviously won't work, so the script crashes.

    Next time, please post complete code that demonstrates the error you got. It might be that your WMI part returns something funny and my quick FileSystemObject.GetFolder().Files call returns different stuff. Makes debugging easier.



    • #3
      Re: VBS - passing variable to shell namespace

      You should remove the backslash that is added to the end of 'drivepath'.
      drivepath = objFile.Drive & objFile.Path

      drivepath = objFile.Drive & objFile.Path
      drivepath = Left(drivepath, Len(drivepath)-1)

      This posting is provided "AS IS" with no warranties, and confers no rights.


      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts