将 2 个最新的文本文件从源文件夹复制到目标文件夹 [英] Copy 2 latest text file from a source folder to destination folder
问题描述
我有一个脚本,可以将任何文件复制到不同的文件夹.我可以知道如何编辑此脚本以仅复制2个最新日期 *text* 来自 C: 的文件.当它复制时,它必须覆盖旧文件.以下是我从网站编辑的脚本.我是脚本世界的初学者.. 请帮助我...
I have a script where i can copy any file to a different folder. May i know how i can edit this script to only copy the 2 latest date *text* file from C:. When it copies, it must overwrite the old file. Below is the script that i edited from a website. I am very beginner in scripting world.. Please help me...
' Copy a File
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\Users\User\Desktop\MY\MMS\tag.txt" , "C:\Users\User\Desktop\new\", OverwriteExisting
推荐答案
您可以使用以下代码 - 我不确定它对大目录的表现如何.我根据您的目录名称做出了一些假设,即它将寻找用户的主文件夹等.
You could use the following code - I'm not sure how it would behave with a large directory. I've made some assumptions based on your directory names, that it will be looking for a user's home folder, etc.
您可以使用命令行参数来调用它并覆盖默认值,或者您可以只替换变量部分中的字符串:
You can use command line parameters to call it and override the defaults, or you could just replace the strings in the Variables section:
cscript {scriptname}.vbs/source:"C:\somefoldername\folder"/destination:"C:\someotherfolder\folder"/ext:txt/recent:2
cscript {scriptname}.vbs /source:"C:\somefoldername\folder" /destination:"C:\someotherfolder\folder" /ext:txt /recent:2
有非常基本的错误检查.如果文件存在,它应该覆盖文件.如果目标文件夹不存在,它还会创建目标文件夹.
There's very basic error checking. It should overwrite files if they exist. It will also create the destination folder if it doesn't exist.
试试这个(我已经测试过了,它确实适用于 Windows 7 [显然是 Vista])
Try this (I've tested it, it does work on Windows 7 [and apparently Vista])
Option Explicit
Dim FolderToCheck, FolderDestination, FileExt, mostRecent, noFiles, fso, fileList, file, filecounter, oShell, strHomeFolder
' Enumerate current user's home path - we will use that by default later if nothing specified in commandline
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
'Variables -----
folderToCheck = strHomeFolder & "\Desktop\MY\MMS" ' Folder Source to check for recent files to copy FROM
folderDestination = strHomeFolder & "\Desktop\New" ' Destination Folder where to copy files TO
fileExt = "txt" ' Extension we are searching for
mostRecent = 2 ' Most Recent number of files to copy
' --------------
PreProcessing() ' Retrieve Command Line Parameters
' Display what we are intending on doing
wscript.echo "Checking Source: " & FolderToCheck
wscript.echo "For Files of type: " & FileExt
wscript.echo "Copying most recent "& mostRecent &" file(s) to: " & FolderDestination & "."
wscript.echo
noFiles = TRUE
Set fso = CreateObject("Scripting.FileSystemObject")
Set fileList = CreateObject("ADOR.Recordset")
fileList.Fields.append "name", 200, 255
fileList.Fields.Append "date", 7
fileList.Open
If fso.FolderExists(FolderToCheck) Then
For Each file In fso.GetFolder(FolderToCheck).files
If LCase(fso.GetExtensionName(file)) = LCase(FileExt) then
fileList.AddNew
fileList("name").Value = File.Path
fileList("date").Value = File.DateLastModified
fileList.Update
If noFiles Then noFiles = FALSE
End If
Next
If Not(noFiles) Then
wscript.echo fileList.recordCount & " File(s) found. Sorting and copying last " & mostRecent &"..."
fileList.Sort = "date DESC"
If Not(fileList.EOF) Then
fileList.MoveFirst
If fileList.recordCount < mostRecent Then
wscript.echo "WARNING: " & mostRecent &" file(s) specified but only " & fileList.recordcount & " file(s) match criteria. Adjusted to " & fileList.RecordCount & "."
mostRecent = fileList.recordcount
End If
fileCounter = 0
Do Until fileList.EOF Or fileCounter => mostRecent
If Not(fso.FolderExists(folderDestination)) Then
wscript.echo "Destination Folder did not exist. Creating..."
fso.createFolder folderDestination
End If
fso.copyfile fileList("name"), folderDestination & "\", True
wscript.echo fileList("date").value & vbTab & fileList("name")
fileList.moveNext
fileCounter = fileCounter + 1
Loop
Else
wscript.echo "An unexpected error has occured."
End If
Else
wscript.echo "No matching """ & FileExt &""" files were found in """ & foldertocheck & """ to copy."
End If
Else
wscript.echo "Error: Source folder does not exist """ & foldertocheck & """."
End If
fileList.Close
Function PreProcessing
Dim source, destination, ext, recent
' Initialize some variables
Set source = Nothing
Set destination = Nothing
Set ext = Nothing
Set recent = Nothing
'Get Command Line arguments
' <scriptname>.vbs /Source:"C:\somepath\somefolder" /Destination:"C:\someotherpath\somefolder" /ext:txt /recent:2
source = wscript.arguments.Named.Item("source")
destination = wscript.arguments.Named.Item("destination")
ext = wscript.arguments.Named.Item("ext")
recent = wscript.arguments.Named.Item("recent")
If source <> "" Then FolderToCheck = source
If destination <> "" Then FolderDestination = destination
If ext <> "" Then FileExt = ext
If recent <> "" Then mostRecent = int(recent)
End Function
这篇关于将 2 个最新的文本文件从源文件夹复制到目标文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!