VBScript copying files from subfolders

General Macro Scheduler discussion

Moderators: JRL, Dorian (MJT support)

Post Reply
BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

VBScript copying files from subfolders

Post by BlackWisdom » Tue Dec 14, 2004 11:28 pm

:?:
Hi, I have reviewed several topics and replies but cant seem to locate this scenario, if you could point me to a previous topic that would be great. Im trying to get my script to copy files from the PC with a specific extension the first part works (deleting and recreating the folder) the second part does not - I get a permission denied error on Line 6 - and that where it stops, any ideas??

VBSTART
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FolderExists("C:\Temp") Then
filesys.DeleteFolder "C:Temp"
End If
Set CreateNewFolder = filesys.CreateFolder("C:\Temp")
If filesys.FileExists ("c:\*.doc") Then
filesys.CopyFile "c:\*.doc", "C:\Temp"
End If
VBEND
VBRun>

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Dec 15, 2004 12:46 am

If filesys.FolderExists("C:\Temp") Then
filesys.DeleteFolder "C:Temp"
You are missing the "\" in the folder you are trying to delete.

Change from "C:Temp" to "C:\Temp"
=============================
Can probably do all of this with MacroScheduler commands and eliminate VB completely.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

Thanks Bob..

Post by BlackWisdom » Wed Dec 15, 2004 12:55 am

I have been looking at the copy command it does not seem to have the capability to scan subfolder during copy, Is there a switch Im not seeing??

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Wed Dec 15, 2004 4:30 am

Other than the missing \ in c:temp, the help on "CopyFile" says this:
If source contains wildcard characters or destination ends with a path separator (\), it is assumed that destination is an existing folder in which to copy matching files. Otherwise, destination is assumed to be the name of a file to create.
In your posted example, your destination path ("C:\Temp") does not end in a \, and thus it may be trying to create a file with that name... which of course already exists.

If you're still having problems, you may wish to attempt to handle errors regarding read only, file not found, etc... the On Error, and err help in the vbscript is handy. For example, add this below the "VBSCRIPT" line:
On Error Resume Next

In the code itself, you can check for errors, and obtain the err number, and handle it accordingly.

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Dec 15, 2004 6:07 am

If filesys.FileExists ("c:\*.doc") Then
filesys.CopyFile "c:\*.doc", "C:\Temp"
Aha, I see you are doing a Copy through the whole tree?

If the VB suggestions don't work for you, then you could do a
"Run Program>....... DIR C:\*.doc /s >> doclist.txt"
then ReadLn> that file, doclist.txt, to get the source file names for the CopyFile command.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

Thanks Guys - that did it...

Post by BlackWisdom » Thu Dec 16, 2004 1:11 am

:D

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Dec 16, 2004 2:42 am

So what was your final solution?
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

Hi Bob..

Post by BlackWisdom » Sun Dec 19, 2004 9:37 pm

I have been working with this VBScript Its supposed to copy only the files in a folder that has a date difference of 1 - basically attempting to copy all the data from the past day. Currently It copies all the files from the previous day. I have attempted several variations including adding the time to the DateDiff the results are the same.


Dim filesys, demofolder, fil, filecoll, filist
Set filesys = CreateObject("Scripting.FileSystemObject")
Set demofolder = filesys.GetFolder("C:\test3")
Set filecoll = demofolder.Files

For Each fil in filecoll
if DateDiff("d",fil.DateCreated,Date)=< 1 then
fil.copy("C:\test3\ReadyBack\")
end if
Next

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Re: Hi Bob..

Post by support » Sun Dec 19, 2004 9:57 pm

BlackWisdom wrote:attempting to copy all the data from the past day. Currently It copies all the files from the previous day.
Is that not the same thing? What is the problem? Please explain.
MJT Net Support
[email protected]

BlackWisdom
Pro Scripter
Posts: 58
Joined: Thu Oct 16, 2003 12:53 am

Soory bob..

Post by BlackWisdom » Sun Dec 19, 2004 10:29 pm

I menat it copies all the files from the other folder instead of just the files from the previous day..

User avatar
Captive
Macro Veteran
Posts: 213
Joined: Sun Oct 20, 2002 8:37 pm
Location: Colorado, USA

Post by Captive » Mon Dec 20, 2004 7:35 am

Change " =< 1" to " = 1".

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts