重命名文件到另一个文件名称前缀在同一目录VISUAL BASIC

时间:2014-04-11 21:04:41

标签: vb.net

目前我有一个代码可以清除目录中的所有内容,并在目录中放置两个模板文件,一个.dwg和一个.jpg文件。用button4.click

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickDim path As 

String = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)& "C:\epds\WIP\"
    System.IO.Directory.Delete(path, True)

    My.Computer.FileSystem.CopyFile( _
   "C:\Users\edevault\Documents\temp\temp.dwg", _
   "C:\epds\WIP\temp.dwg", overwrite:=True)

    My.Computer.FileSystem.CopyFile( _
   "C:\Users\edevault\Documents\temp\BX-.jpg", _
   "C:\epds\WIP\BX-.jpg", overwrite:=True)

然后我将.zip文件下载到C:\ _ epds \ WIP \目录并手动解压缩。 我想要做的是反转序列并下载.zip然后擦除除.zip之外的所有目录,并将.dwg和.jpg文件放在目录中,并将.dwg和.jpg文件重命名为最后一个.zip文件名的6位数字。

  • 示例:
  • SO123456.zip下载到C:\ _ epds \ WIP \
  • button4.click删除除SO123456.zip之外的所有文件,并将CX \ .jpg和temp.dwg复制到C:\ Users \ edevault \ Documents \ temp \中的C:\ _ epds \ WIP \,然后将BX-.jpg重命名为BX-123456.jpg和temp.dwg到123456.dwg

不需要SO123456.zip的SO前缀

2 个答案:

答案 0 :(得分:0)

这可以帮到你

System.IO.File.Copy( "SO123456.zip", "temp\SO123456.zip" )
' same for other files you dont need to remove it


Dim strDirectory As String = " C:\epds\WIP\"
For Each foundFile As String In My.Computer.FileSystem.GetFiles(strDirectory, "*.*")
   My.Computer.FileSystem.DeleteFile(foundFile, FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently)
Next

System.IO.File.Copy( "SO123456.zip", "C:\epds\WIP\SO123456.zip" )
' same for other files you dont need to remove it

答案 1 :(得分:0)

以下是我能够做到这一点的方法:

Dim tmpDir As New IO.DirectoryInfo("C:\epds\WIP\")

Dim subd As IO.DirectoryInfo
Dim old, newf, old2, newf2, old3, newf3 As String
Dim withparts As String
Dim withoutparts As String
Dim zippath, extractpath, zip As String
Dim file As IO.FileInfo

'Button to extract ZIP, delete the ZIP, copy in DWG, BOX label, and CMD templates, and rename them the SO number

Public Sub CopyDWG_Click(sender As Object, e As EventArgs) Handles CopyDWG.Click
    zippath = "C:\epds\WIP\"
    extractpath = "C:\epds\WIP\"
    Directory.GetFiles("C:\epds\WIP\")
    For Each Me.file In tmpDir.GetFiles()

    Next


    Try
        ZipFile.ExtractToDirectory(file.FullName, extractpath)
    Catch ex As NullReferenceException
        MsgBox("ZIP Archive not found.", MsgBoxStyle.Exclamation, "Design Dock")
        Exit Sub
    Catch ex As FileNotFoundException
        MsgBox("ZIP Archive not found.", MsgBoxStyle.Exclamation, "Design Dock")
        Exit Sub
    Catch ex As InvalidDataException
        MsgBox("ZIP Archive not found.", MsgBoxStyle.Exclamation, "Design Dock")
        Exit Sub
    End Try


    My.Computer.FileSystem.DeleteFile(file.FullName)
    For Each Me.subd In tmpDir.GetDirectories()
        withparts = subd.Name
        withoutparts = Replace(withparts, "SO", "")
    Next
    MsgBox("Archive unzipped to: " & subd.FullName, MsgBoxStyle.Information, "Design Dock")

    Dim msg, title, response As String
    Dim style As Single


    msg = "Do you want to copy starter files to " & subd.FullName & "?"
    title = "Design Dock"
    style = MsgBoxStyle.YesNo Or MsgBoxStyle.Question
    response = MsgBox(msg, style, title)



    If response = MsgBoxResult.Yes Then


        My.Computer.FileSystem.CopyFile("C:\TEMP\STARTER.DWG", subd.FullName & "\STARTER.DWG")

        old = subd.FullName & "\STARTER.DWG"
        newf = withoutparts & ".dwg"
        Try
            My.Computer.FileSystem.RenameFile(old, newf)
        Catch ex As IOException
            MsgBox("DWG already exists, nothing copied.", MsgBoxStyle.Information, "Design Dock")
            My.Computer.FileSystem.DeleteFile(subd.FullName & "\STARTER.DWG")
        End Try

        My.Computer.FileSystem.CopyFile("C:\TEMP\BX-.JPG", subd.FullName & "\BX-.JPG")

        old2 = subd.FullName & "\BX-.JPG"
        newf2 = "BX-" & withoutparts & ".JPG"
        Try
            My.Computer.FileSystem.RenameFile(old2, newf2)
        Catch ex As IOException
            MsgBox("Box Label already exists, nothing copied.", MsgBoxStyle.Information, "Design Dock")
            My.Computer.FileSystem.DeleteFile(subd.FullName & "\BX-.JPG")
        End Try

        My.Computer.FileSystem.CopyFile("C:\TEMP\BLANK.CMD", subd.FullName & "\BLANK.CMD")

        old3 = subd.FullName & "\BLANK.CMD"
        newf3 = withoutparts & ".CMD"
        Try
            My.Computer.FileSystem.RenameFile(old3, newf3)




        Catch ex As IOException
            MsgBox("Cut File already exists, nothing copied.", MsgBoxStyle.Information, "Design Dock")
            My.Computer.FileSystem.DeleteFile(subd.FullName & "\BLANK.CMD")


        End Try
    Else


        Exit Sub
    End If