Now, it does work and all vbs files are changed according to the input. The only problem is, maybe once every few hundred files, one file might either end up blank or with its contents copied twice into the same file. The one blank I've ever seen in its many uses was likely user error in stopping the script prematurely, as it has a very small window in which the file is deleted but not replaced with the adjusted text yet. Most of the time it's been issue with the contents of the file appended twice into the same one. I don't think this is due to files being in use, as I've seen this behavior in files that definitely weren't being run at the time. Fortunately for this usage, if something breaks it gets noticed and we can fix the damage pretty easily. I'd just like to get the part between the lines below more reliable, if anyone has suggestions.
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; Place script in the directory containing the files intended to be changed
; Be careful! Script will replace words embedded in larger words. For instance,
; Changing the string 'cat' to 'dog' will change the word concatenate to condogenate, etc.
; toggles case sensitivity
StringCaseSense, On
; Accepts input for file extension to edit, string to replace and what to replace it with. Exits script if cancel is pressed at any point. Does not accept blank input
Loop
{
Inputbox, FileExt, File Extension, Please enter the file extension you would like to affect. (Input * to affect any and all file extensions)
If ErrorLevel
ExitApp
If FileExt !=
Break
}
Loop
{
Inputbox, OldPass, Old String, Please enter the string to be replaced
If ErrorLevel
ExitApp
If OldPass !=
Break
}
Loop
{
Inputbox, NewPass, New String, Please enter the new string
If ErrorLevel
ExitApp
If NewPass =
{
MsgBox, 3,, You have chosen to replace the string with nothing, deleting all instances of that string. Are you sure you wish to do this?
IfmsgBox Cancel
ExitApp
Else ifmsgBox Yes
Break
} else {
Break
}
}
; Remove period from the start of file extension if it exists
StringLen, StrLen, FileExt
StringTrimRight, TestStr, FileExt, (StrLen-1)
If TestStr = .
{
StringTrimLeft, FileExt, FileExt, 1
}
;----------------------------------------------------------------------------------------------------------------------------------------------------------
;***THIS code between the lines is the bit that does all the work, for which I am having the issue****
; Loops looking through all files and subfolders in script's directory, replaces string based on input
Loop, Files, %A_WorkingDir%\*.%FileExt%, R
{
FullName=%A_LoopFileLongPath%
FileRead, OldFile, %FullName%
StringReplace, NewFile, OldFile, %OldPass%, %NewPass%, All
FileDelete, %FullName%
FileAppend, %NewFile%, %FullName%
}
;-----------------------------------------------------------------------------------------------------------------------------------------------------------
ExitApp