Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vbscript error path not found while using movefolder method

Tags:

vbscript

I am fairly new to vbscript, and attempting to write a script that will pick up month and year stamped folders (2012_04) and move them to a year stamped folder (2012). I am getting a Path not found error though when I attempt to move the folder, and I can't seem to find an answer anywhere as to why it is happening.

for i = 0 to UBound(yearArray)
    Set folder = fso.GetFolder(InputP)
    Set subFold = Folder.Subfolders
    yearStamp = yearArray(i)

    if not fso.FolderExists(ArchiveP & yearStamp) then
        fso.createFolder(ArchiveP & yearStamp)
    end if

    ArchiveP = ArchiveP & yearStamp & "\"

    for each dateFold in subFold
       Set fo = fso.GetFolder(InputP & dateFold.Name)
       folderName = InputP & dateFold.name & "\"
       foldName = fo.name & "\"

       if left(foldName,4) = yearStamp then
          fso.MoveFolder folderName , ArchiveP & foldName
       end if
    next

    ArchiveP = UnChangeP & PreArchP
Next

The error happens at fso.MoveFolder folderName , ArchiveP & foldName and I can't figure out what is happening.

like image 243
Tapialj Avatar asked Jul 07 '26 20:07

Tapialj


1 Answers

The error you're getting is caused by misconstructed paths. What you're trying to do is something like this:

fso.MoveFolder "C:\input\2013_03", "D:\archive\2013\2013_03"

However, what you're acutally doing is this:

fso.MoveFolder "C:\input\2013_03\", "D:\archive\2013\2013_03\"
                                ^                           ^

A trailing backslash is only valid in the destination path, and only if the destination path is the parent folder to which you want to move the source folder, i.e. your statement should look either like this:

fso.MoveFolder "C:\input\2013_03", "D:\archive\2013\"

or like this:

fso.MoveFolder "C:\input\2013_03", "D:\archive\2013\2013_03"

Avoid building paths via string concatenation. The FileSystemObjects provides a method BuildPath that will handle path separators correctly.

Your code is rather convoluted, BTW. Instead of using indexed access to yearArray you could simply iterate over all elements with a For Each loop. Also, your iteration over the subfolders of InputP already provides you with Folder objects. fso.GetFolder(InputP & dateFold.Name) is the exact same object as dateFold. Plus, Folder objects come with a Move method, so you'd only need to handle the destination path.

I believe your code could be simplified to the following, which should do what you want:

For Each year In yearArray
  dst = fso.BuildPath(ArchiveP, year)
  If Not fso.FolderExists(dst) Then fso.CreateFolder dst

  For Each dateFold In fso.GetFolder(InputP).SubFolders
    If Left(dateFold.Name, 4) = year Then dateFold.Move dst & "\"
  Next
Next

In terms of performance it might be a good idea to switch the two loops, though. Iterating over folders means you have to read from disk whereas yearArray is in memory, thus the former iteration is bound to be slower than the latter. By making the subfolder iteration the outer loop (and putting the destination folder creation in a separate loop) you eliminate this bottleneck, because that way you read each subfolder just once.

For Each year In yearArray
  dst = fso.BuildPath(ArchiveP, year)
  If Not fso.FolderExists(dst) Then fso.CreateFolder dst
Next

For Each dateFold In fso.GetFolder(InputP).SubFolders
  For Each year In yearArray
    dst = fso.BuildPath(ArchiveP, year)
    If Left(dateFold.Name, 4) = year Then dateFold.Move dst & "\"
  Next
Next
like image 186
Ansgar Wiechers Avatar answered Jul 14 '26 23:07

Ansgar Wiechers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!