读取文件夹路径并使用路径访问文件并重命名

时间:2016-10-01 14:53:30

标签: vbscript rename config-files

我想写一个可以访问具有文件夹路径的配置文件的VBScript。一旦定向到该文件夹​​,就会有_DDMMYYYY的文档。我想删除_和日期戳。

有人可以帮帮我吗?

Option Explicit

Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")

'Declare the variables to be used from the property file
Dim Folder
Dim objWMIService, objProcess, colProcess, obNetwork
Dim strComputer, WshShell, strComputerName

strComputer = "."

Set obNetwork = WScript.CreateObject("Wscript.Network")
strComputerName = obNetwork.ComputerName
Set obNetwork = Nothing

SetConfigFromFile("C:\Users\Lenovo\Desktop\RenameFile\ConfigPad.txt")
MsgBox "Folder = " & Folder

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run Folder

'---------- Get Variables from ConfigPad.txt ----------
Sub SetConfigFromFile(fileName)
  Dim strConfigLine
  Dim fConFile
  Dim EqualSignPosition
  Dim strLen
  Dim VariableName
  Dim VariableValue

  Set fConFile = fso.OpenTextFile(fileName)
  While Not fConFile.AtEndOfStream
    strConfigLine = fConFile.ReadLine
    strConfigLine = Trim(strConfigLine)
    'MsgBox(strConfigLine)
    If (InStr(1,strConfigLine,"#",1) <> 1 And Len(strConfigLine) <> 0) Then
      EqualSignPosition = InStr(1, strConfigLine, "=", 1)
      strLen = Len(strConfigLine)
      VariableName = LCase(Trim(MID(strConfigLine, 1, EqualSignPosition-1))) 'line 34
      VariableValue = Trim(Mid(strConfigLine, EqualSignPosition + 1, strLen - EqualSignPosition))
      Select Case VariableName
        'ADD EACH OCCURRENCE OF THE CONFIGURATION FILE VARIABLES(KEYS)
        Case LCase("Folder")
          If VariableValue <> "" Then Folder = VariableValue
      End Select
    End If
  Wend
  fConFile.Close
End Sub

'---------- Rename the documents ----------
Dim FLD
Dim fil
Dim strOldName
Dim strNewName
Dim strFileParts

'Set the folder you want to search.
Set FLD = FSO.GetFolder("C:\Users\Lenovo\Desktop\RenameFile\RenameFile.vbs")

'Loop through each file in the folder
For Each fil in FLD.Files
  'Get complete file name with path
  strOldName = fil.Path

  'Check the file has an underscore in the name
  If InStr(strOldName, "_") > 0 Then
    'Split the file on the underscore so we can get everything before it
    strFileParts = Split(strOldName, "_")
    'Build the new file name with everything before the
    'first under score plus the extension
    strNewName = strFileParts(0) & ".txt"

    'Use the MoveFile method to rename the file
    FSO.MoveFile strOldName, strNewName
  End If
Next

'Cleanup the objects
Set FLD = Nothing
Set FSO = Nothing

我的配置文件只有这个:

Folder = "C:\Users\Lenovo\Desktop\RenameFile\Test - Copy"

2 个答案:

答案 0 :(得分:0)

Set fso = CreateObject("Scripting.FileSystemObject")
Set TS = fso.OpenTextFile("C:\Users\Lenovo\Desktop\RenameFile\ConfigPad.txt")
SrcFolder = TS.ReadLine
Set fldr = fso.GetFolder(SrcFolder)
Set Fls = fldr.files
For Each thing in Fls
    If Left(thing.name, 1) = "_" AND IsNumeric(Mid(thing.name, 2, 8)) Then 
        thing.name = mid(thing.name, 10)
    End If
Next

这假设配置文件中的第一行是路径。它重命名以下划线开头,后跟8位数字的任何文件。

答案 1 :(得分:0)

请尝试这个

configfile = "Config File Name Here" 'Example : C:\Documents\Config.txt

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set tf = objFSO.OpenTextFile(configfile, 1)

Do Until tf.AtEndOfStream
    cl = tf.ReadLine
    If InStr(cl, "Folder = ") > 0 Then
        Folder = Replace(Replace(cl,"Folder = ",""),chr(34),"")
        tf.Close
        Exit Do
    End If
Loop

For Each File in objFSO.GetFolder(Folder).Files
    If InStr(File.Name, "_") > 0 And IsNumeric(Mid(File.Name,InStr(File.Name, "_") + 1,8)) Then
        NewName = Replace(File.Name,Mid(File.Name,InStr(File.Name, "_"),9),"")
        objFSO.MoveFile File.Path, objFSO.GetParentFolderName(File.Path) & "\" & NewName
    End If
Next
MsgBox "Task Complete", vbOKOnly, "Remove Time Stamp"