Compare 2 files by name and delete those files where there is no pair. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Logonius
Posts: 31
Joined: 19 Aug 2022, 08:20

Compare 2 files by name and delete those files where there is no pair.

Post by Logonius » 19 Aug 2022, 08:26

Hello.
Need an example /solution how to compare files in a folder and subfolders, different extensions.
For example, there is test001.ini in the folder, test001.txt and there is test002.txt , but there is no second such test002 file (of any extension) in the folder, it needs to be deleted, leaving those two (a couple). Those files that do not have a pair with the same name are deleted.

I hope I explained it well, I'm new to ahk, please help.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Compare 2 files by name and delete those files where there is no pair.

Post by BoBo » 19 Aug 2022, 12:02

Code: Select all

#SingleInstance, Force

path := "C:\myDir\mySubDir"
Loop, Files,% path "\*.txt"
	{	SplitPath, A_LoopFileFullPath,,,, OutNameNoExt
		res .=  !FileExist(path "\" OutNameNoExt ".ini") ? delete(A_LoopFileFullPath) "`n" : ""
	}
MsgBox % RTrim(res, "`n")
	
delete(file) {
	FileDelete,% file
	return file
	}
Not tested.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Compare 2 files by name and delete those files where there is no pair.

Post by flyingDman » 19 Aug 2022, 12:11

What if there is an .ini file with no matching .txt file?
14.3 & 1.3.7

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Compare 2 files by name and delete those files where there is no pair.

Post by BoBo » 19 Aug 2022, 12:30

No problem, simply count all files that are matching OutNameNoExt. If > 1, done.

User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Compare 2 files by name and delete those files where there is no pair.

Post by Smile_ » 19 Aug 2022, 12:38

Code: Select all

Dir := "C:\TestFolder"
Loop, Files, %Dir%\*.*
{
    SplitPath, % A_LoopFileFullPath,,,, OutNameNoExt
    I := 0
    Loop, Files, %Dir%\%OutNameNoExt%.*
        If (++I = 2)
        	Break
    If (I = 1)
        FileRecycle, % A_LoopFileFullPath
}
Last edited by Smile_ on 20 Aug 2022, 11:49, edited 2 times in total.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Compare 2 files by name and delete those files where there is no pair.

Post by flyingDman » 19 Aug 2022, 14:59

flyingDman wrote:
19 Aug 2022, 12:11
What if there is an .ini file with no matching .txt file?
That was a question to the OP, but a misread, He states it clearly.
If you loop through only the .txt files, won't you miss the ".ini" singletons?
I came up with this. It will only delete a .ini file or a .txt file unless they appear in pairs.

Code: Select all

dir := A_ScriptDir "\test"
Loop, Files, %dir%\*.*
	{
	SplitPath, % A_LoopFileFullPath,,,, flpttrn
	if !(fileexist(dir "\" flpttrn . ".ini") and fileexist(dir "\" flpttrn . ".txt"))
		{
		FileRecycle, % dir "\" flpttrn . ".ini"
		FileRecycle, % dir "\" flpttrn . ".txt"
		}
	}
@Smile_ should it not be If (I = 1)?. It then works but it also deletes single files that have an extension other than .ini or .txt.

Edit: saw you got it... but it still deletes single files that have an extension other than .ini or .txt.
14.3 & 1.3.7

Logonius
Posts: 31
Joined: 19 Aug 2022, 08:20

Re: Compare 2 files by name and delete those files where there is no pair.

Post by Logonius » 19 Aug 2022, 18:49

Thank you all very much, everything works as it should.

Logonius
Posts: 31
Joined: 19 Aug 2022, 08:20

Re: Compare 2 files by name and delete those files where there is no pair.

Post by Logonius » 19 Aug 2022, 19:02

Smile_ wrote:
19 Aug 2022, 12:38

Code: Select all

Dir := "C\TestFolder"
Loop, Files, %Dir%\*.*
{
    SplitPath, % A_LoopFileFullPath,,,, OutNameNoExt
    I := 0
    Loop, Files, %Dir%\%OutNameNoExt%.*
        If (++I = 2)
        	Break
    If (I = 1)
        FileRecycle, % A_LoopFileFullPath
}
Works perfectly in one folder.
How to make everything deleted in subfolders too.


Logonius
Posts: 31
Joined: 19 Aug 2022, 08:20

Re: Compare 2 files by name and delete those files where there is no pair.

Post by Logonius » 20 Aug 2022, 00:04

I added

Code: Select all

"Loop, Files, %Dir%\*.* , R"
but it doesn't work, why?

Logonius
Posts: 31
Joined: 19 Aug 2022, 08:20

Re: Compare 2 files by name and delete those files where there is no pair.

Post by Logonius » 20 Aug 2022, 01:56

That's how I did it, it doesn't delete anything.
What did I do wrong

Code: Select all

Dir := arw1
Loop Files, %Dir%\*.*,r
{
    SplitPath, % A_LoopFileFullPath,,,, OutNameNoExt
    I := 0
    Loop Files, %Dir%\%OutNameNoExt%.*,r
        If (++I = 2)
        	Break
    If (I = 1)
        FileRecycle, % A_LoopFileFullPath
}

User avatar
Smile_
Posts: 858
Joined: 03 May 2020, 00:51

Re: Compare 2 files by name and delete those files where there is no pair.  Topic is solved

Post by Smile_ » 20 Aug 2022, 12:01

Try this

Code: Select all

Dir := "C:\TestFolder"
Loop, Files, %Dir%\*.*, R
{
    SplitPath, % A_LoopFileFullPath,, OutDir,, OutNameNoExt
    I := 0
    Loop, Files, %OutDir%\%OutNameNoExt%.*
    {
        If (++I = 2)
            Break
    }
    If (I = 1)
        FileRecycle, % A_LoopFileFullPath
}
@flyingDman
Sorry for the late responding, and thanks for your note!, It was a typo, also I know it does work on all extensions, if the OP wants specially for .ini and .txt files then I understood it wrong.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Compare 2 files by name and delete those files where there is no pair.

Post by flyingDman » 20 Aug 2022, 12:27

@Smile_ When I read "of any extension" i assumed (wrongly I guess) that he meant either .ini or.txt, but he I guess any extension. So your script does just that.
14.3 & 1.3.7

Logonius
Posts: 31
Joined: 19 Aug 2022, 08:20

Re: Compare 2 files by name and delete those files where there is no pair.

Post by Logonius » 20 Aug 2022, 18:41

@Smile_

Thank you very much, everything is working as it should now.
Yes, you got it right, I need it for any extension.

Post Reply

Return to “Ask for Help (v1)”