Renaming Group of Files Using Loop in Visual Basic

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-24 21:10:09

问题


I currently have these group of files I want to rename:

C:\Users\tmedina\Documents\testenviroment\Testfolder\file1-1111.doc
C:\Users\tmedina\Documents\testenviroment\Testfolder\file2-1111.doc
C:\Users\tmedina\Documents\testenviroment\Testfolder\file3-1111.doc
...
C:\Users\tmedina\Documents\testenviroment\Testfolder\file20-1111.doc

I have a text box on my form where a I would enter a string of text which will replace the '1111' to whatever the string is in the text box field.

So for example, On my app, in the text box field, I would enter 2222, then when I click on button1, it will rename file1-1111.doc to file1-2222.doc, file2-1111.doc to file2-2222.doc, etc.....

This is my VB code I currently have:

Dim base As String = "C:\Users\tmedina\Documents\testenviroment\"
Dim newDir As String = base + CStr(TextBox1.Text)
Directory.CreateDirectory(newDir)
Directory.SetCurrentDirectory(newDir)
For Each nameChangeFiles As String In My.Computer.FileSystem.GetFiles(newDir, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*1111*")
            My.Computer.FileSystem.RenameFile(nameChangeFiles, "2222")

But this only creates a blank file (with no extension) named 2222.

Any help will be appreciated.


回答1:


You could try with this.

For Each nameChangeFiles As String In My.Computer.FileSystem.GetFiles(newDir, SearchOption.SearchAllSubDirectories, "*1111*") 
    Dim newName As String = nameChangeFiles.Replace("1111", "2222")
    My.Computer.FileSystem.RenameFile(nameChangeFiles, newName) 
Next

But this changes always the part "1111" with "2222". You have said that you input in a textbox the new string to replace the "1111", but in your code above the only textbox present is used to create a subdirectory.
Could you explain where do you get the replacement text?




回答2:


Try this:

Dim base As String = "C:\Users\tmedina\Documents\testenviroment\"
Dim newDir As String = base + CStr(TextBox1.Text)

Directory.CreateDirectory(newDir)
Directory.SetCurrentDirectory(newDir)

For Each nameChangeFiles As String In My.Computer.FileSystem.GetFiles(newDir, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*1111*")

    Dim fi As FileInfo
    fi = New FileInfo(nameChangeFiles)

    Dim newFilename As String = Path.Combine(newDir, fi.Name.Replace("1111", "2222"))
    My.Computer.FileSystem.RenameFile(nameChangeFiles, newFilename)

Next



回答3:


Use String.Replace() function:

Dim base As String = "C:\Users\tmedina\Documents\testenviroment\"
Dim newDir As String = base + CStr(TextBox1.Text)
Directory.CreateDirectory(newDir)
Directory.SetCurrentDirectory(newDir)
String ToReplace = "1111"

For Each nameChangeFiles As String In My.Computer.FileSystem.GetFiles(newDir, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*" & ToReplace & "*")
            My.Computer.FileSystem.RenameFile(nameChangeFiles.Replace(ToReplace, "2222"))



回答4:


RenameFile doesn't know which part of the file you want to rename; it only does the whole thing. Anyway, here's how I'd write it:

Dim newDir As String = Path.Combine(base, TextBox1.Text)

For Each f As FileInfo In New DirectoryInfo(base).GetFiles("*.doc", SearchOption.AllDirectories)
    f.CopyTo(Path.Combine(newDir, Regex.Replace(f.Name, "\d+\.doc$", "2222.doc")))
Next


来源:https://stackoverflow.com/questions/11197619/renaming-group-of-files-using-loop-in-visual-basic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!