Page 1 of 1

Help with loop for files from a list in folders and copy them

Posted: 08 Jun 2021, 20:26
by maitresin
Hi,

I need to loop from target folder and sub folder for all picture that contain the text from the list.txt
IE: 99-2554, 75-8875
Folders can have picture .jpg, .gif, .tif... named 99-2254-1, 99-2254-2, fullsize-99-2254... that's why I do not put prefix or suffix or file extension. If the file contains my string then it need to be copied to the backup folder.

However, below code does not seem to work. Any idea how I can do this ? Thanks!

Code: Select all

numpad1::
msgbox, start!

Destination:="V:\pictures\20210608-backup"
SearchLocation:="V:\pictures\all"

loop, Read, list.txt
{
 Find_it:=A_loopReadLine
 Loop Files, %SearchLocation%\*.*, R
 {
 IfInString, A_LoopFileName, %Find_It%
 {
 ;msgbox, found it! %A_LoopFileName%
 FileCopy, %A_LoopFileFullPath%,%Destination%\%A_LoopFileName%
 Filedelete, %A_LoopFileFullPath%
 }
 }
}
msgbox, end!
return

Re: Help with loop for files from a list in folders and copy them

Posted: 08 Jun 2021, 20:41
by mikeyww
Your description of this script's effect, "does not seem to work", is vague. What actually happens? Which lines are working properly? Is the file being read the way you expect? How do you know? Which files are being found?

It worked here. My list file:

99-2554
75-8875

You can debug your script by adding some lines that display the variables' values at each step. You can also display the ErrorLevel of the copy. If your goal is to move the file, use FileMove. Check your access rights to the directory. Use AHK to copy a hard-coded file path, to see if the copying works.

Re: Help with loop for files from a list in folders and copy them

Posted: 08 Jun 2021, 21:11
by braunbaer
At first sight, I don't see what is wrong with your script.
But instead of a nested loop, you can use regex, that should be more efficient.
I would use the trim function, so that accidental trailing spaces on a line of the list file don't matter, and also skip empty lines of the list file, otherwise all files would be moved if the list file accidentally contains an empty line. Maybe that is the reason your script does not work like expected.

Code: Select all

numpad1::
msgbox, start!

Destination:="V:\pictures\20210608-backup"
SearchLocation:="V:\pictures\all"

rgx:=""
Loop, Read, list.txt
      (x:=Trim(A_loopReadLine))? rgx.=(rgx? "|" : "") x
Loop, Files, %SearchLocation%\*.*, R
    if regexmatch(A_LoopFileName,rgx)
        FileMove, %A_LoopFileFullPath%,%Destination%\%A_LoopFileName%

msgbox, end!
return