How to read a list and copy files? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

How to read a list and copy files?

03 Jun 2019, 16:54

I am trying to read a list of file names within a .txt file then search and copy the files.
I came up with this (see below) but It's still not working. It copies more than I intended it to. For example, If I have two files names in a list, It will copy 30 files with similar names.
I only want to copy exact names.

For example, if my list of file names within the txt file contains...
Redhouse.jpg
Redcar.jpg
RedBird.jpg

The code below will copy everything that starts with "Red". And the result will be....
Redhouse.jpg
Redcar.jpg
RedBird.jpg
RedCat.jpg
redplant.jpg

Note: I did not want RedCat.jpg or redplant.jpg to be copied.

How can i only copy the files in the list?

Here are the folder locations.
%ListFileLocation% = .txt file of file names
%SearchLocation% = Folder to search within for the file names
%Destination% = Folder where to paste the files

Question: how can I copy the files that exactly match the list?

Code: Select all

loop, Read, %ListFileLocation%
{
    Find_it:=A_loopReadLine
	
	Loop,%SearchLocation%\*.*
	{
	IfInString, A_LoopFileName, %Find_It%
	FileCopy, %A_LoopFileFullPath%,%Destination%\%A_LoopFileName%
	}
}
return
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: How to read a list and copy files?  Topic is solved

03 Jun 2019, 22:20

Code: Select all

Loop, Read, %ListFileLocation%
     FileCopy, %SearchLocation%\%A_LoopReadline%.*, %Destination% ; You can replace * with a file extension e.g. jpg , otherwise it will just copy the matched file regardless of the extension
Return
how can I copy the files that exactly match the list?
Your code doesn't match the names exactly because you are using IfInString.
To match it exactly you have to have an full name and see if it's EQUAL to one of the lines listed in your file list.
It's not necessary now though. The above code does exactly what you want.
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: How to read a list and copy files?

04 Jun 2019, 08:48

Cuadrix wrote:
03 Jun 2019, 22:20

Code: Select all

Loop, Read, %ListFileLocation%
     FileCopy, %SearchLocation%\%A_LoopReadline%.*, %Destination% ; You can replace * with a file extension e.g. jpg , otherwise it will just copy the matched file regardless of the extension
Return
how can I copy the files that exactly match the list?
Your code doesn't match the names exactly because you are using IfInString.
To match it exactly you have to have an full name and see if it's EQUAL to one of the lines listed in your file list.
It's not necessary now though. The above code does exactly what you want.
Thank you very much for the explanation and the code :bravo:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mstrauss2021, peter_ahk, Spawnova, william_ahk, zephyrus2706 and 352 guests