如何从.txt文件中读取变量数据?

时间:2014-12-16 16:26:35

标签: vbscript

  1. 如何让下面的脚本自动读取标有????的3个值?来自.txt文件?
  2. 我将如何在下面的代码中对其进行编码?
  3. .txt文件看起来怎么样?请举个例子

  4. Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim strFind
    Dim strReplace
    Dim strFolderPath
    
    strFolderPath = ????
    targetPath = strFolderPath
    
    'Max number of times to replace string
    strCount = 999'Comparison type: 0 = case sensitive, 1 = case insensitive
    strCompare = 1
    
    If targetPath = "" Then
      Wscript.Quit
    End If
    
    strFind = ????
    If strFind = "" Then
      Wscript.Quit
    End If
    
    strReplace = ????
    
    Set objFolder = objFSO.GetFolder(targetPath)
    
    fileRename objFolder
    
    Sub fileRename(folder)
      Do
        Wscript.sleep 10000
    
        'Loop through the files in the folder
        For Each objFile In folder.Files
          filename = objFile.Name
          ext = objFSO.getExtensionName(objFile)
          safename = Left(filename, Len(filename) - Len(ext) - 1)
    
          strStart = 1
          safename = Replace(safename, strFind,strReplace,strStart,strCount,strCompare)
    
          safename = trim(safename)
    
          On Error Resume Next
    
          'Terminate if filename stop.txt is found
          If filename="STOP.txt" Then
            result = MsgBox ("Are you sure you want to terminate the following VBScript?" & vbNewLine & vbNewLine & "FindAndReplace.vbs", vbOKCancel+vbSystemModal , "Terminate VBScript")
    
            Select Case result
            Case vbOK
              WScript.quit
              Case vbCancel
              MsgBox "FindAndReplace.vbs is still running in the background.",,"Information"
            End Select
          End If
    
          'Only rename if new name is different to original name
          If filename <> safename & "." & ext Then
            objFSO.MoveFile objFile.Path, objFile.ParentFolder.Path & "\" & safename & "." & ext
          End If
    
          If Err.Number <> 0 ThenWScript.Echo "Error renaming: " & filename.path & "Error: " & Err.Description
            Err.Clear
          End If
        Next
      Loop
    End Sub
    

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用3行

的文本文件
foo
bar
baz

可以这样读:

Set f = objFSO.OpenTextFile("C:\path\to\your.txt")

strFolderPath = f.ReadLine
strFind       = f.ReadLine
strReplace    = f.ReadLine

f.Close

答案 1 :(得分:0)

谢谢Ansgar Weichers,这就是诀窍:)再一次,解决方案通常比你想象的容易得多。

对于对他人的任何引用,这是我最终得到的代码:

Dim File
Set File = CreateObject("Scripting.FileSystemObject")
sScriptDir = File.GetParentFolderName(WScript.ScriptFullName) & "\Variables.ini"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set f = objFSO.OpenTextFile(sScriptDir)

Dim strFolderPath
Dim strFind 
Dim strReplace    

strFolderPath = f.ReadLine
strFind = f.Readline
strReplace = f.Readline


'strFolderPath = WScript.Arguments.Named("strfolderpath")
targetPath = strFolderPath

'Max number of times to replace string
strCount = 999
'Comparison type: 0 = case sensitive, 1 = case insensitive
strCompare = 1

If targetPath = "" Then
Wscript.Quit
End If

'strFind = WScript.Arguments.Named("strfind")

If strFind = "" Then
Wscript.Quit
End If

'strReplace = WScript.Arguments.Named("strreplace")


Set objFolder = objFSO.GetFolder(targetPath)


fileRename objFolder

Sub fileRename(folder)
Do

Wscript.sleep 10000

'Loop through the files in the folder
For Each objFile In folder.Files

filename = objFile.Name
ext = objFSO.getExtensionName(objFile)
safename = Left(filename, Len(filename) - Len(ext) - 1)

strStart = 1
safename = Replace(safename, strFind,strReplace,strStart,strCount,strCompare)

safename = trim(safename)

On Error Resume Next

'Terminate if filename stop.txt is found
If filename="STOP.txt" Then

result = MsgBox ("Are you sure you want to terminate the following VBScript?" & vbNewLine & vbNewLine & "FindAndReplace.vbs", vbOKCancel+vbSystemModal , "Terminate VBScript")

Select Case result
Case vbOK   
    WScript.quit
Case vbCancel
    MsgBox "FindAndReplace.vbs is still running in the background.",,"Information"
End Select
End If

'Only rename if new name is different to original name
If filename <> safename & "." & ext Then


objFSO.MoveFile objFile.Path, objFile.ParentFolder.Path & "\" & safename & "." & ext
End If

If Err.Number <> 0 Then
WScript.Echo "Error renaming: " & filename.path & "Error: " & Err.Description
Err.Clear
End If

Next

Loop

End Sub

Variable.ini文件只包含3行,第1行为strFolderPath,第2行为strFind,第3行为strReplace。

再次,非常感谢你:)

相关问题