;作者: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
WinGetClass, class, ahk_id %id%
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%
}
StringReplace,out,t,%fatherDir%,%todir%,all
return %out%
}