Solved

bring files two levels up

Posted on 2011-09-15
14
269 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
  • 6
  • 6
  • 2
14 Comments
 
LVL 53

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Expert Comment

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

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 53

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 53

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 53

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 53

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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