Compare 2 files by name and delete those files where there is no pair. Topic is solved
Compare 2 files by name and delete those files where there is no pair.
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.
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.
Re: Compare 2 files by name and delete those files where there is no pair.
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
}
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: Compare 2 files by name and delete those files where there is no pair.
What if there is an .ini file with no matching .txt file?
14.3 & 1.3.7
Re: Compare 2 files by name and delete those files where there is no pair.
No problem, simply count all files that are matching OutNameNoExt. If > 1, done.
Re: Compare 2 files by name and delete those files where there is no pair.
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.
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: Compare 2 files by name and delete those files where there is no pair.
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"
}
}
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
Re: Compare 2 files by name and delete those files where there is no pair.
Thank you all very much, everything works as it should.
Re: Compare 2 files by name and delete those files where there is no pair.
Works perfectly in one folder.Smile_ wrote: ↑19 Aug 2022, 12:38Code: 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 }
How to make everything deleted in subfolders too.
Re: Compare 2 files by name and delete those files where there is no pair.
I added
Code: Select all
"Loop, Files, %Dir%\*.* , R"
Re: Compare 2 files by name and delete those files where there is no pair.
That's how I did it, it doesn't delete anything.
What did I do wrong
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
}
Re: Compare 2 files by name and delete those files where there is no pair. Topic is solved
Try this
@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.
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
}
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.
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: Compare 2 files by name and delete those files where there is no pair.
@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
Re: Compare 2 files by name and delete those files where there is no pair.
@Smile_
Thank you very much, everything is working as it should now.
Yes, you got it right, I need it for any extension.
Thank you very much, everything is working as it should now.
Yes, you got it right, I need it for any extension.