用于复制和重命名文件的批处理文件

时间:2014-09-18 12:32:45

标签: vba powershell batch-file scripting cmd

我有一个excel文件,我需要多次复制(103,具体),这些新文件需要根据文本文件重命名。例如,63100_attachment.xls是要复制的文件,新文件将具有以下名称(我已在单独的Excel文件中列出,我也可以将其保存在文本文件中):

63126_attachment.xls
63283_attachment.xls
63284_attachment.xls
63285_attachment.xls
63299_attachment.xls
63300_attachment.xls
63301_attachment.xls
63326_attachment.xls
63327_attachment.xls
63328_attachment.xls
63348_attachment.xls
63349_attachment.xls
63350_attachment.xls
63351_attachment.xls
63352_attachment.xls
63439_attachment.xls
63440_attachment.xls
63441_attachment.xls...

' _attachment'之前的数字。字符串不是增量的,而且确实需要基于列表。任何人的帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

for /f "delims=" %%a in (listfile.txt) do copy "63100_attachment.xls" "%%a"

即,作为批处理文件中的一行。将%%a减少到%a以从提示操作。添加>nul以禁止邮件。将copy更改为ECHO copy以查看建议的操作,而不是执行它们。

答案 1 :(得分:0)

Sub batch_rename()

On Error GoTo errHndl

Dim fso As New FileSystemObject
Dim fld As Folder
Dim sourcePath As String, destPath As String
Dim sourceFile As String, destFile As String, sourceExtension As String
Dim rng As Range, cell As Range, row As Range

sourcePath = "\path to old files\"
destPath = "\path to new files\"
sourceFile = ""
destFile = ""
Set fso = CreateObject("Scripting.FileSystemObject")
Set rng = ActiveSheet.Range("A2", "B10")

For Each row In rng.Rows
    sourceExtension = Split(Trim(row.Cells(, 2)), ".")(1)
    sourceFile = sourcePath + Trim(row.Cells(, 2))
    destFile = destPath + Trim(row.Cells(, 1)) + "." + sourceExtension
    fso.CopyFile sourceFile, destFile, False
Next row

MsgBox "Yay! Operation was successful.", vbOKOnly + vbInformation, "Done"
Exit Sub


errHndl:
    MsgBox "Error happened while working on: " + vbCrLf + _
        sourceFile + vbCrLf + vbCrLf + "Error " + _
        Str(Err.Number) + ": " + Err.Description, vbCritical + vbOKOnly, "Error"

End Sub