Set fso = CreateObject("Scripting.FileSystemObject")'指定可移动磁盘,并判断是否有效
bDrive = inputBox("输入可移动磁盘盘符","","如:C")
if fso.DriveExists(bDrive)=false then
Msgbox("您输入的驱动器不存在!")
Wscript.Quit
else
bDrive = bDrive &":\"
end if'指定要移动的目录,并判断是否有效
mPath = inputBox("输入要移动文件的目录","","如:D:\资源")
if fso.FolderExists(mPath)=false then
Msgbox("您输入的目录不存在!")
Wscript.Quit
end if'资源文件定位
'原理:判断可移动磁盘每个目录下的第一个文件是否和当前文件Ext后缀吻合.
Function GetExtFolder(SourceExt)
BackPath = ""
Set GFC = fso.GetFolder(bDrive).SubFolders
For Each EF in GFC
if Instr(LCase(EF),"recycle")=0 then
Set GFS = fso.GetFolder(EF).Files
For Each FS in GFS
sTempExt = UCase(fso.GetExtensionName(FS))
if sTempExt=SourceExt then
BackPath = EF
end if
Exit For
Next
if BackPath<>"" then
Exit For
end if
end if
Next
GetExtFolder = BackPath
End Function'取得目录资源
ShowText = ""
Set AGFC = fso.GetFolder(mPath).Files
For Each AEF in AGFC
TempExt = UCase(fso.GetExtensionName(AEF))
TempName = fso.GetFileName(AEF)
ParentPath = GetExtFolder(TempExt)
if ParentPath<>"" then
On Error Resume Next
fso.MoveFile AEF,ParentPath
if Err then
Err.Clear
ShowText = ShowText &VbCrLf &TempName &"移动失败XXX> " &ParentPath
else
ShowText = ShowText &VbCrLf &TempName &"移动成功===> " &ParentPath
end if
end if
Next
Msgbox ShowText 直接保存成vbs文件即可,需要手工输入移动磁盘驱动符,手动输入要移动文件的目录自动判断目标驱动器目录内的文件类型和准备移动的文件类型,符合-移动,不符-继续判断下一个。下次不要提这种问题了,很麻烦,没人喜欢写。######需要注意的是,如果目标目录为只读,就会移动失败。######总也不用vb都忘了'fso组件
Set fso = CreateObject("Scripting.FileSystemObject")'指定可移动磁盘,并判断是否有效
bDrive = inputBox("输入可移动磁盘盘符","","如:C")
if fso.DriveExists(bDrive)=false then
Msgbox("您输入的驱动器不存在!")
Wscript.Quit
else
bDrive = bDrive &":\"
end if'指定要移动的目录,并判断是否有效
mPath = inputBox("输入要移动文件的目录","","如:D:\资源")
if fso.FolderExists(mPath)=false then
Msgbox("您输入的目录不存在!")
Wscript.Quit
end if'资源文件定位
'原理:判断可移动磁盘每个目录下的第一个文件是否和当前文件Ext后缀吻合.
Function GetExtFolder(SourceExt)
BackPath = ""
Set GFC = fso.GetFolder(bDrive).SubFolders
For Each EF in GFC
if Instr(LCase(EF),"recycle")=0 then
Set GFS = fso.GetFolder(EF).Files
For Each FS in GFS
sTempExt = UCase(fso.GetExtensionName(FS))
if sTempExt=SourceExt then
BackPath = EF
end if
Exit For
Next
if BackPath<>"" then
Exit For
end if
end if
Next
GetExtFolder = BackPath
End Function'取得目录资源
ShowText = ""
Set AGFC = fso.GetFolder(mPath).Files
For Each AEF in AGFC
TempExt = UCase(fso.GetExtensionName(AEF))
TempName = fso.GetFileName(AEF)
ParentPath = GetExtFolder(TempExt)
if ParentPath<>"" then
On Error Resume Next
fso.MoveFile AEF,ParentPath &"\" &TempName
if Err then
ShowText = ShowText &VbCrLf &TempName &"移动失败:" &AEF &Err.description &"XXX> " &ParentPath
Err.Clear
else
ShowText = ShowText &VbCrLf &TempName &"移动成功===> " &ParentPath
end if
end if
Next
Msgbox ShowText
分类: 电脑/网络 >>程序设计 >>其他编程语言问题描述:
我的工作每天都要重复 *** 作备份一个文件夹,我想用.vbs文件编辑一段小代码实现,请问需要怎么样实现?
例如: 把c:\abc.vbs
拷贝到 D:\
请问怎么编辑要源码.
解析:
建议用批处理文件完成该任务,效率比VBS高。
例如: 把c:\abc.vbs
拷贝到 D:\
copy c:\abc.vbs D:\
用fso的movefile会出现权限不足的情况 那样代码不能执行 所以 用copyfile 再deletefile 避免了不必要的麻烦即
Set fso=CreateObject("scripting.filesystemobject")
fso.MoveFile "文件路径","目标"
这个并不好用
用
Set fso=CreateObject("scripting.filesystemobject")
fso.CopyFile "文件路径","目标"
fso.DeleteFile "文件路径"
不要太执着了不是没有而是movefile十分不好用 当初我也不想这样用。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)