Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selective File Copy, Subfolders too

Status
Not open for further replies.

tumblor

Technical User
Nov 14, 2001
7
US
I am looking to achieve the following:

1) Copy a folder maintaining the file structure of the source

2) Copy only certain extensions like *.doc and *.xls

Now I have two ways I've been trying this, WMI:

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("SELECT * FROM CIM_DataFile WHERE Extension = 'doc' AND Path = '\\Test\\Start\\'")
For Each objFile in colFiles
'this isn't worked out yet...
wscript.echo objfile.name
strCopy = "C:\test\" & objFile.FileName _
& "." & objFile.Extension
objFile.Copy(strCopy)
Next

and by using FSO:

Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CopyFolder "c:\Test\Start\*.doc\", "c:\Test\Finish"


I want to use WMI but I can't get it to go into the subdirectories AND I'm not sure how to recreate the directory structure at the destination. FSO gives me the structure, but without WMI Query, I can't look for only the file types I want...

Is this making any sense??? HEEEEELLLLLLPPP!
 
Why can't you use FSO to create the folder structure and copy the files?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hello tumblor,

What you need is to build a directory tree with the base c:\tree\start or whatever. Based on the directory tree, you can use the same query for extension one-by-obe walking the directory tree. Whatever you want to do with the file objects follows suit.

Here is how you can build the directory tree. The result is materialized in the array afolder().
Code:
dim afolder()    'must declared dynamic
sroot="c:\test\start"    'in .name unescaped string format
redim afolder(0) 'set (0) to sroot
afolder(0)=sroot
set svc=getobject("winmgmts:root\cimv2")
[blue]build_foldertree sroot,svc,afolder[/blue]
set svc=nothing

wscript.echo ubound(afolder)
wscript.echo join(afolder, vbcrlf)

sub build_foldertree(sroot, wmisvc, afolder)
    'here you can device sanity test (spared here)
    '[1] afolder is dynamic array
    '[2] afolder is nonempty, at least containing (0) equal to sroot if exists only one component
    '[3] sroot is in the unescaped string format

    sQuery="Associators of {win32_directory.name='" & sroot & "'} " & _
        "where AssocClass=win32_subdirectory " & _
        "ResultRole=PartComponent"

    set cfolder=wmisvc.execquery(sQuery)
    for each ofolder in cfolder	
    i=ubound(afolder)+1
    redim preserve afolder(i)
    afolder(i)=ofolder.name    'consistently in unescaped string format
    next

    for each ofolder in cfolder
        build_foldertree ofolder.name, wmisvc, afolder
    next
end sub
regards - tsuji

ps. It is sometimes not enough appreciated what fso does on its own with effectiveness for you and how wmi is inefficient for doing simple thing on hugh monster like cim_datafile. So the idea behind what TomThumbKP's advice is good. But, the subtask in question is reasonably efficient and extendable.
 
Thanks Tsuji, that's some good code, I ran it and got a listing of all folders and subfolders in c:\test\start, so it's working.

One question if I may, how or where would you work in a selective file copy? For example, where would I place my statements to copy all *.doc and *.xls files to the destination. I'm playing around with the code right now, but any nudges in the right direction would be appreciated.
 
tumblor,

You can use the existing script to get the file object then copy.
Code:
for i=0 to ubound(afolder)
    sdrv=split(afolder(i),":")(0)
    spath=split(afolder(i),":")(1)
    sdrv=sdrv&":"
    spath=replace(spath,"\","\\") & "\\"

    '[1] for *.doc files; same construction for *.xls
    sQuery="select name from cim_datafile where drive='" & sdrv & "' and path='" & spath & "' and extension = 'doc'"
    set cfile=svc.execquery(sQuery)
    for each ofile in svc.execquery(sQuery)
        'if target path-chain is broken, it fails with iret<>0
        stgtname=replace(ofilename,"Start","Finish")
        iret=ofile.copy(stgtname)
    next
next
The stgtname definition can be more complicated to reflect user's need. But you get the idea. The thing you have to make sure is the target path-chain exists else create it by whatever means.

But after you get afolder() which is a reasonably good thing to deal with, you can stay with fso which is less massive. wmi's arsenal can be used more readily if you really need to deal with the remote machine.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top