Followers 0

# Recursive search

## 13 posts in this topic

I created the following simple code to edit a particular file if it is found. There are a few things that I am trying to do that I am having trouble with.

1. Is there a way for the @UsernName variable to return the username in lowercase instead of all caps?

2. I want the script to look into an infinite number of subdirs and be able to edit the particular file if found.

Any help would be appreciated

Thanks

$file = FileOpen( "temp", 2)$String1= ":ext:"

$String2= "@blahblah.net" ; Check if file opened for reading OK If$file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

FileWriteLine($file,$String1 & @UserName & $String2) FileClose($file)

##### Share on other sites

#2 ·  Posted (edited)

I created the following simple code to edit a particular file if it is found. There are a few things that I am trying to do that I am having trouble with.

1. Is there a way for the @UsernName variable to return the username in lowercase instead of all caps?

2. I want the script to look into an infinite number of subdirs and be able to edit the particular file if found.

Any help would be appreciated

Thanks

$file = FileOpen( "temp", 2)$String1= ":ext:"

$String2= "@blahblah.net" ; Check if file opened for reading OK If$file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

FileWriteLine($file,$String1 & @UserName & $String2) FileClose($file)

Welcome to the forum. In regards to the lower case question, you can use the StringLower function like this:

FileWriteLine($file,$String1 & StringLower(@UserName) & $String2) As for recursive file searching, you can use the FileFindFirst, FileFindNext, FileGetAttrib and FileChangeDir functions together to recursively move through your files. You might want to look at this to see if will fit your needs first. If not, search the forum for the words "recursive" and the above functions as this is something that has been done before. If you're still stuck, post an example of what you have so far and we'll be glad to help you out. Edited by PaulIA Auto3Lib: A library of over 1200 functions for AutoIt #### Share this post ##### Link to post ##### Share on other sites Ive been looking at the recursive scripts that people have written for a few days and I have been getting a bit overwhelmed with the code. Im not sure of what I need, what I dont need or what I need to add. I would appreciate if someone broke down the concept behind the _FileChangeRecursive script so that I could better understand it. I see that once it searches through the files it passes this information onto a worker function (which part is doing this)? Can I create a worker function that edits a designated file while the script searches through the sub dirs? Thanks #### Share this post ##### Link to post ##### Share on other sites #4 · Posted (edited) I think essentially what you need is to find if the file exists, and if it does, get the path for it so you can do whatever you want to it? Here's my recursive search, incase it helps you. ; _filefind('filename.exe', 'c:\optional\path\to start at\')$find = _findfile('wmplayer.exe', @ProgramFilesDir)

If $find Then MsgBox(0, 'Here''s the file...',$find)
Else
MsgBox(0, 'Nope...', 'Couldn''t find the file.')
EndIf

Func _findfile($file,$path = '')
If $path <> '' And StringRight($path, 1) <> '\' Then $path =$path & '\'
If $path = '' Then$path = @HomeDrive & '\'
Local $success = False Local$output = Run(@ComSpec & " /c " & 'dir ' & '"' & $path & '' & '*.*" /b /s', '', @SW_HIDE, 2) While 1$stdout = StdoutRead($output) If @error Then ExitLoop If StringInStr($stdout, '\' & $file) Then ExitLoop WEnd Local$found = StringSplit(StringStripCR($stdout), @LF) For$i = 1 To UBound($found) - 1 If StringRight($found[$i], StringLen($file) + 1) = '\' & $file Then$success = True
ExitLoop
Else
$success = False EndIf Next If$success Then
Return $found[$i]
Else
Return 0
EndIf
EndFunc
Edited by xcal

##### Share on other sites

Basically I want the script to start in C:\temp and the temp folder has about 100 sub dirs. The script needs to search through all these subdirs for a temp file that exists in each subdir and it needs to make an edit to it. I only need a popup that tells me its done editing all of the files.

##### Share on other sites

#6 ·  Posted (edited)

Ok, here's a fixed version. All you have to do is StringSplit the results and run it through a loop, doing what you want to each file. Or, instead of the &= part, you could just stick $found[$i] directly in to an array and work with that. A few possibilities.

; _filefind('filename.ext', 'c:\optional\path\to start at\')

$find = _findfile('test.txt') If$find = '' Then
MsgBox(0, 'Uh oh', 'No files found!')
Else
MsgBox(0, 'File(s) Found...', $find) EndIf Func _findfile($file, $path = '') If$path <> '' And StringRight($path, 1) <> '\' Then$path = $path & '\' If$path = '' Then $path = @HomeDrive & '\' Local$rets = ''
Local $output = Run(@ComSpec & " /c " & 'dir ' & '"' &$path & '' & '*.*" /b /s', '', @SW_HIDE, 2)
While 1
$stdout = StdoutRead($output)
If @error Then ExitLoop
$found = StringSplit(StringStripCR($stdout), @LF)
For $i = 1 To UBound($found) - 1
If StringRight($found[$i], StringLen($file) + 1) = '\' &$file Then $rets &= @LF &$found[$i] Next WEnd Return$rets
EndFunc

edit - minor tweak

Edited by xcal

##### Share on other sites

I posted a recursive search in Example Scripts a few days ago

http://www.autoitscript.com/forum/index.php?showtopic=42049

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

##### Share on other sites

Can you show how you may incorporate StringSplit to open and edit each file within that script?

##### Share on other sites

Who's script are you talking about? If you're talking about mine, you can just do what you want at this line:

If StringRight($found[$i], StringLen($file) + 1) = '\' &$file Then $rets &= @LF &$found[$i] Change it to... If StringRight($found[$i], StringLen($file) + 1) = '\' & $file Then MsgBox(0, '',$found[$i]) That should give you a clue where you can then access the file and do what you want with it. That line may end up looking like... If StringRight($found[$i], StringLen($file) + 1) = '\' & $file Then MyFunc($found[$i]) And you could just remove the Return$rets.

##### Share on other sites

If StringRight($found[$i], StringLen($file) + 1) = '\' &$file Then MyFunc($found[$i])

---Is this where I need to create a custom function that opens each file for $found[$1] ??

Im pretty lost in all of this. Im sorry for my ignorance

##### Share on other sites

#12 ·  Posted (edited)

Yes. $found[$i] (it's an 'i' not a '1') is where the \path\file.ext is being spit out. Did you try replacing that line and running it like I said? I really can't be more specific than this...

; _filefind('filename.ext', 'c:\optional\path\to start at\')

$input = InputBox('My Search', 'Find What file?', 'file.ext', '', 200, 100) If @error Then Exit$find = _findfile($input) Func _findfile($file, $path = '') If$path <> '' And StringRight($path, 1) <> '\' Then$path = $path & '\' If$path = '' Then $path = @HomeDrive & '\' Local$output = Run(@ComSpec & " /c " & 'dir ' & '"' & $path & '' & '*.*" /b /s', '', @SW_HIDE, 2) While 1$stdout = StdoutRead($output) If @error Then ExitLoop$found = StringSplit(StringStripCR($stdout), @LF) For$i = 1 To UBound($found) - 1 If StringRight($found[$i], StringLen($file) + 1) = '\' & $file Then myfunc($found[$i]) ;this is where the files are found inside$found[$i] Next WEnd EndFunc Func myfunc($var)  ;this is where you would do what you want with each file
EndIf
Send(\$var & @LF)
EndFunc
Edited by xcal

##### Share on other sites

Great I finnally got it. Thanks for working with me.