david1337 Posted October 20, 2015 Posted October 20, 2015 Hey guysI'm a little stuck and could need some help.$Dir = "C:\temp\" $username = @UserName Global $Custom[3] $Custom[0] = ("01_List") $Custom[1] = ("02_List") $Custom[2] = ("03_List") For $i = 0 to 3 - 1 $CustomFile = FileOpen($Dir & $Custom[$i] & ".txt", 0) $ReadCustomFile = FileRead($CustomFile) FileClose($CustomFile) If StringRegExp($ReadCustomFile, $username) Then FileCopy($Dir & $Custom[$i] & ".txt", $Dir & "Match") EndIf NextIn C:\temp\ I have:01_List.txt02_List.txt03_List.txt The script will look through all 3 files to find the one containing my username. When it finds that file, it will copy the file to the folder called "Match"All this works fine. But what if I would like it to only identify the files by the first 3 characters? "01_" & "02_" & "03_"This way I could rename the txt files to 01_whatever, 02_somethingElse etc., and it would still work.
TheDcoder Posted October 20, 2015 Posted October 20, 2015 (edited) $sString = "String" $sFirstThreeChars = StringLeft($sString, 3) ; $sFirstThreeChars contains "Str" Edited October 20, 2015 by TheDcoder EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion
david1337 Posted October 20, 2015 Author Posted October 20, 2015 Thank you for answering I have tried to use StringLeft, but haven't got it to work.Using StringLeft tells the script to read "01_List" as "01_" yes, but that doesn't change the fact that the filecopy will fail, since there is no file called "01_"Or am I missing something obvious?
TheDcoder Posted October 20, 2015 Posted October 20, 2015 #include <File.au3> $aFiles = _FileListToArray($Dir, "???*.txt", $FLTA_FILES) ; Will return an array containing txt files with minimum of 3 chars For $i = 1 To $aFiles[0] If StringLeft($aFiles[$i], 3) = "<Your Matching String Here>" Then ; You code here EndIf Next EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion
david1337 Posted October 20, 2015 Author Posted October 20, 2015 TheDcoderThank you for your time!Ahhhh head explosion... can't get your example implemented into my own script.And why did it change to For $i = 1 To $Custom[0] ?$Dir = "C:\temp\" $username = @UserName Global $Custom[3] $Custom[0] = ("01_List") $Custom[1] = ("02_List") $Custom[2] = ("03_List") #include <File.au3> $Custom = _FileListToArray($Dir, "???*.txt", $FLTA_FILES) ; Will return an array containing txt files with minimum of 3 chars For $i = 1 To $Custom[0] If StringLeft($Custom[$i], 3) = "<What Matching String? The username?>" Then FileCopy($Dir & $Custom[$i] & ".txt", $Dir & "Match") EndIf Next
TheDcoder Posted October 20, 2015 Posted October 20, 2015 And why did it change to For $i = 1 To $Custom[0] ?$Custom[0] contains the number of files, so no need to Ubound $Dir = "C:\temp\" $username = @UserName Global $Custom[3] $Custom[0] = ("01_List") $Custom[1] = ("02_List") $Custom[2] = ("03_List") #include <File.au3> $Custom = _FileListToArray($Dir, "???*.txt", $FLTA_FILES) ; Will return an array containing txt files with minimum of 3 chars For $i = 1 To $Custom[0] If StringLeft($Custom[$i], 3) = $username Then FileCopy($Dir & $Custom[$i], $Dir & "Match") EndIf NextYou appended the txt extension, there is no need for it EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion
david1337 Posted October 20, 2015 Author Posted October 20, 2015 Thanks mate!I got it working, thanks to you Much appreciated!$Dir = "C:\temp\" $username = @UserName Global $Custom[3] $Custom[0] = ("01_List") $Custom[1] = ("02_List") $Custom[2] = ("03_List") #include <File.au3> $Custom = _FileListToArray($Dir, "???*.txt", $FLTA_FILES) ; Will return an array containing txt files with minimum of 3 chars For $i = 1 To $Custom[0] $CustomFile = FileOpen($Dir & $Custom[$i], 0) $ReadCustomFile = FileRead($CustomFile) FileClose($CustomFile) If StringRegExp($ReadCustomFile, $username) Then FileCopy($Dir & $Custom[$i], $Dir & "Match") EndIf Next
TheDcoder Posted October 20, 2015 Posted October 20, 2015 @david1337 My pleasure! EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion
david1337 Posted October 20, 2015 Author Posted October 20, 2015 Oh shit it's reacting on ANYTHING.txt now as long as the textfile has my username in it!
TheDcoder Posted October 20, 2015 Posted October 20, 2015 Does it? It doesn't for me... BTW I modified the filter, try this:$aFiles = _FileListToArray($sLocation, "*_*.txt", $FLTA_FILES) ; Will return an array containing txt files with an underscore in its name david1337 1 EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion
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