Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vbscript to copy files from one directory to another

8,443 views
Skip to first unread message

Pete

unread,
Sep 1, 2011, 2:29:12 PM9/1/11
to
I have a folder with several pdf and txt files in it. the files are
named for example 1.pdf , 1.txt, 2.pdf, 2.txt etc... what i need is a
vbscript that will go to the folder and copy the first 2 files with
identical name but different extension and move them to another
folder. then i need the script to stop for 1 min and continue to the
next set of files ans so on.
Help!!!.
thanks,
Pete

Todd Vargo

unread,
Sep 1, 2011, 5:25:51 PM9/1/11
to

Will all files in this folder be a .pdf with matching .txt file? How many
files are we looking at? Are there other file extensions that need checked?
In the same sentence you say "copy the first 2 files" but then say "move
them to another folder". So do you want to copy or move the matching files
to another folder?

What code have you written so far?

Dave "Crash" Dummy

unread,
Sep 1, 2011, 6:45:36 PM9/1/11
to

Based on your vague description, this will do what you ask. This version
actually moves the files. If you just want to copy them, you will have
to modify the instruction accordingly.

=====================mover.vbs======================
Set fso=CreateObject("Scripting.FileSystemObject")
source="d:\path1\"
target="d:\path2\"

Set fldr=fso.getFolder(source)
for each file in fldr.files
if right(lcase(file.name),4)=".pdf" then
call mover(lcase(file.path))
end if
next

sub mover(f1)
f2=replace(f1,"pdf","txt")
if fso.fileExists(f2) then
fso.MoveFile f1,target
fso.MoveFile f2,target
wscript.sleep 60000
end if
end sub
==================================================
--
Crash

"Facts are stubborn things, but statistics are more pliable."
~ Laurence J. Peter ~

Pete

unread,
Sep 1, 2011, 6:48:32 PM9/1/11
to

yes. they are all pdf with matching txt file. i have over 1000 files.
no other extensions. sorry i meant move. so far i have a vbscript that
moves the files but the problem is that i need to be able to pick up
the files that match one pair at a time and then stop the process for
1 - 2minutes and then continue and so on. Thanks for your quick
response.

greg...@itecgroup.co.za

unread,
Nov 27, 2012, 3:39:21 AM11/27/12
to
Good day all,

I have the same requirements as specified above (except for the delay - I just need the two files alone to be moved), but when I run the script above, I get the File Already Exists error.

Any ideas of what is causing this?

Thanking you in advance!
Regards,

Mayayana

unread,
Nov 27, 2012, 8:54:53 AM11/27/12
to
Whatever "above" you're referring to is nowhere
to be seen in my newsreader. I'm guessing that you
probably responded to a years-dead discussion, not
realizing that it wasn't current.

Please consider getting a real news reader. Google
Groups is little more than an online archive of Usenet
posts. Web forum format is not suitable for newsgroups.
It creates problems like this -- people answering posts
from years past. It also makes discussion difficult. People
end up re-posting entire threads in each post because
they simply can't see the discussion unfold.

Most email program have newsgroup functionality.
With that you can see current posts and take part
easily. ... Then perhaps you could tell us what your
actual question is.

greg...@itecgroup.co.za

unread,
Nov 27, 2012, 9:00:24 AM11/27/12
to
Sure, thank you for your response and sorry for the confusion.

What I need to do is create a script to do the following:

1) Check if a Destination folder is empty, if so...
2) Select the oldest created PDF file in the Source folder, find the same filename with .dat as the extension in the same source folder and then move the two files to the destination folder.
3) If the Destination folder has files, do nothing.

Please may you assist me in creating this script?

Thank you for your support,
Regards,

Mayayana

unread,
Nov 27, 2012, 9:43:05 AM11/27/12
to

| 1) Check if a Destination folder is empty, if so...
| 2) Select the oldest created PDF file in the Source folder, find the same
filename with .dat as the extension in the same source folder and then move
the two files to the destination folder.
| 3) If the Destination folder has files, do nothing.
|

You're probably going to want the scripting docs:

http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9

The central object for file system access is FileSystemObject.
The methods of that do what you want. Below is an "air code"
version of the basics of what you want. You can fill in the
details.
FSO presents the file system as a set of objects and collections.
The code here is a rough template for many file system operations.

Dim FSO, OFols, OFils, OFol, OFil, sFilePath
Set FSO = CreateObject("Scripting.FileSystemObject")

Set OFol = FSO.GetFolder(path)
Set OFils = OFol.Files
If OFils.Count > 0 Then
For Each OFil in OFils
sFilePath = OFil.Path
If UCase(Right(sFilePath, 3)) = "PDF" Then
'-- here a date comparison will need to be
'-- made in order to get oldest PDF.
'-- Then a FileExists check will be needed to ensure
'-- the DAT file exists. You can then use
'-- FSO methods MoveFile, CopyFile, DeleteFile
'-- to do the rest. See the WSH help for details.
End If
Next
End If
Set OFils = Nothing
Set OFol = Nothing
Set FSO = Nothing


greg...@itecgroup.co.za

unread,
Nov 27, 2012, 10:03:32 AM11/27/12
to
Good day Mayayana,

Thank you for your response!

The information you provided is very helpful.

What can I add to the script to ensure that only two files are moved and not all the files in the folder.

Every time the script executes, I need to only move two files.

Thank you,
Regards,

R.Wieser

unread,
Nov 27, 2012, 10:42:49 AM11/27/12
to
Mayayana.

> If UCase(Right(sFilePath, 3)) = "PDF" Then

Quite dangerous. An extension as well as the (without an extension)
filename could end with that letter-combination.

Why not use VBScripts own "GetExtensionName" ?

Regards,
Rudy Wieser


-- Origional message
Mayayana <maya...@invalid.nospam> schreef in berichtnieuws
k92jg5$jqn$1...@dont-email.me...

Mayayana

unread,
Nov 27, 2012, 11:59:43 AM11/27/12
to
|
| What can I add to the script to ensure that only two files are moved and
not all the files in the folder.
|
| Every time the script executes, I need to only move two files.
|

You need to move the oldest PDF, right? So you
first check to see if there are files, then you go
through finding PDFs and check their dates. See
the help file for DateLastModified, DateCreated, etc.

OFil.DateLastModified returns something like this:

"11/28/2012 6:31:34 PM"

You can compare dates with > and <

So all you have to do is to check the date on each
PDF and compare it to the last:

' if this is the first file or current file is older
' than last file, save current path and date.

if len(p1) = 0 or oFil.DateLastModified < d1 then
p1 = oFil.Path
d1 = oFil.DateLastModified
end if

By the end of the loop you either have
p1 = "" ' no PDFs
or you have
p1 = "C:\folder1\somefile.pdf" 'path of oldest PDF



Mayayana

unread,
Nov 27, 2012, 12:12:45 PM11/27/12
to
| > If UCase(Right(sFilePath, 3)) = "PDF" Then
|
| Quite dangerous. An extension as well as the (without an extension)
| filename could end with that letter-combination.
|
| Why not use VBScripts own "GetExtensionName" ?

It could check for ".PDF" to be more dependable. I
was just writing quickie code, assuming that no one
is naming files with no extension that end in "pdf".
(If someone is doing that then I expect they won't
be much interested in organizing their files. :)

If you test GetExtensionName you'll see that it has no
intelligence. For this path:

"C:\windows\desktop\not.es.Four Score And Seven Years Ago"

it returns:

"Four Score And Seven Years Ago"

In other words, it's no different from:

x = instrrev(path, ".")
ext = Right(path, (len(path) - x))

Many of the FSO methods are like that. They're
designed for people who know nothing of operators
and methods of the language, so that network
admins can manage their network without really
needing to know script.

I actually researched that (useless FSO methods)
some years ago, out of curiosity. I posted the results
here at the time, but it's long gone now.


Dave "Crash" Dummy

unread,
Nov 27, 2012, 12:38:09 PM11/27/12
to
greg...@itecgroup.co.za wrote:

<snipped>

> What I need to do is create a script to do the following:
>
> 1) Check if a Destination folder is empty, if so... 2) Select the
> oldest created PDF file in the Source folder, find the same filename
> with .dat as the extension in the same source folder and then move
> the two files to the destination folder. 3) If the Destination folder
> has files, do nothing.
>
> Please may you assist me in creating this script?

This should get you started. You will have to decide if you want to use
DateCreated or DateLastModified.
'==================================================
set fso=CreateObject("Scripting.FileSystemObject")
set source=fso.getFolder("d:\source")
set target=fso.getFolder("d:\target")
date1=date

if target.files.count=0 then
for each file in source.files
if file.type="PDF File" then
if dateDiff("s",date1,file.DateLastModified)<0 then
date1=file.DateLastModified
base=fso.getBaseName(file.name)
end if
end if
next
fso.copyFile "d:\source\" & base & ".*","d:\target"
end if

'==================================================
--
Crash

Today is the first day of the rest of your life,
and there's not a damned thing you can do about it.

Dave "Crash" Dummy

unread,
Nov 27, 2012, 3:08:32 PM11/27/12
to
Mayayana wrote:
> |
> | What can I add to the script to ensure that only two files are moved and
> not all the files in the folder.
> |
> | Every time the script executes, I need to only move two files.
> |
>
> You need to move the oldest PDF, right? So you
> first check to see if there are files, then you go
> through finding PDFs and check their dates. See
> the help file for DateLastModified, DateCreated, etc.
>
> OFil.DateLastModified returns something like this:
>
> "11/28/2012 6:31:34 PM"
>
> You can compare dates with > and <

I didn't know you could compare dates directly like that. I used DateDiff().

> So all you have to do is to check the date on each
> PDF and compare it to the last:
>
> ' if this is the first file or current file is older
> ' than last file, save current path and date.
>
> if len(p1) = 0 or oFil.DateLastModified < d1 then
> p1 = oFil.Path
> d1 = oFil.DateLastModified
> end if
>
> By the end of the loop you either have
> p1 = "" ' no PDFs
> or you have
> p1 = "C:\folder1\somefile.pdf" 'path of oldest PDF
>
>
>

R.Wieser

unread,
Nov 27, 2012, 4:41:00 PM11/27/12
to
Mayayana.

> I was just writing quickie code

:-) The problem is that that kind of code tends to stick - something to do
with 'murphys law' - and starts to cause problems when the 'just make sure
that ...' parts are long forgotten.

> If you test GetExtensionName you'll see that it has no
> intelligence.

I do not agree with you there. It has enough intelligence to only look for
the dot only in the last part on that path (presumably the filename). A
simple instrrev will return faulty results for any path with out a dot, or
where the last dot is not in the filename.

> I actually researched that (useless FSO methods)
> some years ago, out of curiosity.

Because you mentioned that I tried it on both XP sp3 and 98se. They behave
the same.

Regards,
Rudy Wieser


-- Origional message:
Mayayana <maya...@invalid.nospam> schreef in berichtnieuws
k92s8p$e22$1...@dont-email.me...

Mayayana

unread,
Nov 27, 2012, 6:07:16 PM11/27/12
to
|
| > I was just writing quickie code
|
| :-) The problem is that that kind of code tends to stick - something to
do
| with 'murphys law' - and starts to cause problems when the 'just make sure
| that ...' parts are long forgotten.
|

I wasn't being paid to solve this problem. The intention
was only to help the OP with the basics needed to do the
job. But feel free to write a complete, robust script for him.
I don't expect he'll mind.


R.Wieser

unread,
Nov 28, 2012, 1:22:28 PM11/28/12
to
Mayayana,

> I wasn't being paid to solve this problem.

But you where eager enough to post (bad) code without thinking of the
consequences (to the OP to start with) ...

And too bad that you didn't even try to defend your "has no intelligence."
claim.

Hint: *everyone* makes mistakes. Its how you deal with it that matters. In
this case you dealt with it badly :-\

Regards,
Rudy Wieser


-- Origional message:
Mayayana <maya...@invalid.nospam> schreef in berichtnieuws
k93h1h$dgr$1...@dont-email.me...
0 new messages