;作者:sunwind
;2015年10月27日22:34:33
;功能:
;xcopy的增强版,可以过滤扩展名
;xCopyPlus批量复制某类文件
Gui,+AlwaysOnTop
Gui, Add, Text,,源路径(拖放或输入源目录,多条请换行)
Gui, Add, Edit, vSourceEdit W300 R2
Gui, Add, Text,,目的路径(拖放或输入目的目录,只支持一条)
Gui, Add, Edit, vDestinationEdit W300 R2
Gui, Add, Text,,文件类型(多个类型请用|符号分割))
Gui, Add, Edit, vFileType W300,rar|zip
Gui,Add,Button,gFilterCopy,复制文件
Gui,Add,Button,gStructureCopy,仅复制文件夹结构
Options:="W520 H240"
Title:="带文件夹结构复制某类型文件"
Gui, Show , %Options%, %Title%
return
GuiDropFiles: ; 对拖放提供支持.
SelectedFileName := A_GuiEvent
;获取鼠标下面的控件
MouseGetPos, , , id, control
;~ WinGetTitle, title, ahk_id %id%
WinGetClass, class, ahk_id %id%
;~ ToolTip, ahk_id %id%`nahk_class %class%`n%title%`nControl: %control%
if (control="Edit1")
{
GuiControl,, SourceEdit, %SelectedFileName% ; 在控件中显示文本.
}
if (control="Edit2")
{
GuiControl,, DestinationEdit, %SelectedFileName% ; 在控件中显示文本.
}
return
FilterCopy:
Gui,-AlwaysOnTop
ControlGetText,SourcePath,Edit1
ControlGetText,DestinationPath,Edit2
ControlGetText,myext,Edit3
tar_ext=i).(%myext%)$ ;定义你的文件类型
;~ 只能拖动文件夹到路径和目的路径
;~ 源路径支持同时拖入多个文件夹、目的路径只支持一个
response:=
Loop, parse, SourcePath, `n, `r ; 在 `r 之前指定 `n, 这样可以同时支持对 Windows 和 Unix 文件的解析.
{
SourceFolder:=Trim(A_LoopField, OmitChars ="`n")
SourceFolder := RTrim(SourceFolder,"")
DestinationFolder := RTrim(DestinationPath,"") ;remove a trailing backslash, if present
if not InStr(FileExist(SourceFolder), "D")
{
MsgBox 不存在源路径
}
if not InStr(FileExist(DestinationFolder), "D")
{
FileCreateDir,%DestinationFolder%
MsgBox 创建了【%DestinationFolder%】目录
}
response .= FilterCopy(SourceFolder,DestinationFolder,tar_ext) . "`n"
}
MsgBox %myext%文件复制完成!`n%response%
Run %DestinationFolder%
return
StructureCopy:
Gui,-AlwaysOnTop
ControlGetText,SourcePath,Edit1
ControlGetText,DestinationPath,Edit2
DestinationFolder := RTrim(DestinationPath,"") ;remove a trailing backslash, if present
if not InStr(FileExist(DestinationFolder), "D")
{
FileCreateDir,%DestinationFolder%
}
arr:=StrSplit(SourcePath,"`r`n")
res:=
for key,SourcePath in arr
{
SourceFolder := RTrim(SourcePath,"")
if not InStr(FileExist(SourceFolder), "D")
{
MsgBox 不存在源路径
}
res.=StructureCopy(SourceFolder,DestinationFolder)
}
MsgBox 文件夹结构复制完成`n%res% ;输出结果
Run %DestinationFolder%
return
Escape:
GuiClose: ; 用户关闭了窗口.
ExitApp
StructureCopy(fromdir,todir)
{
t:=
Loop,%fromdir%*.*,2,1 ;2只是文件夹
{
t.=A_LoopFileFullPath . "`n"
SplitPath,fromdir,,fatherDir
StringReplace,DirName,A_LoopFileFullPath,%fatherDir%,%todir%,all
FileCreateDir, %DirName%
}
StringReplace,out,t,%fatherDir%,%todir%,all
return %out%
}
FilterCopy(fromdir,todir,tar_ext)
{
StructureCopy(fromdir,todir)
t:=
Loop,%fromdir%*.*,1,1 ; 第一个参数1获取所有匹配通配符模式的文件和文件夹.
{
If (! RegExMatch(A_LoopFileFullPath, tar_ext)) ;在此过滤
continue
t.=A_LoopFileFullPath . "`n"
SplitPath,fromdir,,fatherDir
StringReplace,DirName,A_LoopFileFullPath,%fatherDir%,%todir%,all
FileCopy,%A_LoopFileFullPath%,%DirName%
;~ ErrorCount += ErrorLevel
;~ if ErrorLevel ; Report each problem folder by name.
;~ MsgBox Could not copy %A_LoopFileFullPath% into %DestinationFolder%.
}
StringReplace,out,t,%fatherDir%,%todir%,all
;~ return ErrorCount
return %out%
}
;~ CopyFilesAndFolders(SourcePattern, DestinationFolder, DoOverwrite = false)
; Copies all files and folders matching SourcePattern into the folder named DestinationFolder and
; returns the number of files/folders that could not be copied.
本文说明(xCopyPlus 批量复制工具)
这是一款基于 xcopy 增强的 AutoHotkey(v1)批量复制小工具,核心用途有两个:
- 带文件夹结构批量复制某类文件:通过
FilterCopy按扩展名过滤(默认 rar|zip,可用|分隔多个类型),同时保留源目录的层级结构。 - 仅复制文件夹结构:通过
StructureCopy只重建目录骨架,不复制文件,适合先建目录再手动填充的场景。
使用方法:运行后在弹出的 GUI 中输入(或直接拖放)源路径与目的路径,可多行输入多个源目录,目的目录仅支持一条;随后点击“复制文件”或“仅复制文件夹结构”即可。
注意事项:本脚本为 AHK v1 语法。若需迁移到 v2,注意命令式语法与变量 %var% 引用的差异(v2 多为函数调用与表达式),如 MsgBox、ControlGetText、StringReplace 等在 v2 中需改为函数式写法。

