Vbscript 搜索名称中包含特定字符串的文件并重命名它

Vbscript 搜索名称中包含特定字符串的文件并重命名它,vbscript,file-rename,Vbscript,File Rename,我是VBScript的业余爱好者(并且对它的工作方式非常着迷)。我必须每天处理某些报告,我已经为这些报告生成了一个脚本来完成任务。问题是,我已经为特定的文件名生成了脚本,这些报告每天都带有时间日期,并且在名称中添加了一些额外的参数。因此,我需要一个vbscript来保存我每天根据脚本中指定的名称重命名文件的10分钟工作 例如,对于D:/reports/have fileAMR KilobyteData\u 23022013\u 4399\u 223.xls我想将其重命名为AMR Kilobyte

我是VBScript的业余爱好者(并且对它的工作方式非常着迷)。我必须每天处理某些报告,我已经为这些报告生成了一个脚本来完成任务。问题是,我已经为特定的文件名生成了脚本,这些报告每天都带有时间日期,并且在名称中添加了一些额外的参数。因此,我需要一个vbscript来保存我每天根据脚本中指定的名称重命名文件的10分钟工作

例如,对于
D:/reports/
have file
AMR KilobyteData\u 23022013\u 4399\u 223.xls
我想将其重命名为
AMR KilobyteData.xls
。就这样!:)


请帮我解决这个问题:)

首先使用如下脚本遍历文件夹中的所有文件:

Dim fso, folder, file
Dim folderName, searchFileName, renameFileTo

' Parameters
folderName     = "D:\reports\"
searchFileName = "AMR KilobyteData"
renameFileTo   = "AMR KilobyteData.xls"

' Create filesystem object and the folder object
' how the FSO works: http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx
Set fso = CreateObject("Scripting.FileSystemObject")  
Set folder = fso.GetFolder(folderName)  

' Loop over all files in the folder until the searchFileName is found
For each file In folder.Files    
    ' See if the file starts with the name we search
    ' how instr works: http://www.w3schools.com/vbscript/func_instr.asp
    If instr(file.name, searchFileName) = 1 Then
        file.name = renameFileTo
        ' Exit the loop, we only want to rename one file
        Exit For
    End If
Next
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("D:\reports").Files
  pos = InStr(f.Name, "_")
  If pos > 0 Then
    newName = Left(f.Name, pos-1) & "." & fso.GetExtensionName(f.Name)
    f.Move fso.BuildPath(f.ParentFolder, newName)
  End If
Next

它应该正常工作(但我没有测试它)。我希望我激发了您的好奇心,您将研究此代码的工作机制。这就是为什么我在可以找到文档的链接中添加了内容。

首先使用如下脚本遍历文件夹中的所有文件:

Dim fso, folder, file
Dim folderName, searchFileName, renameFileTo

' Parameters
folderName     = "D:\reports\"
searchFileName = "AMR KilobyteData"
renameFileTo   = "AMR KilobyteData.xls"

' Create filesystem object and the folder object
' how the FSO works: http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx
Set fso = CreateObject("Scripting.FileSystemObject")  
Set folder = fso.GetFolder(folderName)  

' Loop over all files in the folder until the searchFileName is found
For each file In folder.Files    
    ' See if the file starts with the name we search
    ' how instr works: http://www.w3schools.com/vbscript/func_instr.asp
    If instr(file.name, searchFileName) = 1 Then
        file.name = renameFileTo
        ' Exit the loop, we only want to rename one file
        Exit For
    End If
Next
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("D:\reports").Files
  pos = InStr(f.Name, "_")
  If pos > 0 Then
    newName = Left(f.Name, pos-1) & "." & fso.GetExtensionName(f.Name)
    f.Move fso.BuildPath(f.ParentFolder, newName)
  End If
Next

它应该正常工作(但我没有测试它)。我希望我激发了您的好奇心,您将研究此代码的工作机制。这就是为什么我将链接放在可以找到文档的地方。

要删除的信息是否总是在文件名的第一个下划线之后?如果是这样,您可以这样做:

Dim fso, folder, file
Dim folderName, searchFileName, renameFileTo

' Parameters
folderName     = "D:\reports\"
searchFileName = "AMR KilobyteData"
renameFileTo   = "AMR KilobyteData.xls"

' Create filesystem object and the folder object
' how the FSO works: http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx
Set fso = CreateObject("Scripting.FileSystemObject")  
Set folder = fso.GetFolder(folderName)  

' Loop over all files in the folder until the searchFileName is found
For each file In folder.Files    
    ' See if the file starts with the name we search
    ' how instr works: http://www.w3schools.com/vbscript/func_instr.asp
    If instr(file.name, searchFileName) = 1 Then
        file.name = renameFileTo
        ' Exit the loop, we only want to rename one file
        Exit For
    End If
Next
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("D:\reports").Files
  pos = InStr(f.Name, "_")
  If pos > 0 Then
    newName = Left(f.Name, pos-1) & "." & fso.GetExtensionName(f.Name)
    f.Move fso.BuildPath(f.ParentFolder, newName)
  End If
Next

要删除的信息是否总是在文件名的第一个下划线之后?如果是这样,您可以这样做:

Dim fso, folder, file
Dim folderName, searchFileName, renameFileTo

' Parameters
folderName     = "D:\reports\"
searchFileName = "AMR KilobyteData"
renameFileTo   = "AMR KilobyteData.xls"

' Create filesystem object and the folder object
' how the FSO works: http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx
Set fso = CreateObject("Scripting.FileSystemObject")  
Set folder = fso.GetFolder(folderName)  

' Loop over all files in the folder until the searchFileName is found
For each file In folder.Files    
    ' See if the file starts with the name we search
    ' how instr works: http://www.w3schools.com/vbscript/func_instr.asp
    If instr(file.name, searchFileName) = 1 Then
        file.name = renameFileTo
        ' Exit the loop, we only want to rename one file
        Exit For
    End If
Next
Set fso = CreateObject("Scripting.FileSystemObject")

For Each f In fso.GetFolder("D:\reports").Files
  pos = InStr(f.Name, "_")
  If pos > 0 Then
    newName = Left(f.Name, pos-1) & "." & fso.GetExtensionName(f.Name)
    f.Move fso.BuildPath(f.ParentFolder, newName)
  End If
Next
做得好:)谢谢:)我实际上研究了代码的机制,并试图使其更具动态性。当我尝试使用2D数组变量执行此操作时,失败了。不知道为什么<代码>Dim SearchFileName(4,2)SearchFileName(0,0)=“允许的KilobyteData”做得好:)谢谢:)我实际上研究了代码的机制,并试图使其更具动态性。当我尝试使用2D数组变量执行此操作时,失败了。不知道为什么<代码>Dim SearchFileName(4,2)SearchFileName(0,0)=“允许的千字节数据”