Script to Archive Files, needs to verify that file with same name does not exist in destination.

I do OK with shell scripts but I'm so new to VB I will claim no knowledge. Have been tasked to come up with a solution. I found the following and works well, but..... blows up if a file with the same name exists in the destination. What I need is for the script to check for the existence of same filename in destination before move, if found append a 1,2,3,4.... to the file name until can successfully move. Also would be good if it created a log file. Thanks for your help in advance!

'generic move files :
 Dim fso, f, f1, fc
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder("e:\out")
   Set fc = f.Files
   For Each f1 in fc
  If DateDiff("d", f1.DateCreated, Now) > 30 Then
    fso.MoveFile f1 , "e:\in\"
     End If
   Next







mykeymouseAsked:
Who is Participating?
 
vinnyd79Commented:
I added a log file into this example:

'generic move files :
 Dim fso, f, f1, fc ,df,cnt,ext,fn,log

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder("e:\out")
   Set fc = f.Files
  ' adjust path of log file here
   Set log = fso.OpenTextFile("e:\LogFile.txt",8,True)

   For Each f1 in fc
   cnt = 0
   If DateDiff("d", f1.DateCreated, Now) > 30 Then
     log.WriteLine(Now & "    " & "File Found : " & f1)
     fn = Mid(f1,InstrRev(f1,"\") + 1)
     df = "e:\in\" & fn

     While fso.FileExists(df)
         cnt = cnt + 1
         if Instr(fn,".") Then
              ext = Mid(f1,InstrRev(f1,"."))
              df = "e:\in\" & Left(fn,InstrRev(fn,".") -1) & cnt & ext
         else
          df = "e:\in\" & fn & cnt
         end if
     Wend
          log.WriteLine(Now & "    " & "Moving File : " & f1  & " to " & df)
            fso.MoveFile f1 , df
   end if
   Next

log.close
Set log = Nothing
Set fc = Nothing
Set f = Nothing
Set fso = Nothing
0
 
vinnyd79Commented:
Maybe something like this>


'generic move files :
 Dim fso, f, f1, fc ,df,cnt,ext,fn
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder("e:\out")
   Set fc = f.Files

   For Each f1 in fc
   cnt = 0
  If DateDiff("d", f1.DateCreated, Now) > 30 Then
       
      df = "e:\in\" & Mid(f1,InstrRev(f1,"\") + 1)

      While fso.FileExists(df)
          cnt = cnt + 1
          fn = Mid(f1,InstrRev(f1,"\") + 1)
          if Instr(fn,".") Then
                ext = Mid(f1,InstrRev(f1,"."))
                df = "e:\in\" & Left(fn,InstrRev(fn,".") -1) & cnt & ext
          else
            df = "e:\in\" & fn & cnt
          end if
      Wend
            fso.MoveFile f1 , df
   end if
   Next
Set fc = Nothing
Set f = Nothing
Set fso = Nothing
0
 
mykeymouseAuthor Commented:
Vinny, works like a dream! Many thanks for the assist!
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.

All Courses

From novice to tech pro — start learning today.