xcaliber13 Posted March 23, 2017 Share Posted March 23, 2017 Hello Everyone, It has been awhile since I needed some help but I am not making any headway on this. What I am trying to do is search all file in a folder for a string. If string is found move that file to another folder. Can anyone please point out why my script completes with no errors but does not move any files. Yes I do know that some of the file in the folder do have the string in them #include <file.au3> #include <Array.au3> #include <String.au3> #include <StringConstants.au3> #include <Date.au3> #include <FileConstants.au3> Global $sPath = "C:\Test" Global $mPath = "C:\C:\PRPATemp" Global $sFolder = _FileListToArray($sPath, Default, $FLTA_FILES) ;_ArrayDisplay($sFolder) For $i = 1 to $sFolder[0] Local $sFileOpen = FileRead($sFolder[$i]) If StringInStr($sFileOpen, "|F.MAM|") Then FileMove($sFolder[$i], $mPath) EndIF Next Thank you Link to comment Share on other sites More sharing options...
Jfish Posted March 23, 2017 Share Posted March 23, 2017 (edited) Is c:\c:\ a valid path in your $mPath variable? Edit: if you add some debugging you can see where the error occurs. Edited March 23, 2017 by Jfish Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
xcaliber13 Posted March 23, 2017 Author Share Posted March 23, 2017 (edited) Jfish, Thank you for the reply. I did fix that typo so now the paths are: C:\Test\ C:\PRPATemp\ But still the same issue no files moved. I am better at VBS scripting and wrote a quick script in vbs that does work. I am just trying to move all of my scripting to AutoIt. Edited March 23, 2017 by xcaliber13 Link to comment Share on other sites More sharing options...
Jfish Posted March 23, 2017 Share Posted March 23, 2017 I suggest you add some error checking. Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted March 24, 2017 Share Posted March 24, 2017 (edited) FileRead() is probably getting just your filename and not the full path to the file. Also note that the path must already exist to move without forcing the create directory options or using DirCreate() prior to the move. Edited March 24, 2017 by ViciousXUSMC Link to comment Share on other sites More sharing options...
taylansan Posted March 24, 2017 Share Posted March 24, 2017 What I see is; your "FileRead($sFolder[$i])" only read the string in the array which is the file name only. But you are searching this file in another location ($sPath). That's why you cannot find the string you are searching, probably because that file even cannot be reached. If you file read without mentioning the source path, just like you did, then be sure the file is in the same directory with your script. For $i = 1 to $sFolder[0] Local $sPathAndFileName = $sPath & "\" & $sFolder[$i] Local $sFileRead = FileRead($sPathAndFileName) If StringInStr($sFileRead, "|F.MAM|") Then ConsoleWrite("Moving the file: " & $sFolder[$i] & @CRLF) FileMove($sPathAndFileName, $mPath) Else ConsoleWrite("Not found in: " & $sFolder[$i] & @CRLF) EndIf Next TY. Link to comment Share on other sites More sharing options...
Subz Posted March 25, 2017 Share Posted March 25, 2017 You could just change Global $sFolder = _FileListToArray($sPath, Default, $FLTA_FILES) To Global $sFolder = _FileListToArray($sPath, Default, $FLTA_FILES, True) Link to comment Share on other sites More sharing options...
xcaliber13 Posted March 30, 2017 Author Share Posted March 30, 2017 Ok Guys, Thank you for all your help. With your help I was able to complete the script. Here is what ended up working: #include <file.au3> #include <Array.au3> #include <String.au3> #include <StringConstants.au3> #include <Date.au3> #include <FileConstants.au3> #include <MsgBoxConstants.au3> ;This section searchs for |F.MAM| in each file and if found moves the file Global $mPath = "C:\Test\" Global $mPathMove = "C:\PRPATemp\" Global $mFolder = _FileListToArray($mPath, Default, 0, True) ;_ArrayDisplay($mFolder) For $i = 1 to $mFolder[0] Local $mPathAndFileName = $mFolder[$i] Local $mFileOpen = FileOpen($mPathAndFileName) Local $mFileRead = FileRead($mFileOpen) If StringInStr($mFileRead, "|F.MAM|") Then ConsoleWrite("Moving the file: " & $mFolder[$i] & @CRLF) FileMove($mPathAndFileName, $mPathMove) Else ConsoleWrite("Not found in: " & $mFolder[$i] & @CRLF) EndIf Next Again Thank Link to comment Share on other sites More sharing options...
KickStarter15 Posted April 4, 2017 Share Posted April 4, 2017 @xcaliber13 I have tried your posted code and found some little issue. First, if you search for string "|F.MAM|" twice with the same extension (i.e., ".txt") the code won't perform. Second, when you changed the string to search like "|G.MAM|" but has the same extension used ".txt", the code won't perform. Have you experience these issue in your current coding? Or maybe it's just my testing went wrong. KS15 Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
xcaliber13 Posted April 4, 2017 Author Share Posted April 4, 2017 KickStarter15 I have had no issue with this code. And it maybe because all the files I search do not have an extension (i.e., ".txt") Not sure. But it is working correctly for what I need. Link to comment Share on other sites More sharing options...
KickStarter15 Posted April 5, 2017 Share Posted April 5, 2017 @xcaliber13 well that's good to hear. Anyway, thanks for sharing this code, i was thinking of this before but i could not compile a script to do so. However, I made a little add-on with your code for me to have it more efficient when user used it (if it's okay with you). Here: $aSearch = InputBox("Word to find", "Type you want to search:") $sFileSelectFolder = FileSelectFolder("Locate your path to search", "") If @error Then MsgBox(0, "", "No folder was selected.") Exit EndIf Global $mPath = $sFileSelectFolder $aFilePath = FileSelectFolder("Locate your path to move the file(s):", "") If @error Then MsgBox(0, "", "No folder was selected.") Exit EndIf Global $mPathMove = $aFilePath It's just a user's option what string to search, where to search and where to move the searched files. Thank you again. KS15 Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
InunoTaishou Posted April 5, 2017 Share Posted April 5, 2017 So here's a little bit of a different approach. Instead of getting a list of all the files first what about moving the files as you find them? Modifying the _FileListToArray function a little bit, and supporting RegExp in the search. I'm not very good at RegExp but this works. Also, using RegExp would allow for more than one search in a file (I.e., "\|F.MAM\||\|F.BAM\|" would make |F.MAM| and |F.BAM| be matches in the file) expandcollapse popup#include <Array.au3> _ArrayDisplay(_FileFindStringAndMove(@ScriptDir & "\", @ScriptDir & "\found\", "\|F.MAM\|")) Func _FileFindStringAndMove($sFilePath, $sMoveToPath, $sFindString, $sFilter = "*") Local $sDelimiter = "|", $sFileList = "", $sFileName = "", $sFullPath = "" ; Check parameters for the Default keyword or they meet a certain criteria $sFilePath = StringRegExpReplace($sFilePath, "[\\/]+$", "") & "\" ; Ensure a single trailing backslash $sMoveToPath = StringRegExpReplace($sMoveToPath, "[\\/]+$", "") & "\" ; Ensure a single trailing backslash If $sFilter = Default Then $sFilter = "*" ; Check if the directory exists If (Not FileExists($sFilePath)) Then Return SetError(1, 0, 0) If StringRegExp($sFilter, "[\\/:><\|]|(?s)^\s*$") Then Return SetError(2, 0, 0) ; Create the Move To directory if it doesn't exist If (Not FileExists($sMoveToPath)) Then DirCreate($sMoveToPath) Local $hSearch = FileFindFirstFile($sFilePath & $sFilter) If (@error) Then Return SetError(3, 0, 0) While (True) $sFileName = FileFindNextFile($hSearch) If (@error) Then ExitLoop ; File type is a directory If (@extended = 1) Then ContinueLoop Local $hFile = FileOpen($sFilePath & $sFileName) Local $sFile = FileRead($hFile) FileClose($hFile) ; If the string to search for is in the file If (StringRegExp($sFile, $sFindString)) Then ; Move it FileMove($sFilePath & $sFileName, $sMoveToPath & $sFileName) ; Append to the list $sFileList &= $sDelimiter & $sFilePath & $sFileName EndIf WEnd FileClose($hSearch) ; Return an array of all files that were moved ; I hate having the count in the [0] element >.> Return StringSplit(StringTrimLeft($sFileList, 1), $sDelimiter, $STR_NOCOUNT) EndFunc Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now