• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

Handle error on MoveFile

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
redbaran28
Asked:
redbaran28
  • 5
  • 4
  • 2
  • +1
1 Solution
 
Shakti109Commented:


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
 
Shakti109Commented:

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
 
kcchenkdCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
bruintjeCommented:
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
 
bruintjeCommented:
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
 
redbaran28Author Commented:
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
 
redbaran28Author Commented:
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
 
kcchenkdCommented:
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
 
redbaran28Author Commented:
kcchenkd - I get an error at line 11 char 56, expected 'Then'
0
 
bruintjeCommented:
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
 
redbaran28Author Commented:
I still seem to be getting file already exists at line 12, char 5
0
 
bruintjeCommented:
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
 
redbaran28Author Commented:
We have a winner, thanks bruintje
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now