Folder Already Exists Error, but it doesn't

Author Message
myara

  • Total Posts : 5
  • Scores: 0
  • Reward points : 0
  • Joined: 11/29/2011
  • Status: offline
Folder Already Exists Error, but it doesn't Tuesday, November 29, 2011 8:29 PM (permalink)
0
Hi, I keep getting folder already exists error messages but it doesn't exists.
 
'Ive got this folder
'C:\TEST\TEST0\TEST0
 
And this subroutine:

sub move (oSubFolder, oFolder)
oFolderPath = oFolder.Path
 
'Rename folder
oSubFolder.name = oSubFolder.name & "_temp"

'so it should be C:\TEST\TEST0\TEST0_temp now

'Move it to parent-parent
FSO.MoveFolder oSubFolder, oFolder.ParentFolder & "\"

'now I should have C:\TEST\TEST0
'and C:\TEST\TEST0_temp

'Delete parent
oFolder.delete

'It should delete TEST0 so
'only C:\TEST\TEST0_temp is left

'Get Moved folder
set oSubFolderMoved = FSO.GetFolder(oFolderPath & "_temp")
'Rename back
originalname = Left(oSubFolder.name, Len(oSubFolder.name) - 5)

'Since I had "Already exists" errors, I put a if FolderExists before rename it back

if not FSO.FolderExists(Left(oSubFolderMoved.path, Len(oSubFolderMoved.path) - 5)) then
oSubFolderMoved.name = originalname
end if
end sub
 
It passed the if FolderExists so there isn't any folder named C:\TEST\TEST0 
I get the same error with FSO.CreateFolder too.
<message edited by myara on Tuesday, November 29, 2011 8:33 PM>
 
#1
    59cobalt

    • Total Posts : 969
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Folder Already Exists Error, but it doesn't Wednesday, November 30, 2011 6:59 AM (permalink)
    0
    myara
    'Move it to parent-parent
    FSO.MoveFolder oSubFolder, oFolder.ParentFolder & "\"
    
    'now I should have C:\TEST\TEST0
    'and C:\TEST\TEST0_temp
    At that point you do have those two folders. However, since you used the FileSystemObject MoveFolder() method, your oSubFolder object still references the now non-existent folder C:\TEST\TEST0\TEST0_temp, which should cause your script to fail in this line:
    originalname = Left(oSubFolder.name, Len(oSubFolder.name) - 5)
    Since it apparently doesn't, you most likely have an "On Error Resume Next" somewhere in your code. Remove it. And change your function to this:
    Sub Move(oSubFolder, oFolder)
     oSubFolder.Name = oSubFolder.Name & "_temp"
     oSubFolder.Move oFolder.ParentFolder & "\"
     oFolder.Delete True
     oSubFolder.Name = Left(oSubFolder.Name, Len(oSubFolder.Name) - 5)
    End Sub

     
    #2
      myara

      • Total Posts : 5
      • Scores: 0
      • Reward points : 0
      • Joined: 11/29/2011
      • Status: offline
      Re:Folder Already Exists Error, but it doesn't Wednesday, November 30, 2011 1:48 PM (permalink)
      0

      Thanks for your help!

      It didn't work though.

      I'm not very familiar with FSO or "windows" vbscript. I had no idea FSO didn't "communicate" with the Folder Collection or their objects.

      Anyway, just to be sure I deleted all FSO commands. Now I'm using FSO only to convert the path to object.

      I'm not using "on error resume next". Like you said, SubFolder is still referring to a non-existent folder so the .path doesn't exist anymore, but the .name is still "valid" as a string. So the line Left(oSubFolder.name, Len(oSubFolder.name) - 5) is valid. At least it seams to work but just to be sure, I got rid of it too.

      I tried your code and get the same error. I just don't get it.
       
      Tried renaming with Folder.name and FSO.MoveFolder. Both failed.

      In one of my loops experiments with this script, I do a SubFolders.Count and I get "4 folders" where there should be only 3, but when I do a list with a for loop I only get 3 messages.

      It's like the deleted action has not been acknowledge by some commands, so Count, Rename and FSO.Create Folder thinks the deleted folder is still there. I though that it was a problem with the Folder Collection not "refreshing", but even the FSO.Create Folder thinks the deleted folder exists, even when FSO.FolderExists says it doesn't.
       
      My temporal solution was to create another sub routine loop for renaming, and run it after the [move] subroutine.

      By the way how do you write those cool highlighted codes?

      <message edited by myara on Wednesday, November 30, 2011 3:08 PM>
       
      #3
        ebgreen

        • Total Posts : 8227
        • Scores: 98
        • Reward points : 0
        • Joined: 7/12/2005
        • Status: offline
        Re:Folder Already Exists Error, but it doesn't Thursday, December 01, 2011 4:14 AM (permalink)
        0
        oSubFolder.name = oSubFolder.name & "_temp"
         
        What exactly is oSubFolder? Since you are only showing us a portion of the script, we can't really give you a good answer.
        "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
        Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
        http://www.visualbasicscript.com/m_47117/tm.htm
         
        #4
          myara

          • Total Posts : 5
          • Scores: 0
          • Reward points : 0
          • Joined: 11/29/2011
          • Status: offline
          Re:Folder Already Exists Error, but it doesn't Thursday, December 01, 2011 4:41 AM (permalink)
          0
          The script is about eliminate redundant folders. I was trying to write something to delete those annoying duplicated folders.
          C:\folder\folder\
          to
          C:\folder

          So my logic was:
          if the subfolder name = folder name,  rename subfolder with "_temp" and move it to the same level (folder parent). Then delete folder, and rename the moved subfolder.
          The thing is, when I try to rename the moved subfolder I get this error, the deleted folder "exists".
          Line 9 Char 1 [File already exists]

          oSubFolder is just a subfolder from a folder collection.

          I'll write just the most important part because I think this would be more understandable than the whole code (not that I don't want to share my code):

          sPath = "D:\TEMP\TEST"             
           DIM FSO : Set FSO = CreateObject("Scripting.FileSystemObject")               
           set oFolder= FSO.GetFolder(sPath)                
           for each oSubFolder in oFolder.SubFolders                
           if oSubFolder.name = oFolder.name Then                
           oSubFolder.name = oSubFolder.name & "_temp"                
           oSubFolder.Move oFolder.ParentFolder & "\"           
           oFolder.delete                
           oSubFolder.name = Left(oSubFolder.name, Len(oSubFolder.name) - 5)  
           end if 
           next   
           

          -------------------------------------
          The code I wrote with two sub routines is here:
          http://myara.web.fc2.com/Scripts/mRedundantFolderCleaner.vbs

          Thanks in advance
          <message edited by myara on Thursday, December 01, 2011 5:12 AM>
           
          #5
            59cobalt

            • Total Posts : 969
            • Scores: 91
            • Reward points : 0
            • Joined: 7/17/2011
            • Status: offline
            Re:Folder Already Exists Error, but it doesn't Thursday, December 01, 2011 6:44 AM (permalink)
            0
            myara
            sPath = "D:\TEMP\TEST"
            DIM FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
            set oFolder= FSO.GetFolder(sPath)
             for each oSubFolder in oFolder.SubFolders
             if oSubFolder.name = oFolder.name Then
             oSubFolder.name = oSubFolder.name & "_temp"
             oSubFolder.Move oFolder.ParentFolder & "\"
             oFolder.delete
             oSubFolder.name = Left(oSubFolder.name, Len(oSubFolder.name) - 5)
             end if
             next
            That code raised an "Access denied" error when I tested it. Perhaps an open handle to oFolder due to the loop, which prevents deletion of the folder. However, the loop is pointless anyway. You can simply use the FolderExists() method to determine if your folder has a subfolder of the same name.
            subFolderPath = FSO.BuildPath(oFolder.Path, oFolder.Name)
            If FSO.FolderExists(subFolderPath) Then
             Set oSubFolder = FSO.GetFolder(subFolderPath)
             oSubFolder.Name = oSubFolder.Name & "_temp"
             oSubFolder.Move oFolder.ParentFolder & "\"
             oFolder.Delete True
             oSubFolder.Name = Left(oSubFolder.Name, Len(oSubFolder.Name) - 5)
            End If
            Or you could do it the other way 'round: rename the parent folder, move the subfolder, then delete the parent folder.
            subFolderName = oFolder.Name
            If FSO.FolderExists(FSO.BuildPath(oFolder.Path, subFolderName)) Then
             oFolder.Name = oFolder.Name & "_temp"
             FSO.MoveFolder FSO.BuildPath(oFolder.Path, subFolderName), oFolder.ParentFolder & "\"
             oFolder.Delete True
            End If

             
            #6
              myara

              • Total Posts : 5
              • Scores: 0
              • Reward points : 0
              • Joined: 11/29/2011
              • Status: offline
              Re:Folder Already Exists Error, but it doesn't Thursday, December 01, 2011 12:38 PM (permalink)
              0
              The "Access Denied" raises when you are running the script while having one or more of this folders opened in Windows Explorer. I can't find any solution to this but to close Explorer before start the script.

              The loop isn't pointless because the complete code is to find duplicated folders in the whole folder tree. I didn't wrote the entire loop because the problem isn't there.

              I'll try renaming the parent, since the problem was with the deleted folder not being recognized I guess it will work. But still, I can't understand why the other way doesn't.

              Thanks.

              [UPDATE]

              I think I've got it working. It wasn't smooth though.
              I remembered why I didn't do the parent rename. If I rename the parent the child and parent's parent will be invalid. So I had to do a FSO.GetFolder to convert the renamed path to an object and create an string with the parent path before renaming or it won't be recognized either.

              Something like this:

              sub moveren(oFolder)    
               if oFolder.SubFolders.Count >0 Then    
               for each oSubFolder in oFolder.SubFolders    
               if oSubFolder.name = oFolder.name Then    
               parentpath = oFolder.ParentFolder.path & "\"    
               oFolder.name = oFolder.name & "_temp"    
               set oSubFolder = FSO.GetFolder(oFolder.path & "\" & Left(oFolder.name, Len(oFolder.name) - 5))    
               oSubFolder.Move parentpath    
               oFolder.delete    
               moveren(oSubFolder)    
               else    
               moveren(oSubFolder)    
               end if    
               next    
               end if    
               end sub    
               

              [UPDATE 2]
              Nope it doesn't work. It works only the first loop. Then in the second loop the deleted file "ghost" is still present so the moved child can't be rename to "_temp".

              I mean, If I've got:
              C:\folder\folder\folder
              then it's renamed to
              C:\folder_temp\folder\folder
              then folder is moved under the parent's parent and folder_temp is deleted:
              C:\folder\folder
              loops with the moved child so it will try to rename it. It should be :
              C:\folder_temp\folder
              but it just fails because the former folder_temp ghost is still present and I get the same "File already exists" error again.

              So my conclusion is that  it's a VBScript Folder Collection limitation not being able to refresh itself.
              <message edited by myara on Thursday, December 01, 2011 1:23 PM>
               
              #7
                59cobalt

                • Total Posts : 969
                • Scores: 91
                • Reward points : 0
                • Joined: 7/17/2011
                • Status: offline
                Re:Folder Already Exists Error, but it doesn't Thursday, December 01, 2011 2:36 PM (permalink)
                0
                A folder collection is updated automatically, but not necessarily while you're iterating over its items. Every time you pull a subfolder up, you MUST re-set the variable that references the path you just manipulated.
                Set FSO = CreateObject("Scripting.FileSystemObject")
                
                Sub Mangle(oFolder)
                 For Each oSubFolder In oFolder.SubFolders
                 While FSO.FolderExists(FSO.BuildPath(oSubFolder.Path, oSubFolder.Name))
                 subFolderPath = oSubFolder.Path
                 subFolderName = oSubFolder.Name
                 oSubFolder.Name = oSubFolder.Name & "_temp"
                 FSO.MoveFolder FSO.BuildPath(oSubFolder.Path, subFolderName), oFolder.Path & "\"
                 oSubFolder.Delete True
                 Set oSubFolder = FSO.GetFolder(subFolderPath)
                 Wend
                 Mangle oSubFolder
                 Next
                End Sub
                
                Mangle FSO.GetFolder("C:\folder")

                <message edited by 59cobalt on Thursday, December 01, 2011 2:38 PM>
                 
                #8
                  myara

                  • Total Posts : 5
                  • Scores: 0
                  • Reward points : 0
                  • Joined: 11/29/2011
                  • Status: offline
                  Re:Folder Already Exists Error, but it doesn't Thursday, December 01, 2011 6:25 PM (permalink)
                  0
                  Thanks! it worked !
                   
                  I'll have to remember to re-set every object from now on.
                  I'm more use to use VBS in XSI (3D software) where you don't have to do that with XSI Objects or Collections.
                   
                  I just modified it a little your code so it can check the input folder too and put some other condition lines.
                   
                  #9
                    59cobalt

                    • Total Posts : 969
                    • Scores: 91
                    • Reward points : 0
                    • Joined: 7/17/2011
                    • Status: offline
                    Re:Folder Already Exists Error, but it doesn't Friday, December 02, 2011 9:04 AM (permalink)
                    0
                    myara
                    Thanks! it worked !
                    You're welcome.

                    myara
                    I'll have to remember to re-set every object from now on.
                    No, you don't. That was only required in this particular situation, because you wanted to substitute the folder object while using an iterator.
                     
                    #10

                      Online Bookmarks Sharing: Share/Bookmark

                      Jump to:

                      Current active users

                      There are 0 members and 1 guests.

                      Icon Legend and Permission

                      • New Messages
                      • No New Messages
                      • Hot Topic w/ New Messages
                      • Hot Topic w/o New Messages
                      • Locked w/ New Messages
                      • Locked w/o New Messages
                      • Read Message
                      • Post New Thread
                      • Reply to message
                      • Post New Poll
                      • Submit Vote
                      • Post reward post
                      • Delete my own posts
                      • Delete my own threads
                      • Rate post

                      2000-2012 ASPPlayground.NET Forum Version 3.9