简体   繁体   中英

Rename a file extension in VB.NET

I've written a data sorter which writes out a .build extension to the files it's writing to so nothing tries to lift it as it's writing data to particular files.

At the end, I want to rename anything with a .build extension to a .dat before the program closes.

 For Each f In outputFiles

        For Each r In TrailerRecords

            ' Set the bill count variable
            If r.VariableField Then
                r.Fields(1) = String.Format("{0:000000}", f.Value)
            End If

            ' Write the trailer record to the output file
            File.AppendAllText(f.Key, r.ToString() & vbLf)
        Next

        ' Rename all.build files to .dat

NEEDS TO GO HERE

        ' Copy each output file to the backup directory
        File.Copy(f.Key, Path.Combine(backupFolder, Path.GetFileName(f.Key)), True)
    Next
End Sub

Get the files and rename it:

Dim auxFile as String
Dim files() As String = IO.Directory.GetFiles(myFolderPath, "*.build")

For Each sFile As String In files
    auxFile = IO.Path.GetFileNameWithoutExtension(sFile) & ".dat"
    My.Computer.FileSystem.RenameFile(sFile, auxFile)
Next

You can use File.Copy() with the overwrite property to True instead.

Take a look at the MSDN documentation:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM