Solved

Handle error on MoveFile

Posted on 2006-07-11
13
522 Views
Last Modified: 2008-01-09
I am trying to move files that are in a directory that are more then 10 min old to another directory. I have the code below but from time to time we are getting files with the same name in the source dir and destination dir and then throws an error that the files exsits. I just need to overright the one in the destination what do I need to add to do this? Also I would like to only move PDF files.

Thanks

Rick

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile in oFolder.Files
If DateDiff("n", oFile.DateLastModified, Now()) > 10 Then
oFSO.MoveFile oFile, sDestin
End If
Next
0
Comment
Question by:redbaran28
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 8

Expert Comment

by:Shakti109
ID: 17085106


Simply change one line of your script :

Change

oFSO.MoveFile oFile, sDestin


TO

oFso.copyfile oFile, sDestin, True

Copyfile accepts a boolean as the last argument that tells it to overwrite or not.
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17085158

Oops, forgot to include the "filter" for .pdf's. :

Modify the "for-each" routine as follows :


For Each oFile in oFolder.Files
   If DateDiff("n", oFile.DateLastModified, Now()) > 10 Then
       if instr(ofile.name, ".pdf") then
           oFso.copyfile oFile, sDestin, True
       end if
  End If
Next
0
 
LVL 2

Expert Comment

by:kcchenkd
ID: 17085162
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile in oFolder.Files
if GetExtension(oFile)="pdf" Then
   If DateDiff("n", oFile.DateLastModified, Now()) > 10 Then
       oFso.delete oFile
       oFso.copyfile oFile, sDestin, True
end if
End If
next

Public Function GetExtension(ByVal i_sFilePath As String) As String
   
    Dim i As Long
   
    On Error GoTo FAILED
   
    i = RevInstr(i_sFilePath, ".")
    If i = 0 Then GoTo FAILED
   
    GetExtension = Mid(i_sFilePath, i + 1)
   
    Exit Function

FAILED:

    GetExtension = ""

End Function


so it only copy file with pdf extension
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 44

Expert Comment

by:bruintje
ID: 17085168
Hi redbaran28,
----------

try something like


Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile in oFolder.Files
  If (DateDiff("n", oFile.DateLastModified, Now()) > 10) and (UCase(Right(OFile, 3)) = "PDF") Then
    if fso.FileExists(sDestin & oFile) the.pdfn
      oFSO.DeleteFile sDestin & oFile
    end if
    oFSO.MoveFile oFile, sDestin
  End If
Next

----------
bruintje
share what you know, learn what you don't
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17085179
saw all those posts to late, and there is a typo in my comment
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile in oFolder.Files
  If (DateDiff("n", oFile.DateLastModified, Now()) > 10) and (UCase(Right(OFile, 3)) = "PDF") Then
    if fso.FileExists(sDestin & oFile) then oFSO.DeleteFile sDestin & oFile
    oFSO.MoveFile oFile, sDestin
  End If
Next
0
 

Author Comment

by:redbaran28
ID: 17085180
I need to move the file out of the source dir and overright in the destination dir if it already exsist. What about only moving PDF files?
0
 

Author Comment

by:redbaran28
ID: 17085357
Thanks for all the quick responses.

kcchenkd - I get a error at line 18 char 48, expected ')'

bruintje - I get an error at line 11 char 5, object required: 'fso'.  
I changed
if fso.FileExists(sDestin & oFile) then oFSO.DeleteFile sDestin & oFile

to

if ofso.FileExists(sDestin & oFile) then oFSO.DeleteFile sDestin & oFile

but then go the error that the file exsists

Shakti109 - I need to move the files.

Thanks again

Rick
0
 
LVL 2

Expert Comment

by:kcchenkd
ID: 17085403
try it agian

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile in oFolder.Files
if  lcase(GetExtension(oFile))="pdf" Then
   If DateDiff("n", oFile.DateLastModified, Now()) > 10) Then
       oFso.copyfile oFile, sDestin, True
     oFso.delete oFile
end if
End If
next

Public Function GetExtension(ByVal i_sFilePath As String) As String
   
    Dim i As Long
   
    On Error GoTo FAILED
   
    i = RevInstr(i_sFilePath, ".")
    If i = 0 Then GoTo FAILED
   
    GetExtension = Mid(i_sFilePath, i + 1)
   
    Exit Function

FAILED:

    GetExtension = ""

End Function
0
 

Author Comment

by:redbaran28
ID: 17085450
kcchenkd - I get an error at line 11 char 56, expected 'Then'
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17085527
my fault the delete needs a true for force deletion

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile In oFolder.Files
  If (DateDiff("n", oFile.DateLastModified, Now()) > 10) And (UCase(Right(oFile, 3)) = "PDF") Then
    If oFSO.FileExists(sDestin & oFile) Then oFSO.DeleteFile sDestin & oFile, True
    oFSO.MoveFile oFile, sDestin
  End If
Next
0
 

Author Comment

by:redbaran28
ID: 17085630
I still seem to be getting file already exists at line 12, char 5
0
 
LVL 44

Accepted Solution

by:
bruintje earned 500 total points
ID: 17085688
i practically tested this

Dim oFSO
Dim oFile
Set oFSO = CreateObject("Scripting.FileSystemObject")

      
Comment from Shakti109
Date: 07/11/2006 10:16PM CEST
ID: 17085106       Comment       



Simply change one line of your script :

Change

oFSO.MoveFile oFile, sDestin


TO

oFso.copyfile oFile, sDestin, True

Copyfile accepts a boolean as the last argument that tells it to overwrite or not.

Comment from Shakti109
Date: 07/11/2006 10:21PM CEST
ID: 17085158       Comment       


Oops, forgot to include the "filter" for .pdf's. :

Modify the "for-each" routine as follows :


For Each oFile in oFolder.Files
   If DateDiff("n", oFile.DateLastModified, Now()) > 10 Then
       if instr(ofile.name, ".pdf") then
           oFso.copyfile oFile, sDestin, True
       end if
  End If
Next

Comment from kcchenkd
Date: 07/11/2006 10:21PM CEST
ID: 17085162       Comment       

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile in oFolder.Files
if GetExtension(oFile)="pdf" Then
   If DateDiff("n", oFile.DateLastModified, Now()) > 10 Then
       oFso.delete oFile
       oFso.copyfile oFile, sDestin, True
end if
End If
next

Public Function GetExtension(ByVal i_sFilePath As String) As String
   
    Dim i As Long
   
    On Error GoTo FAILED
   
    i = RevInstr(i_sFilePath, ".")
    If i = 0 Then GoTo FAILED
   
    GetExtension = Mid(i_sFilePath, i + 1)
   
    Exit Function

FAILED:

    GetExtension = ""

End Function


so it only copy file with pdf extension

Comment from bruintje
Date: 07/11/2006 10:22PM CEST
ID: 17085168       Your Comment       

Hi redbaran28,
----------

try something like


Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile in oFolder.Files
  If (DateDiff("n", oFile.DateLastModified, Now()) > 10) and (UCase(Right(OFile, 3)) = "PDF") Then
    if fso.FileExists(sDestin & oFile) the.pdfn
      oFSO.DeleteFile sDestin & oFile
    end if
    oFSO.MoveFile oFile, sDestin
  End If
Next

----------
bruintje
share what you know, learn what you don't

Comment from bruintje
Date: 07/11/2006 10:23PM CEST
ID: 17085179       Your Comment       

saw all those posts to late, and there is a typo in my comment
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile in oFolder.Files
  If (DateDiff("n", oFile.DateLastModified, Now()) > 10) and (UCase(Right(OFile, 3)) = "PDF") Then
    if fso.FileExists(sDestin & oFile) then oFSO.DeleteFile sDestin & oFile
    oFSO.MoveFile oFile, sDestin
  End If
Next

Comment from redbaran28
Date: 07/11/2006 10:23PM CEST
ID: 17085180       Author Comment       

I need to move the file out of the source dir and overright in the destination dir if it already exsist. What about only moving PDF files?

Comment from redbaran28
Date: 07/11/2006 10:45PM CEST
ID: 17085357       Author Comment       

Thanks for all the quick responses.

kcchenkd - I get a error at line 18 char 48, expected ')'

bruintje - I get an error at line 11 char 5, object required: 'fso'.  
I changed
if fso.FileExists(sDestin & oFile) then oFSO.DeleteFile sDestin & oFile

to

if ofso.FileExists(sDestin & oFile) then oFSO.DeleteFile sDestin & oFile

but then go the error that the file exsists

Shakti109 - I need to move the files.

Thanks again

Rick

Comment from kcchenkd
Date: 07/11/2006 10:50PM CEST
ID: 17085403       Comment       

try it agian

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile in oFolder.Files
if  lcase(GetExtension(oFile))="pdf" Then
   If DateDiff("n", oFile.DateLastModified, Now()) > 10) Then
       oFso.copyfile oFile, sDestin, True
     oFso.delete oFile
end if
End If
next

Public Function GetExtension(ByVal i_sFilePath As String) As String
   
    Dim i As Long
   
    On Error GoTo FAILED
   
    i = RevInstr(i_sFilePath, ".")
    If i = 0 Then GoTo FAILED
   
    GetExtension = Mid(i_sFilePath, i + 1)
   
    Exit Function

FAILED:

    GetExtension = ""

End Function

Comment from redbaran28
Date: 07/11/2006 10:57PM CEST
ID: 17085450       Author Comment       

kcchenkd - I get an error at line 11 char 56, expected 'Then'

Comment from bruintje
Date: 07/11/2006 11:06PM CEST
ID: 17085527       Your Comment       

my fault the delete needs a true for force deletion

Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")

sSource = "d:\testorg\"
sDestin = "d:\testnew\"

Set oFolder = oFSO.GetFolder(sSource)

For Each oFile In oFolder.Files
  If (DateDiff("n", oFile.DateLastModified, Now()) > 10) And (UCase(Right(oFile, 3)) = "PDF") Then
    If oFSO.FileExists(sDestin & oFile.Name) Then oFSO.DeleteFile sDestin & oFile.Name, True
    oFSO.MoveFile oFile, sDestin & oFile.Name
  End If
Next
0
 

Author Comment

by:redbaran28
ID: 17085748
We have a winner, thanks bruintje
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tidtcpserver connection lost handle 2 83
how to update exe applicatio from internet ? 6 77
Advice in Xamarin 21 79
What are programming codes and how do they work? 7 93
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question