Sign in to follow this  
Followers 0
sfunk1x

Replace a string in a file that is in root and in all subfolders

7 posts in this topic

I need to modify the index.htm of a website. Only problem is, there was 7 subfolders off root, and there are 100 subfolders in each of the 7 subfolders, and there are (you guessed it) 32 subfolders in each of those 100 subfolders. In EACH of those folders, the index.htm needs to have one string replaced. The string is a JRE version check that forces the client to either use java or just use ie without javascript. I want to modify the file so the user is not forced to use java (whcih means a seperate download and install for the end user).

What I can't figure out, however, is to how to make this perform a recursive search and modify the file in each fo the subfolders off the root of the website. Can anyone spend some time tonight and assist me on this? I'd really appreciate it, and I'd buy you virtual beer for your help! :-)

So, this is my code so far:

$search = FileFindFirstFile("index.htm")

If $search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

StringReplace("if (ver == 4)", "", "if (ver >= 4)")

WEnd

; Close the search handle

FileClose($search)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

this recursive request is so frequent, I may make a list here and link to it!

http://www.autoitscript.com/fileman/users/jdeb/jdeb_autoit_stuff.htm

DirFrench1.au3

Dir command like dos ?, need to store dir names in a file Options

Larry's recursive filesearch UDF?

Autoit3Explorer.au3

ExploreAU3

cameronsdad

UDF: _FileGetTreeList, _FileFindAllR, recursively list folders and files Options Rating

UDF: Recursive directory size, output to XML Options Rating

BlindWig rewrote the _FileGetTreeList() function.

BlindWig's

Deleting old files Options LxPDOSAlex [DPS -cf my DOS coms link]

I'm sure there are others;

Maybe others can contribute to the list of recursive directory scripts which could be modified for this sort of purpose?

Best, Randall

PS I'll duplicate this so all keywords find it from a abase post...

Edited by randallc

Share this post


Link to post
Share on other sites

I need to modify the index.htm of a website. Only problem is, there was 7 subfolders off root, and there are 100 subfolders in each of the 7 subfolders, and there are (you guessed it) 32 subfolders in each of those 100 subfolders. In EACH of those folders, the index.htm needs to have one string replaced. The string is a JRE version check that forces the client to either use java or just use ie without javascript. I want to modify the file so the user is not forced to use java (whcih means a seperate download and install for the end user).

What I can't figure out, however, is to how to make this perform a recursive search and modify the file in each fo the subfolders off the root of the website. Can anyone spend some time tonight and assist me on this? I'd really appreciate it, and I'd buy you virtual beer for your help! :-)

So, this is my code so far:

$search = FileFindFirstFile("index.htm")

If $search = -1 Then

MsgBox(0, "Error", "No files/directories matched the search pattern")

Exit

EndIf

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

StringReplace("if (ver == 4)", "", "if (ver >= 4)")

WEnd

; Close the search handle

FileClose($search)

try this... currently the starting directory is the root of c: (leave backslash off when you change path because it's added in other places in the code), files changed are first backed up with .old extension.

untested

$tosearch = "C:"
$output = FileOpen("c:\filelist.txt",2)
$start = TimerInit()
RecFileSearch($tosearch)
FileClose($output)
MsgBox(0,"done","it only took " & int(TimerDiff($start)/1000) & " seconds")
Func RecFileSearch($current)
    Local $search = FileFindFirstFile($current & "\*.*")
    While 1
    Dim $file = FileFindNextFile($search)
    If @error Or StringLen($file)<1 Then ExitLoop
    If $file = "index.html" Then Fixit($current & "\" & $file)
    If StringInStr(FileGetAttrib($current & "\" & $file),"D") And ( $file <> "." Or $file <> ".." ) Then RecFileSearch($current & "\" & $file)
    WEnd
    FileClose($search)
EndFunc

Func Fixit($thefile)
    $content = FileRead($thefile,FileGetSize($thefile))
    StringReplace($content,"if (ver == 4)","if (ver >= 4)")
    FileMove($thefile,StringLeft($thefile,StringLen($thefile)-3) & "old")
    FileWrite($thefile,$content)
EndFunc

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Here's a version of what you're asking for using Larry's UDF, with some cheating and brute-forceness (i.e. no error checking). Compile it and place it in the root of the tree to be searched.

; sfunkx1 Search & Replace utility

#include <larryUDF.au3>;or whatever you've named it.

Dim $search[30000];assume a large number of files could be found

$search = _FileSearch("index.html",1)

If $search[0] = 0 Then Exit

For $i = 1 to $search[0]
    Run("notepad.exe " & $search[$i], "", @SW_HIDE)
    WinWaitActive("index.html - Notepad")
    Send("^h")
    WinWaitActive("Replace")
    Send("if (ver == 4){TAB}")
    Send("if (ver >= 4){TAB 4}")
    Send("{ENTER}")
    WinActivate("index.html - Notepad")
    Send("^s")
    Send("!f")
    Send("x")
Next

Exit

Yeah, I used Send(), which is cheating - but if it works, use it!


(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]

Share this post


Link to post
Share on other sites

Here's a version of what you're asking for using Larry's UDF, with some cheating and brute-forceness (i.e. no error checking). Compile it and place it in the root of the tree to be searched.

; sfunkx1 Search & Replace utility

#include <larryUDF.au3>;or whatever you've named it.

Dim $search[30000];assume a large number of files could be found

$search = _FileSearch("index.html",1)

If $search[0] = 0 Then Exit

For $i = 1 to $search[0]
    Run("notepad.exe " & $search[$i], "", @SW_HIDE)
    WinWaitActive("index.html - Notepad")
    Send("^h")
    WinWaitActive("Replace")
    Send("if (ver == 4){TAB}")
    Send("if (ver >= 4){TAB 4}")
    Send("{ENTER}")
    WinActivate("index.html - Notepad")
    Send("^s")
    Send("!f")
    Send("x")
Next

Exit

Yeah, I used Send(), which is cheating - but if it works, use it!

Welcome to the forum!

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Welcome to the forum!

Thanks!

By the way, sfunk1x, I put in a search filename of index.html, so you'll want to chop off the "L" if you decide to use my version for your project.


(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0