![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | move directories and files that exist in a sub folder - problem Hi all, I am trying to create an automated script that searches in a sub directory for files and directories. Once it counts if any exist, it creates a sub directory and moves these files and directories into it. I am having problems trying to achieve this as it is not as straight forward as I initially thought as you have to first of all search for files in a function and then search for directories in a seperate function as I have not seen an object that does both types (pretty much like a dir, although I want to use the fso). Ultimately I want to be able to store the paths of all the files and directories that the fso object finds in an array and then enumerate through this in a for each and move these into another directory. Check out my script idea below; Dim fso, f, f1, fc, s, folderspec Set fso = CreateObject("Scripting.FileSystemObject") folderspec = "C:\scripts\test" Set f = fso.GetFolder(folderspec) Set fc = f.Files countf = fc.count set fd = f.subfolders countd = fc.count fldr = "C:\scripts\test\my documents" 'Check to see whether the my documents folder exists mydocsexists = fso.FolderExists(fldr) if mydocsexists = "False" then 'Check to see if any files exist if countf <> 0 then 'Move all Files For Each fitem in fc file = file & fitem.path & VBCRLF Next end if 'Check to see if any directories exist if countd <> 0 then 'Move All Directories For Each ditem in fd folder = ditem.path next end if 'Create my documents folder fso.createfolder("C:\Scripts\Test\My Documents") 'Here I would like to enumerate through the folder and file variables 'That were collected earlier and move the files into the my documents folder above 'I get an error when I try and read a for each variable thats not a collection ' How do i create a collection for this information? end if Thanks -- Firewalker82 |
My System Specs![]() |
| | #2 (permalink) |
| | Re: move directories and files that exist in a sub folder - problem "Firewalker82" <Firewalker82@xxxxxx> wrote in message news:FE40FDF8-F310-4577-B494-00A01C2C03D9@xxxxxx Quote: > Hi all, > > I am trying to create an automated script that searches in a sub directory > for files and directories. Once it counts if any exist, it creates a sub > directory and moves these files and directories into it. I am having > problems trying to achieve this as it is not as straight forward as I > initially thought originally thought, the cause is sometimes the complexity of the scripting solution, but sometimes it is the complexity of the problem itself. Occasionally, there are elements of both. It is usually a good idea to determine how this applies before the best path can be found. Quote: > as you have to first of all search for files in a function > and then search for directories in a seperate function as I have not seen > an > object that does both types (pretty much like a dir, although I want to > use > the fso). fear, however, that this would introduce additional complexity, as you would be tempted to want to also combine (or overload) all of the operations that could be done on files or folders into one; things like create, rename, delete. The two types of objects are, however, intrinsically different. Quote: > Ultimately I want to be able to store the paths of all the files and > directories that the fso object finds in an array and then enumerate > through > this in a for each and move these into another directory. differently... Quote: > Check out my > script idea below; > > Dim fso, f, f1, fc, s, folderspec > Set fso = CreateObject("Scripting.FileSystemObject") > folderspec = "C:\scripts\test" > > Set f = fso.GetFolder(folderspec) > Set fc = f.Files > countf = fc.count > set fd = f.subfolders > countd = fc.count countd = fd.count Quote: > fldr = "C:\scripts\test\my documents" > > 'Check to see whether the my documents folder exists > mydocsexists = fso.FolderExists(fldr) > > if mydocsexists = "False" then if ( not ( fso.FolderExists(fldr) ) ) then Quote: > 'Check to see if any files exist > if countf <> 0 then which there are no files, then the body of the for each loop below will be executed zero times. Quote: > > 'Move all Files completely different here. Quote: > For Each fitem in fc > file = file & fitem.path & VBCRLF state, then that variable should be set to that state before the loop. In this case you should have a FILE = "" statement somewhere before the FOR statement. Quote: > Next > > end if > > 'Check to see if any directories exist > > if countd <> 0 then Quote: > 'Move All Directories > For Each ditem in fd > folder = ditem.path be set to the path of only the last one. You need something like what you do in the file loop above, i.e.: folder = folder & ditem.path & VBCRLF Quote: > next > end if > > 'Create my documents folder > fso.createfolder("C:\Scripts\Test\My Documents") > > 'Here I would like to enumerate through the folder and file variables > 'That were collected earlier and move the files into the my documents > folder above > 'I get an error when I try and read a for each variable thats not a > collection problem is here. But if you are simply using the path names previously stored in the file and folder variables, you will need to either convert them to a file or folder object, or use one of the MOVE methods connected to the object of the destination folder that accepts a file or folder name to be copied there. Quote: > ' How do i create a collection for this information? loops rather than saving their names to use later? Or, alternately... As I understand it, you are moving the entire contents of one folder that exists to another that does not yet exist. Why don't you just MOVE THAT FOLDER, rename the result if necessary, and be done with it? /Al |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| watch folder and rotate and move Jpg files | VB Script | |||
| Copy/Move file and rename if destination exist | PowerShell | |||
| Move Offline Files folder | Vista General | |||
| Move Folder/Files bug | Vista General | |||
| Cannot move files to public folder | Vista networking & sharing | |||