Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

bring files two levels up

Hi,

EEs helped me with this long time ago, what it does is to bring all files to one level up.
aaa
      folder1
         file1
      folder2
         file2
to

bbb
   file1
   file2


is there a way to change the code to bring the files two levels up?
aaa
    folder1
      sub1
         file1
      sub2
         file2
to

bbb
   file1
   file2


thanks
'This will copy all files to one level up

'option explicit

dim SourceFolder, DestFolder ,fso, f
Set fso = CreateObject("Scripting.FileSystemObject")




SourceFolder = "C:\aaa\"
DestFolder = "C:\bbb\"

Set f = fso.GetFolder(SourceFolder)
For Each file In f.Files
       fso.MoveFile SourceFolder  & file.Name, DestFolder  & file.name
Next 

For Each subdir in f.SubFolders 
     Set f = fso.GetFolder(SourceFolder  & subdir.name)
          For Each file In f.Files
               fso.copyFile SourceFolder  & subdir.name & "\" & file.Name, DestFolder  & file.name
          Next      
Next 

msgbox "Finished!!!"

Open in new window

0
mcrmg
Asked:
mcrmg
  • 6
  • 6
  • 2
1 Solution
 
Bill PrewCommented:
It looked like wanted the files 0, 1 and 2 levels down all moved, so that would look like this:

'This will copy all files to two levels up
Set oFSO = CreateObject("Scripting.FileSystemObject")

sBaseDir = "C:\aaa\"
sDestDir = "C:\bbb\"

Set oBaseDir = oFSO.GetFolder(sBaseDir)

For Each oFile In oBaseDir.Files
    oFile.Move DestFolder & oFile.Name
Next 

For Each oSubDir1 in oBaseDir.SubFolders 
    For Each oFile In oSubDir1.Files
        oFile DestFolder & oFile.Name
    Next      
    For Each oSubDir2 in oSubDir1.SubFolders 
        For Each oFile In oSubDir2.Files
            oFile DestFolder & oFile.Name
        Next      
    Next 
Next 

MsgBox "Finished!!!"

Open in new window

~bp
0
 
haggardCommented:
I'm not positive tat I understand your question but I am guessing that what you want to do is to use c:\ as the base of your destination tree instead of c:\bbb. If that is the case then this should do it.

Change this line:
DestFolder = "C:\bbb\"

to
DestFolder = "C:\"

0
 
mcrmgAuthor Commented:
is it possible to add to "skip" or overwite the files that have already existed?  thnaks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
haggardCommented:
Disregard my previous answer. I misunderstood your question.
0
 
Bill PrewCommented:
==> is it possible to add to "skip" or overwite the files that have already existed?

Yes, but which do you want, to skip them, or to overwrite them?

~bp
0
 
mcrmgAuthor Commented:
overwuite...lol
0
 
Bill PrewCommented:
See how this goes.

'This will copy all files to two levels up
Set oFSO = CreateObject("Scripting.FileSystemObject")

sBaseDir = "C:\aaa\"
sDestDir = "C:\bbb\"

Set oBaseDir = oFSO.GetFolder(sBaseDir)

For Each oFile In oBaseDir.Files
    oFile.Move DestFolder & oFile.Name
Next 

For Each oSubDir1 in oBaseDir.SubFolders 
    for Each oFile In oSubDir1.Files
        If oFSO.FileExists(DestFolder & oFile.Name) Then
           oFSO.DeleteFile(DestFolder & oFile.Name)
        End If
        oFile.Move DestFolder & oFile.Name
    Next      
    For Each oSubDir2 in oSubDir1.SubFolders 
        For Each oFile In oSubDir2.Files
           If oFSO.FileExists(DestFolder & oFile.Name) Then
              oFSO.DeleteFile(DestFolder & oFile.Name)
           End If
            oFile.Move DestFolder & oFile.Name
        Next      
    Next 
Next 

MsgBox "Finished!!!"

Open in new window

~bp
0
 
mcrmgAuthor Commented:
still getting File already exist error.
0
 
Bill PrewCommented:
Okay, I'll set up some testing here and see how it goes.

~bp
0
 
mcrmgAuthor Commented:
thanks
0
 
Bill PrewCommented:
This works properly here, after I fixed a mistake I found.

'This will copy all files to two levels up
Set oFSO = CreateObject("Scripting.FileSystemObject")

sBaseDir = "C:\EE\EE27310567\base\"
sDestDir = "C:\EE\EE27310567\dest\"

Set oBaseDir = oFSO.GetFolder(sBaseDir)

For Each oFile In oBaseDir.Files
    oFile.Move DestFolder & oFile.Name
Next 

For Each oSubDir1 in oBaseDir.SubFolders 
    for Each oFile In oSubDir1.Files
        If oFSO.FileExists(sDestDir & oFile.Name) Then
           oFSO.DeleteFile(sDestDir & oFile.Name)
        End If
        oFile.Move sDestDir & oFile.Name
    Next      
    For Each oSubDir2 in oSubDir1.SubFolders 
        For Each oFile In oSubDir2.Files
           If oFSO.FileExists(sDestDir & oFile.Name) Then
              oFSO.DeleteFile(sDestDir & oFile.Name)
           End If
            oFile.Move sDestDir & oFile.Name
        Next      
    Next 
Next 

MsgBox "Finished!!!"

Open in new window

~bp
0
 
mcrmgAuthor Commented:
this piece is still giving me File Exist Error

For Each oFile In oBaseDir.Files
    oFile.Move DestFolder & oFile.Name
Next

after I removed it, it works fine.  There is no files in the root, but I dont undeerstand why it did not work..thanks
0
 
Bill PrewCommented:
Okay, see if this works better.  I think this may be a VB version issue or a Windows version issue.  I never have to check the Files.Count, and can just do the loop and if there are no files it doesn't execute the loop and continues on.  But I see code like below often and suspect it's because of the behavior you are mentioning.

'This will copy all files to two levels up
Set oFSO = CreateObject("Scripting.FileSystemObject")

sBaseDir = "C:\EE\EE27310567\base\"
sDestDir = "C:\EE\EE27310567\dest\"

Set oBaseDir = oFSO.GetFolder(sBaseDir)

If oBaseDir.Files.Count > 0 Then
    For Each oFile In oBaseDir.Files
        oFile.Move DestFolder & oFile.Name
    Next 
End If

For Each oSubDir1 in oBaseDir.SubFolders 
    If oSubDir1.Files.Count > 0 Then
       For Each oFile In oSubDir1.Files
           If oFSO.FileExists(sDestDir & oFile.Name) Then
              oFSO.DeleteFile(sDestDir & oFile.Name)
           End If
           oFile.Move sDestDir & oFile.Name
       Next      
    End If
    For Each oSubDir2 in oSubDir1.SubFolders 
        If oSubDir2.Files.Count > 0 Then
           For Each oFile In oSubDir2.Files
              If oFSO.FileExists(sDestDir & oFile.Name) Then
                 oFSO.DeleteFile(sDestDir & oFile.Name)
              End If
               oFile.Move sDestDir & oFile.Name
           Next      
        End If
    Next 
Next 

MsgBox "Finished!!!"

Open in new window

~bp
0
 
mcrmgAuthor Commented:
thank you very much
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now