Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. </p> <p>You can use command line parameters to call it and override the defaults, or you could just replace the strings in the Variables section:</p> <p>cscript {scriptname}.vbs /source:"C:\somefoldername\folder" /destination:"C:\someotherfolder\folder" /ext:txt /recent:2</p> <p>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. </p> <p>Try this (I've tested it, it does work on Windows 7 [and apparently Vista])</p> <pre><code>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 &amp; "\Desktop\MY\MMS" ' Folder Source to check for recent files to copy FROM folderDestination = strHomeFolder &amp; "\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: " &amp; FolderToCheck wscript.echo "For Files of type: " &amp; FileExt wscript.echo "Copying most recent "&amp; mostRecent &amp;" file(s) to: " &amp; FolderDestination &amp; "." 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 &amp; " File(s) found. Sorting and copying last " &amp; mostRecent &amp;"..." fileList.Sort = "date DESC" If Not(fileList.EOF) Then fileList.MoveFirst If fileList.recordCount &lt; mostRecent Then wscript.echo "WARNING: " &amp; mostRecent &amp;" file(s) specified but only " &amp; fileList.recordcount &amp; " file(s) match criteria. Adjusted to " &amp; fileList.RecordCount &amp; "." mostRecent = fileList.recordcount End If fileCounter = 0 Do Until fileList.EOF Or fileCounter =&gt; 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 &amp; "\", True wscript.echo fileList("date").value &amp; vbTab &amp; fileList("name") fileList.moveNext fileCounter = fileCounter + 1 Loop Else wscript.echo "An unexpected error has occured." End If Else wscript.echo "No matching """ &amp; FileExt &amp;""" files were found in """ &amp; foldertocheck &amp; """ to copy." End If Else wscript.echo "Error: Source folder does not exist """ &amp; foldertocheck &amp; """." 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 ' &lt;scriptname&gt;.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 &lt;&gt; "" Then FolderToCheck = source If destination &lt;&gt; "" Then FolderDestination = destination If ext &lt;&gt; "" Then FileExt = ext If recent &lt;&gt; "" Then mostRecent = int(recent) End Function </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload