如果目标文件夹中已存在文件,如何使用重命名复制文件? [英] How can I copy files with rename, if files already exist in destination folder ?
问题描述
我想在目标文件夹中重命名副本...
目标文件夹包含子文件夹,在子文件夹中包含目标文件夹中具有相同名称的文件。 />
File1.Txt
File2.jpg
file3.docx
file4.exe
file5.pdf
...
和复制文件时我想重命名放置特定文件照顾者或信件或任何种类...
file1_a.txt
File2_a.jpg
file3_a.docx
file4_a.exe
file5_a.pdf
我该怎么做?
高级谢谢。
我尝试过:
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:
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
推荐答案
使用File.Exists
进行检查如果文件已经存在。如果不是,那么你很高兴。
如果是,则使用Directory.GetFiles查找所有使用后缀文件名:
UseFile.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 & "_*")
然后,您可以找到最新并生成新名称。
我强烈建议您使用数字而不是字母:当您发现file1_z已经存在时,您打算做什么?改为使用数字:
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.
这篇关于如果目标文件夹中已存在文件,如何使用重命名复制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!