Home All Groups Group Topic Archive Search About

File copy and paste script

Author
12 Feb 2009 5:11 PM
Sam
Hi,

I need to traverse a bunch of folders looking for files whose name contain
"MyPhrase" in them and copy them into a folder on say my desktop. I'd
appreciate some pointers in writing this script.

Basically, I want to tell the script what folder to start from. It will go
through all its subfolders and whenever it finds "MyPhrase" in file name, it
will copy and paste that file into a folder on my desktop.

Thanks for your help.
--
Thanks,

Sam

Author
12 Feb 2009 5:48 PM
Pegasus (MVP)
Show quote Hide quote
"Sam" <S**@discussions.microsoft.com> wrote in message
news:76989B96-44F6-4D4C-992C-B0A64653DDA7@microsoft.com...
> Hi,
>
> I need to traverse a bunch of folders looking for files whose name contain
> "MyPhrase" in them and copy them into a folder on say my desktop. I'd
> appreciate some pointers in writing this script.
>
> Basically, I want to tell the script what folder to start from. It will go
> through all its subfolders and whenever it finds "MyPhrase" in file name,
> it
> will copy and paste that file into a folder on my desktop.
>
> Thanks for your help.
> --
> Thanks,
>
> Sam

Try this script:

Set oFSO = CreateObject("Scripting.FileSystemObject")
sFolder = "d:\Some Folder"
sPhrase = "Some Phrase"
CheckFolder oFSO.GetFolder(sFolder)

'--------------------------
'Check a folder recursively
'--------------------------
Sub CheckFolder(oFolder)
For Each oFile In oFolder.Files
  If InStr(1, oFile.Name, sPhrase, 1) > 0 Then
   WScript.Echo "Copying " & oFile.path
  End If
Next

For Each oSubfolder In oFolder.Subfolders
  CheckFolder oSubfolder
Next
End Sub

I leave it to you to implement the functions that will prompt the user for a
folder name and the one that performs the actual copy action.