我创建了脚本FindAndReplace.vbs,它只是查看文件夹并在文件名中找到任何所需的字符串,并用所需的字符串替换该字符串。
现在,我尝试创建的是一个VBScript(ConfigureFindAndReplace.vbs),可以在FindAndReplace.vbs代码中轻松配置以下3件事:
我希望脚本对没有编程技能的用户来说是用户友好的。
我希望主要的可执行脚本FindAndReplace.vbs在每次运行ConfigureFindAndReplace.vbs时自动更新。
为了更好地帮助你理解这里是 e链接到包含上述两个文件的.zip文件。这是我能得到的,我已经被困了2天了:
https://www.dropbox.com/s/to3r3epf4ffyedb/StackOverFlow.zip?dl=0
希望我能正确解释。如果没有,请告诉我你需要知道的事情。
提前致谢:)
以下是文件中的代码:
ConfigureFindAndReplace.vbs:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject ("Shell.Application")
Set objTFolder = objShell.BrowseForFolder (0, "Select Target Folder", (0))
targetPath = objTFolder.Items.Item.Path
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName) & "/"
strFind = InputBox("Add string to find.","String to Find", "")
If strFind = "" Then
Wscript.Quit
End If
strReplace = InputBox("Add string to replace with.","Replace with", "")
Dim VarFind
Dim VarReplace
Dim VarPath
VarFind = strFind
VarReplace = strReplace
VarPath = targetPath
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run sScriptDir & "FindAndReplace.vbs /strfolderpath:" & VarPath
WshShell.Run sScriptDir & "FindAndReplace.vbs /strfind:" & VarPath
WshShell.Run sScriptDir & "FindAndReplace.vbs /strreplace:" & VarPath
FindAndReplace.vbs:
'Written by Terje Borchgrevink Nuis on 15.12.2014
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strFind
Dim strReplace
Dim strFolderPath
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
答案 0 :(得分:1)
你想你希望ConfigureFindAndReplace
改变另一个脚本,这是一个坏主意。
您还不知道它 ,但您真正想要的是FindAndReplace
从配置文件中读取这些项目。
如果配置文件格式正确且易于阅读,那么您的用户可以直接更新配置文件,因此您甚至可能不需要ConfigureFindAndReplace
脚本。
如何吗
有一个包含3行的文本文件
Target Folder=c:\DataFolder
String to find=a string
Replace with=Replace a string with this string
然后在FindAndReplace
中,在做任何工作之前,打开此文件并阅读三行。
拆分'='标志上的线条。左半部分是设置,右半部分是值
在脚本
If configLineLeft = "Target Folder" then REM Each of these should be case insensitive match
REM e.g. lcase(configLineLeft) = lcase("Target Folder")
TargetFolder = configLineRight
else if configLineLeft = "String to find" then
FindString = configLineRight
else if configLineLeft = "Replace with" then
ReplaceString = configLineRight
else
REM REPORT A PROBLEM TO THE USER AND EXIT
EXIT SUB
end if
您可以在while循环中执行上述操作(而不是文件末尾),读取每一行并测试以查看它是什么设置。
答案 1 :(得分:1)
由于我在.Zip中找不到任何VBScript,所以提供一些一般建议。如果你想要一个不被编辑的脚本来做不同的事情
cscript FindAndReplace.vbs "c:\some\folder" "param" "arg"
我会从第一种方法开始。
阅读完修改后
而不是用坏的args调用你的脚本:
WshShell.Run sScriptDir & "FindAndReplace.vbs /strfolderpath:" & VarPath
WshShell.Run sScriptDir & "FindAndReplace.vbs /strfind:" & VarPath
WshShell.Run sScriptDir & "FindAndReplace.vbs /strreplace:" & VarPath
使用适当的args执行一次:
WshShell.Run sScriptDir & "FindAndReplace.vbs /strfolderpath:" & VarPath & " /strfind:" & VarFind & "/strreplace:" & VarReplace
(未经测试;您需要检查名称并注意正确引用; cf here)