Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
VB
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

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
Posted
Updated 22-Dec-15 23:02pm
v2
Comments
phil.o 23-Dec-15 5:04am    
Why are you iterating over the directory twice? You don't need that.
Also, did you try to debug (F5)?
Pete_123 23-Dec-15 5:52am    
Hi Phil Yes I see what you mean when the code is executed it fires this error:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

Additional information: Could not find file 'C:\Users\DaBeast\Desktop\PR\1.txt'.

dra {2.txt} System.IO.FileInfo
fileName "C:\Users\DaBeast\Desktop\PR\1.txt" String
sourcePath "C:\Users\DaBeast\Desktop\PR" String

I’m guessing in that due to there being two for loops one loop is ahead of the other causing this error.

in the folder it has already renamed 1.txt to (AF)1.txt which is correct but it then needs to rename 2.txt to (AF)2.txt
phil.o 23-Dec-15 5:54am    
Yes. Try to get rid of the second loop; the first one gives you all what you need already.
Pete_123 23-Dec-15 6:05am    
Hi Phil I’m not quite understanding what you’re saying if I remove the second loop its un able to uses the original file name and add the Initials at the beginning or am I missing a step ?
phil.o 23-Dec-15 6:07am    
You got the initial file name in the fileName variable. That's all you need. The dra variable is unnecessary, as well as the second loop.

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
VB.NET
Dim sourcePath As String = DJdr
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:
VB.NET
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
 
Share this answer
 
v2
VB
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.
 
Share this answer
 
v2
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:
VB
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
 
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