将Initials添加到文件夹中的所有文件? [英] Adding Initials to all files within a folder?

查看:98
本文介绍了将Initials添加到文件夹中的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试创建一个函数来添加一个人姓名的缩写。我正在开发的软件是允许我们的每个DJ下载彼此的播放列表和历史文件,但我们希望确保跟踪他们的来源。例如,我们有一个名为BG120的文件,它是由PR创建的,并且将被下载到AF机器,文件需要重命名为(PR)BG120.txt。下面是我一直在使用的一些代码,但我无法理解它。

Hi All, I’m trying to create a function to add the Initials of a person’s name. the software I’m developing is to allow each of our DJs to download each other playlist and history files but we want to make sure we keep track of who they are from. for example we have file named BG120 it was created by PR and is going to be downloaded to AF machine the file needs to be renamed to (PR)BG120.txt. below is some code I have been working with but I just can't get my head around it.

Dim DJdr As String = "C:\Users\DaBeast\Desktop\PR"
Dim searchPattern As String = "*.txt"
Dim sourcePath As String = DJdr
Dim di As New IO.DirectoryInfo(DJdr)
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo

For Each fileName As String In Directory.GetFiles(sourcePath, searchPattern, SearchOption.AllDirectories)'gets the files full path ie C:\Users\DaBeast\Desktop\PR BG120.txt
   For Each dra In diar1 'gets just the file name
      File.Move(Path.Combine(sourcePath, fileName), Path.Combine(sourcePath, "(AF)" & dra.ToString))
   Next
Next



我可能会以错误的方式解决这个问题,如果有人能说明这一点会很好。



一切都很好



Pete


I could be going about this the wrong way if anyone could shed some light on this that would be great.

All the best

Pete

推荐答案

phil.o已经指出了正确的方向。



你只需要从文件夹中获取文件名一次。



没有必要得到 FileInfo 对于每个文件,您可以使用路径 [ ^ ] class - 特别是那些只提供基本文件名和扩展名的方法。



phil.o has already pointed you in the right direction.

You only need to get the filenames from the folder once.

There is no need to get FileInfo for each of the files, you can use methods on the Path[^] class instead - specifically the methods that give you just the base filename and just the extension.

The line
Dim sourcePath As String = DJdr

也是多余的 - 只需使用 DJdr



虽然您可以在移动操作期间重命名文件,但我建议您使用文件系统。重命名 [ ^ ]方法。当然,如果你需要将文件移动到另一个文件夹,那么使用Move方法。



你可能还想将所有这些放入子程序中你可以为不同的DJ打电话。



最后,如果你跑两次会怎么样?以前重命名的文件将再次添加首字母 - 但您可能需要定期运行此处理任何新文件。所以添加一个检查以确定它是否已经完成。



例如:

is also redundant - just use DJdr

Although you can rename files during a move operation I would suggest just renaming the file in situ using the FileSystem.Rename[^] method. Of course, if you need to move the files into another folder then use the Move method.

You probably also want to put all of this into a sub-routine so that you can call it for the different DJs.

Finally, what happens if you run this twice? The previously renamed files will get the initials added on again - but you will probably need to run this regularly to handle any new files. So add a check to see if it's already been done.

For example:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    RenameFilesForAuthor("*.txt", "C:\Users\DaBeast\Desktop\PR", "PR")
    RenameFilesForAuthor("*.txt", "C:\Users\DaBeast\Desktop\AF", "AF")

End Sub

Private Sub RenameFilesForAuthor(searchPattern As String, folderName As String, ownerInitials As String)
    For Each fileName As String In Directory.GetFiles(folderName, searchPattern, SearchOption.AllDirectories)
        If Not fileName.Contains(String.Format("({0}", ownerInitials)) Then
            Dim newName As String = String.Format("({0}){1}{2}", _
                    ownerInitials, _
                    Path.GetFileNameWithoutExtension(fileName), _
                    Path.GetExtension(fileName))
            My.Computer.FileSystem.RenameFile(fileName, newName)
        End If
    Next
End Sub


Dim sourcePath As String = @"C:\Users\DaBeast\Desktop\PR"
Dim searchPattern As String = "*.txt"
Dim di As New IO.DirectoryInfo(sourcePath)
Dim newName As String

For Each fileName As String In di.GetFiles(searchPattern, SearchOption.AllDirectories) '' gets the files full path ie C:\Users\DaBeast\Desktop\PR BG120.txt
   newName = ChangeName(fileName)
   File.Move(fileName, newName)
Next

Private Function ChangeName(ByVal oldName As String) As String
   int index = oldName.LastIndexOf(@"\") + 1
   return oldName.Insert(index, "(AF)")
End Function



这是一段可以帮助你的代码。

调试会话是最明显的事情;它可以让你验证你的变量值。


Here's a piece of code that may help you.
A debug session would be the most obvious thing to do; it would allow you to validate your variables values.


我似乎找到了一个解决方案,感谢Phil.o的额外指导,我相信这是修复,所以这就是我所做的:

I seem to have found a solution thank you to Phil.o for the extra guidance I believe this is the fix so here’s what i have done:
Dim searchPattern As String = "*.txt"
Dim sourcePath As String = DJdr
For Each fileName As String In Directory.GetFiles(sourcePath, searchPattern, SearchOption.AllDirectories)
    Dim JustTheFileName As String = IO.Path.GetFileName(fileName)

    File.Move(Path.Combine(sourcePath, fileName), Path.Combine(sourcePath, "(AF)" & JustTheFileName))


Next


这篇关于将Initials添加到文件夹中的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆