Dionysis Posted March 5, 2019 Share Posted March 5, 2019 (edited) Hello world! I'm want to iterate through a directory and move some files to a subdirectory, something like: #include <File.au3> #include <FileConstants.au3> $path = "C:\someFolder\" $sub = "SubDir\" $hSearch = FileFindFirstFile($path & "*.txt") If $hSearch = -1 Then ConsoleWrite("Oops! No files found..." & @CRLF) FileChangeDir($path) While 1 $filename = FileFindNextFile($hSearch) If @error Then ExitLoop ;no more files If StringLeft(FileRead($filename),3) = "Yes" Then ConsoleWrite('Moving file "' & $filename & '" to ' & $path & $sub & '.' & @CRLF) FileMove($filename, ".\" & $sub) If @error Then ConsoleWrite("Cannot Move File!") EndIf Wend ConsoleWrite("Operation Complete!") Does anyone know if moving or deleting a file from the directory I'm iterating through can cause problems to FileFindNextFile? (like skipping files or sth) Looking up FileFindNextFile in the helpfile didn't helped much, and looking up FindFirstFileA/FindFirstFileW in WinAPI docs gave me no clue either. Edited March 6, 2019 by Dionysis Solved Topic Link to comment Share on other sites More sharing options...
Subz Posted March 5, 2019 Share Posted March 5, 2019 Have you tried using _FileListToArrayRec? Link to comment Share on other sites More sharing options...
Dionysis Posted March 5, 2019 Author Share Posted March 5, 2019 54 minutes ago, Subz said: Have you tried using _FileListToArrayRec? I could use _FileListToArray($path,"*.txt",1) , but this is a folder with ~22000 files and I want moved ~5000 of them. So creating an array doesn't seem very optimal. The script without the FileMove works as expected using FileFindFirstFile, but I haven't tried to run it with FileMove added because if it's not working correctly there will be a mess. Link to comment Share on other sites More sharing options...
abberration Posted March 5, 2019 Share Posted March 5, 2019 I made two folders on my desktop and got this code to work: #include <File.au3> $path = @DesktopDir & "\test\" $path2 = @DesktopDir & "\test2\" $hSearch = FileFindFirstFile($path & "yes*.txt") If $hSearch = -1 Then ConsoleWrite("Oops! No files found..." & @CRLF) FileChangeDir($path) While 1 $filename = FileFindNextFile($hSearch) If @error Then ExitLoop ;no more files If StringLeft($filename, 3) = "yes" Then ConsoleWrite('Moving file "' & $filename & '" to ' & $path & $path2 & '.' & @CRLF) FileMove($filename, $path2) If @error Then ConsoleWrite("Cannot Move File!") EndIf Wend ConsoleWrite("Operation Complete!") Dionysis and Fr33b0w 1 1 Easy MP3 | Software Installer | Password Manager Link to comment Share on other sites More sharing options...
Dionysis Posted March 6, 2019 Author Share Posted March 6, 2019 12 hours ago, abberration said: I made two folders on my desktop and got this code to work: #include <File.au3> $path = @DesktopDir & "\test\" $path2 = @DesktopDir & "\test2\" $hSearch = FileFindFirstFile($path & "yes*.txt") If $hSearch = -1 Then ConsoleWrite("Oops! No files found..." & @CRLF) FileChangeDir($path) While 1 $filename = FileFindNextFile($hSearch) If @error Then ExitLoop ;no more files If StringLeft($filename, 3) = "yes" Then ConsoleWrite('Moving file "' & $filename & '" to ' & $path & $path2 & '.' & @CRLF) FileMove($filename, $path2) If @error Then ConsoleWrite("Cannot Move File!") EndIf Wend ConsoleWrite("Operation Complete!") Thanks! abberration 1 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