1

I have made the script FindAndReplace.vbs which simply watches a folder and finds any desired string in the filenames and replaces that string with a desired string.

Now, What I´m trying to create is a VBScript (ConfigureFindAndReplace.vbs) that will easily configure the following 3 things in the FindAndReplace.vbs code:

  1. Browse and select which folder to watch (targetPath)
  2. Which text string to search for in the filenames of the files inside this folder (strFind)
  3. Which string to replace with (strReplace)

I want the script to be user friendly for users with no programming skills.

And I want the main executable script FindAndReplace.vbs to automatically be updated EVERY time the ConfigureFindAndReplace.vbs is run.

To better help you understand here is th e link to a .zip file containing both of the above mentioned files. This is as far as I can get and I´ve been stuck for 2 days now:

https://www.dropbox.com/s/to3r3epf4ffyedb/StackOverFlow.zip?dl=0

Hope I explained it properly. If not, let me know whatever you need to know.

Thanks in advance:)

And here are the codes from the files:

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
tnuis
  • 121
  • 2
  • 4
  • 8

2 Answers2

1

You think you want ConfigureFindAndReplace to change the other script, this is a bad idea.

You don't know it yet, but what you actually want is for FindAndReplace to read those items from a configuration file.

If the config file is well formed and easy to read, then your users can directly update the config file, so you may not even need the ConfigureFindAndReplace script.

How?

Have a text file with 3 lines

Target Folder=c:\DataFolder
String to find=a string
Replace with=Replace a string with this string

Then in FindAndReplace, before doing any work, you open this file and read in the three lines. Split the lines on the '=' sign. The left half is the setting and the right half is the value.
Math these up to three variables in the script

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

You'd do the above in a while loop (while not end of file), reading each line and testing to see which setting it is.

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • Binary Worrier: No down voting needed here, I´m open for any suggestions:) How would you propose this to be done? Do you have any solutions that you could share with me? – tnuis Dec 16 '14 at 13:23
  • Hmmm... I tried the suggested options but cannot seem to get any of them to work. Is there no easier way of doing it? – tnuis Dec 16 '14 at 16:19
1

As I can't find any VBScript in your .Zip, some general advice. If you want a not-to-be-edited script to do different things

  • let the script access parameters/arguments and specifying the differences by calling the script with different arguments: cscript FindAndReplace.vbs "c:\some\folder" "param" "arg"
  • let the script access config data (from a .txt, .ini, .xml, .json, ... file; from a database; from the registry; ...) and use the config script to set these data
  • use a template/placeholder file to generate (different version of) the script

I would start with the first approach.

After reading your edit:

Instead of calling your script trice with bad args:

WshShell.Run sScriptDir & "FindAndReplace.vbs /strfolderpath:" & VarPath
WshShell.Run sScriptDir & "FindAndReplace.vbs /strfind:" & VarPath
WshShell.Run sScriptDir & "FindAndReplace.vbs /strreplace:" & VarPath

execute it once with proper args:

WshShell.Run sScriptDir & "FindAndReplace.vbs /strfolderpath:" & VarPath & " /strfind:" & VarFind & "/strreplace:" & VarReplace

(untested; you need to check the names and take care of proper quoting; cf here)

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96