Solved

bring files two levels up

Posted on 2011-09-15
14
273 Views
Last Modified: 2012-05-12
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
Comment
Question by:mcrmg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
14 Comments
 
LVL 56

Expert Comment

by:Bill Prew
ID: 36545057
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
 

Expert Comment

by:haggard
ID: 36545061
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
 

Author Comment

by:mcrmg
ID: 36545552
is it possible to add to "skip" or overwite the files that have already existed?  thnaks
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Expert Comment

by:haggard
ID: 36545835
Disregard my previous answer. I misunderstood your question.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 36545885
==> 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
 

Author Comment

by:mcrmg
ID: 36545929
overwuite...lol
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 36545999
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
 

Author Comment

by:mcrmg
ID: 36546036
still getting File already exist error.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 36546067
Okay, I'll set up some testing here and see how it goes.

~bp
0
 

Author Comment

by:mcrmg
ID: 36546073
thanks
0
 
LVL 56

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 36546891
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
 

Author Comment

by:mcrmg
ID: 36548267
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
 
LVL 56

Expert Comment

by:Bill Prew
ID: 36548579
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
 

Author Comment

by:mcrmg
ID: 36548673
thank you very much
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question