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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

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

08 Jun 2021, 20:26

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
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

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

08 Jun 2021, 20:41

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.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

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

08 Jun 2021, 21:11

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, Gorgrak, Leonardo_Portela and 133 guests