File rename

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newyorker213
    New Member
    • Feb 2008
    • 1

    File rename

    Hello,

    We have files ( named in the following format ) in one of our folders:

    MV0001.XLS
    MV0002.XLS
    and so on

    WV0001.XLS
    WV0002.XLS
    and so son

    CV0001.XLS
    CV0002.XLS
    and so on

    We need a script that will rename the files in the following way:

    if filename starts with 'M', rename file to " XXXXX_SERVER1_Y YYY_MM"
    ( where 'XXXXX' are the 2nd - 6th characters of the original file name e.g.V0001,
    V0002 etc,
    'AA' are constant texts,
    'YYYY' is current year,
    'MM' is previous month )

    similarly, if filename starts with 'W', rename files to "XXXXX_SERVER2_ YYYY_MM" , and if it starts with
    'C', rename to "XXXXX_SERVER3_ YYYY_MM".

    Any help will be greatly appreciated.
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Kindly post what you have tried so far.

    Comment

    • jamesd0142
      Contributor
      • Sep 2007
      • 471

      #3
      I tried something similar: take a look and see if it gives you any idea where to start...

      [code=vbnet]
      Private Sub RenameFiles(ByV al StartPath As String)
      Dim myfolder As DirectoryInfo = New DirectoryInfo(S tartPath)
      Dim mySubfolders() As FileInfo = myfolder.GetFil es()
      Dim strFiles() As FileInfo = myfolder.GetFil es()

      For Each myItem As FileInfo In strFiles
      'myItem.Delete( )
      'open File
      Dim a As String = StartPath & "\" & myItem.ToString

      'read File
      Dim EntireLine1 As String
      Dim oFile1 As System.IO.File
      Dim oRead1 As System.IO.Strea mReader
      If System.IO.File. Exists(a) = True Then
      oRead1 = oFile1.OpenText (a)
      EntireLine1 = oRead1.ReadToEn d
      oRead1.Close() 'test line
      End If
      Dim b As String = "2008" 'vbCrLf
      'find date
      date1 = EntireLine1.Sub string(EntireLi ne1.IndexOf("DA TE"), EntireLine1.Ind exOf(b)) ', EntireLine1.Ind exOf("2008"))

      'function cuts of the end of the line where it finds a return character or line feed...
      For i = 1 To Len(date1)
      j = Mid(date1, i, 1)
      If a = vbCr Or a = vbLf Then
      Exit For
      End If
      Next
      date1 = Mid(date1, 6, i - 7)

      'rename File
      If System.IO.File. Exists(a) = True Then
      FileSystem.Rena me(a, StartPath & "\" & RTrim(date1) & ".txt")
      'System.IO.File .Move(a, StartPath & "\" & RTrim(date1) & ".txt")
      'either works^^^^^^
      End If

      Next

      End Sub
      [/code]
      Last edited by Killer42; Feb 21 '08, 09:52 PM. Reason: Corrected "simular"

      Comment

      Working...