1 Replies - 502 Views - Last Post: 21 March 2016 - 07:23 AM Rate Topic: -----

#1 skyscraper11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 23-July 14

How to auto rename multiple files with different indexing

Posted 21 March 2016 - 06:39 AM

how do i auto rename multiple files that are being copied with different indexing? i mean the (0), (1), etc... i.e. if i have two files in Folder1 that has a.txt and b.txt, And another two same files inside the Folder2 And copy the a.txt and b.txt from Folder1 to Folder2 then the a.txt will become a(1).txt and the b.txt to b(1).txt. how do i do it in a single instance? what came to my mind is to have many Strings and Integers as many as the files, but i will be dealing thousands of files. This is what i have so far.

Dim ii as Integer = 0
Dim iii as Integer = 0

Sub Copy()
   For i = 0 To updatedFiles.Count - 1
    Dim fileName As String = Path.GetFileNameWithoutExtension(updatedFiles(i))
    Dim filePath As String = Path.GetDirectoryName(updatedFiles(i))
    Dim fileExt As String = Path.GetExtension(updatedFiles(i))
    Dim newFile As String = filePath & "\" & fileName & "(" & ii & ")" & fileExt
    Dim newFile2 As String = filePath & "\" & fileName & "(" & iii & ")" & fileExt

    If File.Exists(Path.Combine(dest, updatedFiles(i))) Then
       ii += 1
       'Copy newFile
    ElseIf File.Exists(Path.Combine(dest, newFile)) Then
       iii += 1
       'Copy newFile2
    End If
   Next
End Sub


This doesn't do it right, from the situation above, the a.txt becomes a(1).txt but the b.txt becomes b(2).txt. the result should be
a (1).txt
b (1).txt

This post has been edited by andrewsw: 21 March 2016 - 07:38 AM
Reason for edit:: Removed VB.NET from topic title, it is redundant


Is This A Good Question/Topic? 0
  • +

Replies To: How to auto rename multiple files with different indexing

#2 dday9  Icon User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 495
  • Joined: 17-April 13

Re: How to auto rename multiple files with different indexing

Posted 21 March 2016 - 07:23 AM

Take a look at the following example. Basically what it does is uses the LINQ to count the amount of files that match(using windows wildcards) the file name followed by the open parenthesis, number, and closed parenthesis:
Imports System
Imports System.Linq

Public Module Module1

    Public Sub Main()
        'Create some directories for example purposes
        IO.Directory.CreateDirectory("Folder1") : IO.Directory.CreateDirectory("Folder2")
        'Create some files for example purposes
        IO.File.CreateText(IO.Path.Combine("Folder1", "a.txt")).Dispose() : IO.File.CreateText(IO.Path.Combine("Folder1", "b.txt")).Dispose() : IO.File.CreateText(IO.Path.Combine("Folder2", "a.txt")).Dispose() : IO.File.CreateText(IO.Path.Combine("Folder2", "b.txt")).Dispose()
        'Loop through each .txt file in Folder1
        For Each file As IO.FileInfo In New IO.DirectoryInfo("Folder1").GetFiles("*.txt")
            Dim newName As String = String.Empty
            'Check to see if the file exists in Folder2
            If IO.File.Exists(IO.Path.Combine("Folder2", file.Name)) Then
                'Get just the file name without the extension
                Dim shortName As String = IO.Path.GetFileNameWithoutExtension(file.FullName)
			
                'If the file does exists then first get all the files who's name matches the file name
                Dim incrementedFileCount As Integer =(From f As IO.FileInfo In New IO.DirectoryInfo("Folder2").GetFiles(shortName & "(?).txt")).Count + 1
			
                'Now set the new name equal to the filename(incrementedFileCount).txt
                newName = shortName & "(" & incrementedFileCount.ToString() & ").txt"
            Else
                'If the file does not exists in Folder2 then set the new name equal to its old name
                newName = file.Name
            End If

            'Copy over the file
            IO.File.Copy(file.FullName, IO.Path.Combine("Folder2", newName))
        Next

        'Print out all the files in Folder2(debugging purposes)
        Console.WriteLine(String.Join(Environment.NewLine, (From f As IO.FileInfo In New IO.DirectoryInfo("Folder2").GetFiles("*.txt") Select f.FullName).ToArray))
    End Sub
End Module


Fiddle: https://dotnetfiddle.net/rSlRU2
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1