Jump to content

Copying all subfolder from a folder to another folder


Recommended Posts

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

Link to comment
Share on other sites


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.

Link to comment
Share on other sites

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 ScriptDim 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
Link to comment
Share on other sites

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! 

Link to comment
Share on other sites

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 ScriptDim 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

Link to comment
Share on other sites

 

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

Your my hero.

Have decided to buy a couple of books and learn it from scratch :-) 

 

Thanks for the help 

 

Update: Thanks again for the info I ended up using Fld.MoveHere Obj.Path,16+Bar as (24) I seemed to create a 'copy of ***' in the new destination folder if file existed. I've ordered the 'Wiley.Microsoft.PowerShell.VBScript.and.JScript.Bible.Feb.2009' book as I think that might be a good place to start on my scripting journey :-)

 

Thanks for the links too! 

Link to comment
Share on other sites

Hi Guys so my learning quest continues. 

I've learnt to move folders now based on there names using a search string then create a folder based on the search string results. 

This is the code thus far.

Set objRegEx = CreateObject("VBScript.RegExp") objRegEx.Global = True   objRegEx.Pattern = "^." Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFolder = objFSO.GetFolder("T:\input") Set colFiles = objFolder.Files For Each objFile in colFiles    strSearchString = objFile.Name    Set colMatches = objRegEx.Execute(strSearchString)       For Each strMatch in colMatches        strFolderName1 = strMatch.Value        strFolderName = strMatch.Value        'Next line saved for removing characters from the identified string        'strFolderName = Replace(strFolderName, "S", " ")        strFolderName = "T:\output\" & strFolderName & "\"             If Not objFSO.FolderExists(strFolderName) Then                Set objNewFolder = objFSO.CreateFolder(strFolderName)            End If    Next     objFSO.MoveFile objFile.Path, strFolderName Next '-> End Of Script Message Closes Automatically After 3 Seconds CreateObject("Wscript.Shell").Popup _ "Script Completed",3,"End Of Script",4128 
Using the object pattern (objRegEx.Pattern = "^.") basically takes the first letter of the filename and creates the folder with the first letter and then places the file within that folder. Can I do a search on the same filename for a separate section to create a further sub-folder. So second search (objRegEx.Pattern = "S/d{2,}") which would be a further sub-folder. Then place the original file in that sub folder to the first folder. so using the sample above "T:\output\R\S01\filename.ext

 

Hope that makes sense 

 

Cheers in advance

Edited by gunsmokingman
Added Code Box
Link to comment
Share on other sites

You could use less of this stuff

Set objFolder = objFSO.GetFolder("T:\input") Set colFiles = objFolder.Files For Each objFile in colFiles
You could do this with the same results

 For Each Obj in objFSO.GetFolder("T:\input").Files
This

  If Not objFSO.FolderExists(strFolderName) Then   Set objNewFolder = objFSO.CreateFolder(strFolderName) End If
With this

  If Not objFSO.FolderExists(strFolderName) Then    objFSO.CreateFolder strFolderName End If
I am not sure about your question.
Link to comment
Share on other sites

Thanks GunSmokinMan for the cut down code.

 

With regards the question i was trying to use regex to look at the file name and break it down in parts to recall later and archive the file  into subfolders based on the file name.

 

So original is located as follows T:\input\Love.TV.Series.S01E01.mp4 

 

I was hoping i could break down parts of the file name and recall to make subfolders then place the file in the final subfolder.

 

so (L)(ove.TV.Series).S(01)E01.mp4

therefore (^.)(.+).S(/d{2})

 

Then place in subfolders T:\TVseries\$1\$1$2\$3\Love.TV.Series.S01E01.mp4

 

which would end up like  T:\TVseries\L\Love TV Series\01\Love.TV.Series.S01E01.mp4

 

Hope thats a bit more clear. 

 

Thanks in advance

Edited by NewScriptJunkie
Link to comment
Share on other sites

What you want done is a very complex problem, I have a script that I wrote to process my music files.

I Have a very specific way of naming my files

Babe I'm Gonna Leave You(Led Zeppelin(Rock_1969)).wma

I Split The name using ( to split the name into 3 parts

Group\Gene And Year\Song Name

Before Script

post-5386-0-22926900-1423194169_thumb.pn

After Script

post-5386-0-71702200-1423195752_thumb.pn

 

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")Dim Arg, Col, Dir, Drv, Loc, Obj'-> Start Script With Folder Paths And No Drive Letters ChkForFolder("\Music\Wma")'-> The Results From ChkForFolder Then'-> If True List The Files In Parent Folder  If Arg = True Then  MsgBox "Preparing To Process The Wma Files" & vbCrLf & _  Fso.GetFolder(".").Path,4128,"Music Processing"    ListFiles(Fso.GetFolder("."))  Else    ReportChkFolder(Arg)  End If '-> Function For Script   Function ChkForFolder(Chk)    For Each Drv In Fso.Drives     If Drv.IsReady = True Then      If Fso.FolderExists(Drv & Chk) Then       Loc = Drv & Chk        Arg = True       Exit For      Else       Arg = False       Obj = Obj & vbCrLf & Drv & Chk      End If      End If     Next    End Function '->   Function ReportChkFolder(A)    If A = False Then     Obj = "Error Pathways Do Not Exist" & vbCrLf & Obj      MsgBox Obj,4128,"Error"    End If    WScript.Quit    End Function      Function ListFiles(D)    Dim D1, D2, D3     For Each Col In D.Files      If Col.type = "Windows Media Audio file" Then       If InStr(Col,"(") Then'-> Where The Name Get Split And Then Make The Folder Name              Obj = Split(Col.Name,"(")         D1 = Loc & "\"& Obj(1)       '  WScript.Echo D1                If Not Fso.FolderExists(D1) Then Fso.CreateFolder(D1)        D2 = D1 & "\" & Replace(Obj(2),")).wma","")       ' WScript.Echo D2        If Not Fso.FolderExists(D2) Then Fso.CreateFolder(D2)        D3 = D2 & "\" & Obj(0)        If Not Fso.FolderExists(D3) Then Fso.CreateFolder(D3)       ' WScript.Echo D3        Fso.CopyFile Col.Path, D3 & "\" & Col.Name, True         Fso.DeleteFile Col,True       End If       End If       Next     MsgBox "Completed Procssing Wma Files In This Folder" & vbCrLf & _     Fso.GetFolder(".").Path,4128,"Music Processing"   End Function 
Rename ProcessMusicFiles_V1.vbs.txt to ProcessMusicFiles_V1.vbs make active

ProcessMusicFiles_V1.vbs.txt

Link to comment
Share on other sites

Progress - However is there seems to be a check in your code which I think is

 

If Col.type = "Windows Media Audio file" Then

 

I commented out (') the if statement and the end if but that creates problems later as it’s a valid check. Is there a type video or txt version of the same?

I'm now attacking the entire project of mine from another angle which is a bit long winded code but I'm learning as I go so definitely not wasted time.

Still working on the above too ;-)

 

GunSmokingMan  :thumbup

Edited by NewScriptJunkie
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...