Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

Copying all subfolder from a folder to another folder

- - - - -

  • Please log in to reply
5 replies to this topic

#1
NewScriptJunkie

NewScriptJunkie
  • Member
  • 3 posts
  • Joined Yesterday, 09:59 AM
  • OS:XP Pro x64
  • Country: Country Flag

Hi Guys,

I'm new to VBS and only learn from using. I’ve search the forum for this and found similar but still can’t seem to get it right and what I’m trying to do seems so simple.  I’m using the following script to copy all files maybe 5 sub folders deep from a root folder ‘A’ to a destination folder ‘B’ but it also copies the root folder ‘A’ but I don’t want that just what’s in ‘A’ if that makes sense. The subfolders in a are never the same and change each time I run the script.  Here’s the script I have been using. So it’s the contents of A I want (not including A) to move to B. I want A (root) to remain after but no subfolder inside.  

 

Const FOF_CREATEPROGRESSDLG = &H0&

TargetFolder = "T:\My folder\B\"

Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.NameSpace(TargetFolder)

objFolder.MoveHere "T:\My folder\A\", FOF_CREATEPROGRESSDLG

 

Many thanks in advance




How to remove advertisement from MSFN

#2
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,589 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

try this:

CreateObject("Scripting.FileSystemObject").CopyFolder "T:\My Folder\A", "T:\My Folder\B"

if you are doing a move instead of a copy you could do this:

CreateObject("Scripting.FileSystemObject").MoveFolder "T:\My Folder\A", "T:\My Folder\B"

In the above case you should of course have an existing source directory and no existing destination directory.

 

I know the above aren't exactly your requirements, but they should help with your learning. In the latter example for instance the original source folder has disappeared, which wasn't your requirement. Why you want to keep an empty directory I don't know, but you could of course just create it again if you feel the need.


  • NewScriptJunkie likes this

#3
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,440 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
Try this VBS script and see if it what you want, it will move all the folders in the parent folder to Set Fld folder and leave the parent folder empty.
Const Bar = &H0&
'-> Objects For Script
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Shl :Set Shl = CreateObject("Shell.Application")
Dim Fld, Obj
'-> Fso.GetFolder(".") Makes The Folder Whee The Script Is Located 
'-> The Parent Folder. Place A Pathway Fso.GetFolder("Drive:\FolderName")
'-> To Use Another Folder
  For Each Obj In Fso.GetFolder(".").SubFolders    
'-> Check To Make Sure It Not In The Pth Folder
   Set Fld = Shl.NameSpace("C:\Users\Gunsmokingman\Desktop\ToThere")
   If Not Fld Is Nothing Then
    'WScript.Echo Obj.Path
    Fld.MoveHere Obj.Path,Bar
   End If
  Next
'-> End Of Script Message Closes Automatically After 5 Seconds
 CreateObject("Wscript.Shell").Popup _
 "Script Completed",5,"End Of Script",4128

  • NewScriptJunkie likes this


GunSmokingMan



#4
NewScriptJunkie

NewScriptJunkie
  • Member
  • 3 posts
  • Joined Yesterday, 09:59 AM
  • OS:XP Pro x64
  • Country: Country Flag

Thank guys for your responses. The original folder has to stay even as empty as it gets repopulated by a separate file sorting programme which I want to eventually replace when I know enough scripting to do exactly what I want utilising VBscripting. Again thanks guys will let you know how I get on! 



#5
NewScriptJunkie

NewScriptJunkie
  • Member
  • 3 posts
  • Joined Yesterday, 09:59 AM
  • OS:XP Pro x64
  • Country: Country Flag

Try this VBS script and see if it what you want, it will move all the folders in the parent folder to Set Fld folder and leave the parent folder empty.

Const Bar = &H0&
'-> Objects For Script
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Shl :Set Shl = CreateObject("Shell.Application")
Dim Fld, Obj
'-> Fso.GetFolder(".") Makes The Folder Whee The Script Is Located 
'-> The Parent Folder. Place A Pathway Fso.GetFolder("Drive:\FolderName")
'-> To Use Another Folder
  For Each Obj In Fso.GetFolder(".").SubFolders    
'-> Check To Make Sure It Not In The Pth Folder
   Set Fld = Shl.NameSpace("C:\Users\Gunsmokingman\Desktop\ToThere")
   If Not Fld Is Nothing Then
    'WScript.Echo Obj.Path
    Fld.MoveHere Obj.Path,Bar
   End If
  Next
'-> End Of Script Message Closes Automatically After 5 Seconds
 CreateObject("Wscript.Shell").Popup _
 "Script Completed",5,"End Of Script",4128

gunsmokingman,

Thank you this worked exactly how I needed it too. I just need to make the script use the 'yes to all' or suppress the confirmation of overwrite if the folder already exists. And many thanks for the comments within the script to help me understand what part is doing what.

 

Many Thanks



#6
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,440 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
Here are some links to help you with your scripts
MoveHere

Shell.Windows method

To add copy to a filename if exists and no dialog to ask yes no
Fld.MoveHere Obj.Path,24+Bar



GunSmokingMan






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users