Solved

Handle error on MoveFile

Posted on 2006-07-11
13
519 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 50
firstChar challenge 13 86
wordmultiple challenge 12 93
Counting documents in a Domino View 3 36
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now