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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mykeymouseAuthor Commented:
Vinny, works like a dream! Many thanks for the assist!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.