Renaming a file extension

wonder_gal

Active member
Joined
Jun 5, 2006
Messages
34
Programming Experience
1-3
Hi, I was coding a function which unzips a zip file and then changed the extension from ".csv" to ".txt" but I received this error., "Procedure Call or argument is not valid." The highlighted statement is the line which cause this error. Can someone let me know what's wrong with my arguments?

VB.NET:
Dim fileName As String = Path.GetFullPath("..\")

        With OpenZIPFileDialog
            .InitialDirectory = fileName
            .Filter = "Zip Files (*.zip)|*.zip*"
            .FilterIndex = 1

            If .ShowDialog() = DialogResult.OK Then
                Dim x As New ICSharpCode.SharpZipLib.Zip.FastZip
                x.ExtractZip(.FileName, fileName, "csv;xsl")
            End If
        End With

        MsgBox("Congratulations, your file have been unzipped!", MsgBoxStyle.Information, "Sucessfully Unzipped")

        Try
            [B]FileSystem.Rename("..\testing.csv", "..\testing.txt")[/B]
            MsgBox("Congratulations, new txt file being created using NEW method!", MsgBoxStyle.Information, "New File Path")
        Catch ex As Exception
            MsgBox("Error occured: " & ex.Message, MsgBoxStyle.Information, "Error")
        End Try
 
FileSystem.Rename?? Do you mean IO.File.Move?
 
Why do your rename from/to names contain double-dots? .. Use absolute paths.. If you start the user in

c:\program files\my app

and then then navigate to
c:\temp\myzip.zip

which you then extract to
c:\temp\myzip

then the testing.csv (which i'm assuming youre expecting to find inside the zip) is not in
c:\program files

I recommend you work with absolute paths once they are known. Avoid using the Microsoft.VisualBasic.FileSystem if you can use .NET core System.IO.File, System.IO.FileInfo, System.IO.Directory and System.IO.DirectoryInfo
Surely, if the user picked the file, then the
 
Hi, I've tried ur suggestion, using absolute paths, so my rename file name coding became

VB.NET:
Microsoft.VisualBasic.FileSystem.Rename("C:\temp\mcom20070403141406.csv", "C:\temp\mcom20070403141406.txt")

But I still getting this error "Procedure Call or argument is not valid".
If the method Microsoft.VisualBasic.FileSystem.Rename is not recommended here, can u all suggest me a better method or a better .NET built in function to rename my CSV file to TXT file?

Your help is greatly appreciated.
Thanks.
 
Does file "C:\temp\mcom20070403141406.csv" really exist? IO.File.Move method is more specific with error message here.
 
Yaya, this path "C:\temp\mcom20070403141406.csv" really exists one, the extract method is working fine here, I only have problem renaming my CSV file to become TXT file. :(
 
Hi, I've just tried another method

VB.NET:
Dim sourcePath As String = "C:\temp\testing.csv"
Dim destPath As String = "C:\temp\testing.txt"
Dim fInfo As FileInfo = New FileInfo(sourcePath)
fInfo.MoveTo(destPath)

But I received an error saying that "Unable to find the specified file". Can someone please point out to me what's wrong with my destination path? Is that the correct way to rename my file extension??
 
"Unable to find the specified file".
The error meant that the source file didn't exist. If the filename input string can't be verified from code logic you can also check if it exist with IO.File.Exist method before attempting to rename=move it. Usually the path is verified because you are iterating a folder content.
 
Hi all,

I've solved my problem, by using File.Move method. :)
Anyway, thanks for everyone's suggestions earlier. ;)

FileInfo.MoveTo and File.Move should use the same underlying code; I cant understand why one would work and the other not..

MSDN File.Move said:
This method works across disk volumes, and it does not throw an exception if the source and destination are the same. Note that if you attempt to replace a file by moving a file of the same name into that directory, you get an IOException. You cannot use the Move method to overwrite an existing file.
The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see <A onclick="javascript:Track('ctl00_LibFrame_ctl26|ctl00_LibFrame_ctl27',this);" href="http://msdn2.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory.aspx">GetCurrentDirectory.

MSDN FileInfo.MoveTo said:
This method works across disk volumes. For example, the file c:\MyFile.txt can be moved to d:\public and renamed NewFile.txt.
For a list of common I/O tasks, see <A onclick="javascript:Track('ctl00_LibFrame_ctl26|ctl00_LibFrame_ctl27',this);" href="http://msdn2.microsoft.com/en-us/library/ms404278.aspx">Common I/O Tasks.
 
Back
Top