Jump to content

Moving Files while using FileFindNextFile [SOLVED]


Recommended Posts

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 by Dionysis
Solved Topic
Link to comment
Share on other sites

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

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!")

 

Link to comment
Share on other sites

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!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...