【问题标题】:read folder path and using the path to access the files and rename it读取文件夹路径并使用路径访问文件并重命名
【发布时间】:2016-10-01 14:53:30
【问题描述】:

我想编写一个可以访问具有文件夹路径的配置文件的 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"

【问题讨论】:

  • 什么不能按预期工作?你期望代码做什么,它实际上做什么?你有错误吗?他们说什么?
  • 第 60 行,找不到路径。
  • C:\Users\Lenovo\Desktop\RenameFile\RenameFile.vbs 很可能是一个文件,而不是文件夹。您是否期望 GetFolder 返回该文件的父文件夹?这不是方法的工作原理。请改用FSO.GetFile("...").ParentFolder。
  • 然后您需要使用变量Folder 调用GetFolder,其中您的其他过程存储了该路径:Set FLD = FSO.GetFolder(Folder)。
  • 检查变量的值。检查该文件夹是否确实存在并且是一个文件夹。 WScript.Echo Folder &amp; ": " &amp; FSO.FolderExists(Folder)

标签: vbscript rename configuration-files


【解决方案1】:
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 位数字的所有文件。

【讨论】:

  • 我懒得理你了。你怎么敢说“它不起作用”这样可悲的话。确实是因为我测试了它。
  • 我试过了,还是不行,我为什么要骗它呢?我说得很好。
【解决方案2】:

请试试这个

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"

【讨论】:

    猜你喜欢
    • 2021-10-04
    • 2017-04-10
    • 2017-04-13
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多