I've seen threads on this topic but it didnt shed any light on my case so here it goes
I am trying to rename a file as such;

Dim loc_path = "D:\Test"
Dim filenames() As String = Directory.GetFiles(loc_path, "*.mp3")

        For i = 0 To ListBox1.Items.Count - 1
            filepath = Path.Combine(loc_path, ListBox1.Items(i).ToString)
            filename = Path.GetFileName(filenames(i))

            Dim idtag As New id3v1Tagger.Tagger(filepath)
            temp = idtag.Title.ToString

            idtag.Title.Replace(idtag.Title, filename)

            fname = temp

            My.Computer.FileSystem.RenameFile(filepath, fname)

The part " My.Computer.FileSystem.RenameFile(filepath, fname) " throws an 'Argument was unhanded; Illegal Characters in path' error

But when i checked the filepath using break points theres no problems in the string

and i thought u guys may need the data in listbox


The problem is most likely with the contents of fname. Can you show us some samples that fail (and might as well show filepath too).

try using try catch to get the exception

heres the exception i got using try..catch;

System.ArgumentException: Illegal characters in path.
   at System.IO.Path.CheckInvalidPathChars(String path)
   at System.IO.Path.Combine(String path1, String path2)
   at Microsoft.VisualBasic.FileIO.FileSystem.GetFullPathFromNewName(String Path, String NewName, String ArgumentName)
   at Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(String file, String newName)
   at Microsoft.VisualBasic.MyServices.FileSystemProxy.RenameFile(String file, String newName)
   at WindowsApplication1.Form2.Button4_Click(Object sender, EventArgs e) in D:\PRaser\PRaser\Form2.vb:line 158

and @Momerath i dont know what you meant by failed samples but fname var hold the temp var data

here are the filpaths


the exception is thrown on this line:
My.Computer.FileSystem.RenameFile(filepath, fname)

So you better check out the values of filepath and fname.
Set a breakpoint on the renaming part and check out the exception details and its innerexception.

Nope. no luck the path seems to be correct but i am wondering whether the "(songs.pk)" part can affect the validity of the path? because it has a "()" in it, If so how can i remove that part of the string? (programtically) and the details dialog shows that inner Exception = nothing

before calling the rename function try using msgbox to see what actually the fname and filename are containing such an error may creep due to using "/" instead of "\" or vice versa but there may be some other factor too. Try it!!!

Do not use the following characters in the file name:
/ (slash)
\ (back slash)
? (question mark)
* (star)
" (quotes)
> (greater than)
< (less than)
| (pipe)
: (colon)
(\0) (null char)
The : (colon) character can appear only once in the full path to define de unit like C: or PRN:
The \ (back slash) character can appear twice at the begning instead of the unit to define a network path. Ie \\MyServer.
Also the \ back slash character is used to name the full directory path like C:\Users

The file name (including the extesion if any) must not finish with a blank or null char.

The $ at the end of a sharing name is used to hide the directory or unit

See this article from Wikipedia to learn about file system and naming conventions.

Hope this helps

I found it, I found IT!!! :) The problem occurs due to the spaces in the new name in My.Computer.RenameFile (file as string, newname as string).
I tested it with as random string without spaces and it worked My.Computer.FileSystem.RenameFile(filepath, "tetet.mp3") But I want to rename the file with a string that contains the spaces (needed, ie.Song File one" and such and such) how can i do this??

Just surround the new name with quotes.

Hi raaif

Try using regex (Regular Expressions)

private sub InvalidcharsReplacer(Byref SourceString as string)
        Dim rgPattern = "[\\\/:\*\?""'<>|]"  '----Invalid Characters

        Dim objRegEx As New Regex(rgPattern)

        SourceString =  objRegEx.Replace(SourceString, "")
End sub

Call this function to your code each time to make sure all the invalid characters are filtered. Read Regular expressions tutorials for further study



@lolafuertes i dont understand. The names are not given by me but the user so ...??????

Probaly you are receiving the message because

MSDN: newName contains path information (ArgumentException).

Instead of

My.Computer.FileSystem.RenameFile("D:\Test\5prodigy1.mp3", "D:\Test\5prodigy1(songs.pk).mp3")

Try to use

My.Computer.FileSystem.RenameFile("D:\Test\5prodigy1.mp3", "5prodigy1(songs.pk).mp3")

Anyway I would prefer to use the System.IO functions like:

Dim sourceFile as New Syste.IO.FileInfo(filepath)
If sourceFile.Exists then
   Dim destinationFile as New System.IO.FileInfo(path)
   If destinationFile.Exists then
      MsgBox "Destination file already exists"
      ' Can't rename
      ' TODO: Exit 
   End If
      ' To rename, move it
   Catch ex as Exception
      ' Can't move
      ' TODO: Exit
   End Try
End IF

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.