I am receiving the following error in the logfile relating to the below script:
1/12/2014 7:52:22 AM: Script failed to compile with error: 'System.Array' does not contain a definition for 'OrderByDescending' and no extension method 'OrderByDescending' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) on line 17.
The script is a simple script that finds the latest files in a directory and copies them to another location. The research seems to point to including a reference to "using System.Linq;", which does not error out, but also does not resolve the issue.
Would anyone have any ideas on this error?
Script listing:
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MLS.ZWave.Service.Rules;
using MLS.ZWave.BusinessObjects;
public class CopyRecentVideos : ScriptBase, ScriptInterface {
public void runScript() {
try {
string searchFolder = @"e:\AllVideos\";
string destinationFolder = @"e:\LatestVideos\";
string filePattern = @"*.mp4";
int fileCount = 3;
foreach (var fi in new DirectoryInfo(searchFolder).GetFiles(filePattern, System.IO.SearchOption.AllDirectories).OrderByDescending(x => x.LastWriteTime).Take(fileCount))
fi.CopyTo(destinationFolder + fi.Name,true);
} catch (Exception ex) {
var message = ex.Message;
writeLog(message);
}
}
}
Problem with C# Script - retrieving list of latest files
- 77 Views
- Last Post 14 January 2014
I'm getting ready to head to work now, but check here. Sorry if not much help, but just a quick search. You might need to drop back to .NET 2.0 methods.
This is painful. Does anyone know the specific constraints of the C# scripting language that needs to be applied when developing scripts? I altered the approach as listed below and now get this error:
1/13/2014 6:22:33 PM: Script failed to compile with error: 'System.Array' does not contain a definition for 'ToArray' and no extension method 'ToArray' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?) on line 19.
public class CopyRecentVideos : ScriptBase, ScriptInterface {
public void runScript() {
try {
string searchFolder = @"e:\SourceVideos\";
string destinationFolder = @"e:\Latest_Videos\";
string filePattern = @"*.mp4";
int fileCount = 3;
int tmp = 1;
DirectoryInfo info = new DirectoryInfo(searchFolder);
FileInfo[] files = info.GetFiles(filePattern, System.IO.SearchOption.AllDirectories).ToArray();
Array.Sort
{
return a.LastWriteTime.CompareTo(b.LastWriteTime);
});
foreach (FileInfo file in files)
{
if (tmp <= filecount)="" file.copyto(destinationfolder="" +="" file.name,="" true);="">=>
tmp = tmp + 1;
}
} catch (Exception ex) {
var message = ex.Message;
writeLog(message);
}
}
}
The GetFile() method returns an Array object type already, so you just need to drop ToArray().
I copied your script into my setup, dropped the ".ToArray()" and everything worked beautifully!
(I had files numbered 1-4, the oldest modified file was #3, so it copied filed 1,2,4 to the destination.)
Nice work!
Oh yeah, another script to add to the library!
Good catch, thank you. The updated script is below. Note the additional change I made to search for the newest files in the directory (just changed the order of the compare in the sort line).
____________________
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MLS.ZWave.Service.Rules;
using MLS.ZWave.BusinessObjects;
public class CopyRecentVideos : ScriptBase, ScriptInterface {
public void runScript() {
try {
string searchFolder = @"e:\VideoFiles\";
string destinationFolder = @"e:\LatestVideos\";
string filePattern = @"*.mp4";
int fileCount = 3;
int tmp = 1;
DirectoryInfo info = new DirectoryInfo(searchFolder);
FileInfo[] files = info.GetFiles(filePattern, System.IO.SearchOption.AllDirectories);
Array.Sort
{
return b.LastWriteTime.CompareTo(a.LastWriteTime);
});
foreach (FileInfo file in files)
{
if (tmp <= filecount)="" file.copyto(destinationfolder="" +="" file.name,="" true);="">=>
tmp = tmp + 1;
}
} catch (Exception ex) {
var message = ex.Message;
writeLog(message);
}
}
}