比较不同目录中的文件名(减去扩展名),如果与 AutoHotkey 同名则 copy/move

Compare file names (minus extension) in different directories and copy/move if the same name with AutoHotkey

我想比较两个目录之间的文件名(减去扩展名),如果匹配,则将文件从两个目录之一复制(或移动 [tbd])到第三个目录。即

Dir_A 有 a.jpg、b.jpg、c.jpg、d.jpg、e.jpg、f.jpg

Dir_B 有 a.pdf、c.pdf、d.pdf、f.pdf

结果会是

Dir_C 得到 a.jpg、c.jpg、d.jpg、f.jpg

我已经能够使用批处理文件完成此操作,但想了解如何通过 AHK 完成此操作。

批处理文件是:

@Echo Off & SetLocal EnableExtensions
pushd D:\temp
For /F "tokens=*" %%I IN ('dir /a-d /b *.jpg') DO (
    IF EXIST "D:\temp\comp\%%~nI.pdf" move "%%~I" "D:\temp\new\" 
)

经过大量查找,找到 相似 帖子并尝试进行插值,我认为我很接近,但显然遗漏了一些东西。我希望有人能为我阐明这一点。

#NoEnv
SendMode Input

SFolder:="D:\temp\"         ;Source folder
CFolder:="D:\temp\comp"     ;Compare folder
DestDir:="D:\temp\new"      ;where to move files

Loop,
 {
    Loop, %SFolder%*.jpg        ;look for all jpg files
    JpgName = %A_LoopFileName%  ;save the file names to var 
    Loop, %CFolder%*.pdf        ;look for all pdf files
    PdfName = %A_LoopFileName%  ;save the file names to var

    JpgCompare:=Trim(JpgName,".jpg")    ;remove the files .ext
    PdfCompare:=Trim(PdfName,".pdf")    ;remove the files .ext

    If JpgCompare = %PdfCompare%    ;if there are matching file names (minus .ext)
                                    ;in both directories
    {
        FileMove, %JpgName%, %DestDir%  ;move the file.jpg to the "new" directory
    }
    Else
    {}
 }
Esc::
ExitApp

您可以使用 SplitPath to store the jpg file name without its path, dot and extension in a variable (name_no_ext) and check for the existence of a pdf file with the same name in the other directory using FileExist():

SFolder:="D:\temp\"         ;Source folder
CFolder:="D:\temp\comp"     ;Compare folder
DestDir:="D:\temp\new"      ;where to move files

Loop Files, %SFolder%*.jpg        ;look for all jpg files
{
    SplitPath, A_LoopFileName,,,, name_no_ext
    If FileExist(CFolder . "\" . name_no_ext . .pdf)
        FileMove, %A_LoopFileFullPath%, %DestDir%  ;move the file.jpg to the "new" directory
}