Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - move directories and files that exist in a sub folder - problem

Reply
 
Old 08-02-2008   #1 (permalink)
Firewalker82


 
 

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 SpecsSystem Spec
Old 08-02-2008   #2 (permalink)
Al Dunbar


 
 

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
Be careful, because when one finds things less straightforward than
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).
One could create one's own class to combine the two functions into one. I
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.
If done that way, you would still have to handle files and folders
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
change last line above to:

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
I would strongly recommend replacing the last two statements above with:

if ( not ( fso.FolderExists(fldr) ) ) then
Quote:

> 'Check to see if any files exist
> if countf <> 0 then
Why bother checking the count of files? If fc is a collection of files in
which there are no files, then the body of the for each loop below will be
executed zero times.
Quote:

>
> 'Move all Files
Your comment says you are moving the files, but the code is doing something
completely different here.
Quote:

> For Each fitem in fc
> file = file & fitem.path & VBCRLF
Best practice: if a loop assumes that a variable will start in a particular
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
Why bother checking the count of directories (see explanation above)?
Quote:

> 'Move All Directories
> For Each ditem in fd
> folder = ditem.path
If there are three folders in this collection, then the FOLDER variable will
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
Without showing us the code, it is difficult for us to say what the specific
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?
Again, why bother? Why not move each folder and file directly in the two for
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46