Click here to Skip to main content
15,915,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make Renamed copy in destination Folder...

Target folder has subfolders and in subfolders has files with the same named in destination Folders.

File1.Txt
File2.jpg
file3.docx
file4.exe
file5.pdf
...

and while copying Files I want to rename files put with specific carecter or letter or any kind...

file1_a.txt
File2_a.jpg
file3_a.docx
file4_a.exe
file5_a.pdf

how can I do this ?

Advanced thanks.

What I have tried:

VB
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FolderBrowserDialog1.SelectedPath = ""

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = ""
        If RadioButton1.Checked = True Then
            If vbOK = FolderBrowserDialog1.ShowDialog Then
                TextBox1.Text = FolderBrowserDialog1.SelectedPath
            End If
        Else
            OpenFileDialog1.ShowDialog()
            TextBox1.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Application.Exit()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        TextBox2.Text = ""
        'If RadioButton1.Checked = True Then
        If vbOK = FolderBrowserDialog1.ShowDialog Then
            TextBox2.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim location As String
        Dim locationsave As String
        location = TextBox1.Text
        locationsave = TextBox2.Text

        Dim parts As String() = location.Split(New Char() {"\"c}) 'Dzieli plik 
        Dim filename As String = parts(parts.Count - 1) 'pobiera z podzielonego pliku tylko nazwę pliku który kopiujemy

        If RadioButton1.Checked = True Then
            My.Computer.FileSystem.CopyDirectory(location, locationsave, True)

        Else
            My.Computer.FileSystem.CopyFile(location, locationsave, True)
            Label3.Text = "Compleated" & locationsave
        End If

    End Sub
End Class
Posted
Updated 18-Nov-18 20:55pm

1 solution

Use File.Exists to check if the file is already there. If it isn't, you're good to go.
If it is, then use Directory.GetFiles to find all the "used suffix" file names:
Dim files As String() = Directory.GetFiles(destPath, filenameWithoutExtension & "_*")
You can then find the "latest" and generate your new name.

I'd strongly suggest that you use numbers instead of letters: what are you going to do when you find "file1_z" already exists? Using numbers instead:
file1.txt
file1.00001.txt
file1.00002.txt
...
Is a lot simpler to work with in the long run.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900