VBS获取指定目录下最新文件拷贝然后以当前日期命名 推荐
2016-12-20 19:50
686 查看
VBS获取指定目录下最新文件拷贝然后以当前日期命名
近期有个需求,想对数据的备份目录中数据再次远程拷贝到计算机上,所以我们就通过vbs脚本将备份目录下的最新文件拷贝到指定目录,然后以当前的系统日期命名,总结为下:
我们在D盘下的data目录下有系统的备份文件,然后以日期命名,我们想通过获取文件的最后 一次修改时间进行获取最新文件,然后进行拷贝
然后拷贝到指定目录中,我们也可以拷贝到远程计算机
我们在本地进行测试,在D盘下的databackup中即可
代码送上:
如果一个脚本想实现不同目录的数据拷贝的话,我们也可以通过以下方式写,我们有两种,第一种不正规的写法
附件:http://down.51cto.com/data/2368504
近期有个需求,想对数据的备份目录中数据再次远程拷贝到计算机上,所以我们就通过vbs脚本将备份目录下的最新文件拷贝到指定目录,然后以当前的系统日期命名,总结为下:
我们在D盘下的data目录下有系统的备份文件,然后以日期命名,我们想通过获取文件的最后 一次修改时间进行获取最新文件,然后进行拷贝
然后拷贝到指定目录中,我们也可以拷贝到远程计算机
我们在本地进行测试,在D盘下的databackup中即可
代码送上:
sourcefilespath="D:\data" 'desfilepath="\\x.x.x.x\Backup\DataBackup"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\" desfilepath="D:\dbbackup\"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\" Set dic=CreateObject("Scripting.Dictionary") Set fso=CreateObject("Scripting.FileSystemObject") If Not fso.FolderExists(desfilepath) Then fso.CreateFolder desfilepath End If backFolderPath=GetLastModify(sourcefilespath) fso.CopyFile backFolderPath,desfilepath&fso.GetFileName(backFolderPath) msgbox "Finish" ''移动文件 Function MoveFiles(yPath,sPath) On Error Resume Next Dim folder,Files,File,subFolder,subFolders Set fso = createobject("scripting.FileSystemObject") Set Folder = fso.getFolder(yPath) Set Files = Folder.Files 'msgbox yPath & sPaht For Each File In Files fso.MoveFile File,sPath&"\" 'msgbox File Next Set subFolder = Folder.SubFolders For Each subFolders In subFolder folderTemp = Split(subFolders,"\") FolderName=FolderTemp(ubound(folderTemp)) fso.createFolder(sPath&"\"&FolderName) MoveFiles subFolders,sPath&"\"&FolderName&"\" fso.DeleteFolder subFolders Next End Function Function GetLastModify(folder) Set fso = createobject("scripting.FileSystemObject") Set Folder=fso.getFolder(folder) Set subFolders = Folder.Files nowdate= Now For Each subFolder In subFolders dic.Add datediff("s",subFolder.DateLastModified,nowdate),subFolder.path Next NumArray=dic.Keys bn = NumArray(0) For Each nn In NumArray If bn >= nn Then bn = nn End If Next GetLastModify = dic.Item(bn) End Function Function fSortArray(aSortThisArray) Dim oArrayList, iElement Set oArrayList = CreateObject( "System.Collections.ArrayList" ) For iElement = 0 To UBound(aSortThisArray) oArrayList.Add aSortThisArray(iElement) Next oArrayList.Sort set fSortArray = oArrayList End Function ''拷贝文件 Function CopyFiles(yPath,sPath) On Error Resume Next Dim folder,Files,File,subFolder,subFolders Set fso = createobject("scripting.FileSystemObject") Set Folder = fso.getFolder(yPath) Set Files = Folder.Files 'msgbox yPath & sPaht For Each File In Files fso.copyFile File,sPath&"\" 'msgbox File Next Set subFolder = Folder.SubFolders For Each subFolders In subFolder folderTemp = Split(subFolders,"\") FolderName=FolderTemp(ubound(folderTemp)) fso.createFolder(sPath&"\"&FolderName) CopyFiles subFolders,sPath&"\"&FolderName&"\" 'fso.DeleteFolder subFolders Next End Function我们执行脚本后,发现以日期命名,然后将最新的数据拷贝到了这个文件夹路劲下
如果一个脚本想实现不同目录的数据拷贝的话,我们也可以通过以下方式写,我们有两种,第一种不正规的写法
sourcefilespath="D:\test" 'desfilepath="\\10.12.0.51\Backup\DataBackup"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\" desfilepath="e:\data1\"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\" Set dic=CreateObject("Scripting.Dictionary") Set fso=CreateObject("Scripting.FileSystemObject") If Not fso.FolderExists(desfilepath) Then fso.CreateFolder desfilepath End If backFolderPath=GetLastModify(sourcefilespath) fso.CopyFile backFolderPath,desfilepath&fso.GetFileName(backFolderPath) msgbox "Finish" sourcefilespath2="D:\test2" 'desfilepath="\\10.12.0.51\Backup\DataBackup"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\" desfilepath2="e:\data2\"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\" Set dic=CreateObject("Scripting.Dictionary") Set fso=CreateObject("Scripting.FileSystemObject") If Not fso.FolderExists(desfilepath2) Then fso.CreateFolder desfilepath2 End If backFolderPath=GetLastModify(sourcefilespath2) fso.CopyFile backFolderPath,desfilepath2&fso.GetFileName(backFolderPath) msgbox "Finish" ''移动文件 Function MoveFiles(yPath,sPath) On Error Resume Next Dim folder,Files,File,subFolder,subFolders Set fso = createobject("scripting.FileSystemObject") Set Folder = fso.getFolder(yPath) Set Files = Folder.Files 'msgbox yPath & sPaht For Each File In Files fso.MoveFile File,sPath&"\" 'msgbox File Next Set subFolder = Folder.SubFolders For Each subFolders In subFolder folderTemp = Split(subFolders,"\") FolderName=FolderTemp(ubound(folderTemp)) fso.createFolder(sPath&"\"&FolderName) MoveFiles subFolders,sPath&"\"&FolderName&"\" fso.DeleteFolder subFolders Next End Function Function GetLastModify(folder) Set fso = createobject("scripting.FileSystemObject") Set Folder=fso.getFolder(folder) Set subFolders = Folder.Files nowdate= Now For Each subFolder In subFolders dic.Add datediff("s",subFolder.DateLastModified,nowdate),subFolder.path Next NumArray=dic.Keys bn = NumArray(0) For Each nn In NumArray If bn >= nn Then bn = nn End If Next GetLastModify = dic.Item(bn) End Function Function fSortArray(aSortThisArray) Dim oArrayList, iElement Set oArrayList = CreateObject( "System.Collections.ArrayList" ) For iElement = 0 To UBound(aSortThisArray) oArrayList.Add aSortThisArray(iElement) Next oArrayList.Sort set fSortArray = oArrayList End Function ''拷贝文件 Function CopyFiles(yPath,sPath) On Error Resume Next Dim folder,Files,File,subFolder,subFolders Set fso = createobject("scripting.FileSystemObject") Set Folder = fso.getFolder(yPath) Set Files = Folder.Files 'msgbox yPath & sPaht For Each File In Files fso.copyFile File,sPath&"\" 'msgbox File Next Set subFolder = Folder.SubFolders For Each subFolders In subFolder folderTemp = Split(subFolders,"\") FolderName=FolderTemp(ubound(folderTemp)) fso.createFolder(sPath&"\"&FolderName) CopyFiles subFolders,sPath&"\"&FolderName&"\" 'fso.DeleteFolder subFolders Next End Function我们可以看见,如果有多个目录的话,我们把函数以外的数据进行了多条执行,但是这样不科学,我们可以采用arry的方式,所以修改见下:
sourcefilespath = Array("D:\data" , "D:\data2") desfilepath = Array("D:\dbbackup\"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\" , "D:\dbbackup\"&""&Year(date)&-Month(date)&-Day(date)&" "&Hour(time)&-Minute(time)&"\") Set dic=CreateObject("Scripting.Dictionary") Set fso=CreateObject("Scripting.FileSystemObject") For Each destfold In desfilepath If Not fso.FolderExists(desfilepath) Then fso.CreateFolder desfilepath End If Next WScript.Quit For Each srcfile In sourcefilespath backFolderPath=GetLastModify(srcfile) For Each destfold In desfilepath fso.CopyFile backFolderPath,destfold&fso.GetFileName(destfold) next next msgbox "Finish" ''移动文件 Function MoveFiles(yPath,sPath) On Error Resume Next Dim folder,Files,File,subFolder,subFolders Set fso = createobject("scripting.FileSystemObject") Set Folder = fso.getFolder(yPath) Set Files = Folder.Files 'msgbox yPath & sPaht For Each File In Files fso.MoveFile File,sPath&"\" 'msgbox File Next Set subFolder = Folder.SubFolders For Each subFolders In subFolder folderTemp = Split(subFolders,"\") FolderName=FolderTemp(ubound(folderTemp)) fso.createFolder(sPath&"\"&FolderName) MoveFiles subFolders,sPath&"\"&FolderName&"\" fso.DeleteFolder subFolders Next End Function Function GetLastModify(folder) Set fso = createobject("scripting.FileSystemObject") Set Folder=fso.getFolder(folder) Set subFolders = Folder.Files nowdate= Now For Each subFolder In subFolders dic.Add datediff("s",subFolder.DateLastModified,nowdate),subFolder.path Next NumArray=dic.Keys bn = NumArray(0) For Each nn In NumArray If bn >= nn Then bn = nn End If Next GetLastModify = dic.Item(bn) End Function Function fSortArray(aSortThisArray) Dim oArrayList, iElement Set oArrayList = CreateObject( "System.Collections.ArrayList" ) For iElement = 0 To UBound(aSortThisArray) oArrayList.Add aSortThisArray(iElement) Next oArrayList.Sort set fSortArray = oArrayList End Function ''拷贝文件 Function CopyFiles(yPath,sPath) On Error Resume Next Dim folder,Files,File,subFolder,subFolders Set fso = createobject("scripting.FileSystemObject") Set Folder = fso.getFolder(yPath) Set Files = Folder.Files 'msgbox yPath & sPaht For Each File In Files fso.copyFile File,sPath&"\" 'msgbox File Next Set subFolder = Folder.SubFolders For Each subFolders In subFolder folderTemp = Split(subFolders,"\") FolderName=FolderTemp(ubound(folderTemp)) fso.createFolder(sPath&"\"&FolderName) CopyFiles subFolders,sPath&"\"&FolderName&"\" 'fso.DeleteFolder subFolders Next End Function
附件:http://down.51cto.com/data/2368504
相关文章推荐
- 获取文件夹下的文件名生成新的文件&DOC命令想从指定的txt中读出文件名,然后在指定文件夹以及子目录下搜索文件,并拷贝到指定目录
- DOS工具: 当前目录文件重命名并找到最新的复制到指定目录
- Vbs程序备份指定文件到指定目录并且以日期重命名
- shell获取目录下最新的文件,文件是以日期命名
- [转]C# 获取指定目录下所有文件信息、移动目录、拷贝目录
- 复制文件(夹)到指定目录下,并按日期进行重命名
- C# 获取指定目录下所有文件信息、移动目录、拷贝目录
- 检查当前目录下是否存在指定的文件,如果存在就重新命名
- Perforce 获取当前workspace中指定目录下最新的changelist
- Python基础 —— 获取当前文件所在目录,遍历当前目录,获取指定后缀的文件
- C# 获取指定目录下所有文件信息、移动目录、拷贝目录
- 在指定路径创建文件夹, 获取EXE文件的当前目录
- C# 获取指定目录下所有文件信息、移动目录、拷贝目录
- C++ 删除、重命名文件 、获取当前目录所有文件代码
- 创建一个Date.text文件,获取当前的日期,将日期格式为“2013/02/14 05:20:00”的形式。然后一秒钟记录一次,将新的时间存入到文件中。
- C# 获取指定目录下所有文件信息、移动目录、拷贝目录
- C#获取指定目录下最新创建的指定类型的文件
- 通过vbs获取远程host文件并保存到指定目录
- WPF 获取指定目录下的图片文件,然后进行切割