Komut dosyası göreve ForEach döngüsü için liste toplanıyor
Değişken sayacı gelen Foreach, kendisine geçirilen bir deðiþkene ve her öğe aynı görevleri gerçekleştiren bir listedeki öğeler üzerinde numaralandırır.Komut dosyası göreve özel kod, bu amaç için bir liste doldurmak için kullanabilirsiniz.Numaralayıcı hakkında daha fazla bilgi için bkz: Foreach döngü kapsayıcısı.
Not
Birden çok paket arasında daha kolay yeniden kullanabileceğiniz bir görev oluşturmak isterseniz, bu komut görevin örnek kodu özel bir görev için başlangıç noktası olarak kullanarak göz önünde bulundurun.Daha fazla bilgi için bkz:Özel görev geliştirme.
Açıklama
Aşağıdaki örnek yöntemleri kullanan System.ıo ad yeni veya bir değişken kullanıcı tarafından belirtilen gün sayısından daha eski olan bir bilgisayarda, Excel çalışma kitapları listesini toplanacak.Bu sürücü C özyinelemeli olarak .xls uzantılı dosyaları, dizinleri arar ve her dosyanın en son dosya listesinde ait olup olmadığını belirlemek için değiştirildiği tarih inceler.Uygun dosyaları ekler bir ArrayList kaydeder ArrayList için bir deðiþkene, daha sonra bir Foreach döngü kapsayıcısında kullanın. Foreach döngü kapsayıcı, değişken numaralayıcı gelen Foreach kullanmak üzere yapılandırılır.
Not
Değişken sayacı gelen Foreach kullanan değişken türünde olmalıdır Object. Nesne değişkeni yerleştirdiğiniz aşağıdaki arabirimlerden birine uygulamalısınız: System.Collections.IEnumerable, System.Runtime.InteropServices.ComTypes.IEnumVARIANT, System.ComponentModel IListSource, veya Microsoft.SqlServer.Dts.Runtime.Wrapper.ForEachEnumeratorHost. Bir Array veya ArrayList yaygın olarak kullanılır. The ArrayList requires a reference and an Imports deyim for the System.Collections namespace.
Bu göreve farklı pozitif ve negatif değerler için Ek Yardım düğmesini kullanarak da deneyebilirsiniz FileAge Paket deðiþken. Örneğin, son beş gün içinde oluşturulan dosyaları aramak için 5 girerseniz veya üç günden daha önce oluşturulan dosyaları aramak için -3 girin.Bu görev, bir veya birçok klasörleri aramak için bir sürücüdeki iki dakika sürebilir.
Bu komut dosyası görev örnek yapılandırmak için
Adlı bir paket değişkeni oluşturun. FileAge türü tamsayı olarak ve pozitif veya negatif bir tamsayı bir değer girin. Pozitif bir değer olduğunda, kod dosyaları yeni belirtilen sayıda gün; negatif, belirtilen gün sayısından daha eski olan dosyaları arar.
Adlı bir paket değişkeni oluşturun. FileList türü Object Foreach tarafından daha sonra kullanmak için komut dosyası görev tarafından değişken Numaralandırıcı toplanan dosyaların listesini almak için .
Ekleme FileAge komut dosyası görevin bir değişkene ReadOnlyVariables özellik ve FileList değişken için ReadWriteVariables özellik.
Kodunuzda alma System.Collections ve System.IO ad.
Code
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Collections
Imports System.IO
Public Class ScriptMain
Private Const FILE_AGE As Integer = -50
Private Const FILE_ROOT As String = "C:\"
Private Const FILE_FILTER As String = "*.xls"
Private isCheckForNewer As Boolean = True
Dim fileAgeLimit As Integer
Private listForEnumerator As ArrayList
Public Sub Main()
fileAgeLimit = DirectCast(Dts.Variables("FileAge").Value, Integer)
' If value provided is positive, we want files NEWER THAN n days.
' If negative, we want files OLDER THAN n days.
If fileAgeLimit < 0 Then
isCheckForNewer = False
End If
' Extract number of days as positive integer.
fileAgeLimit = Math.Abs(fileAgeLimit)
listForEnumerator = New ArrayList
GetFilesInFolder(FILE_ROOT)
' Return the list of files to the variable
' for later use by the Foreach from Variable enumerator.
System.Windows.Forms.MessageBox.Show("Matching files: " & listForEnumerator.Count.ToString, "Results", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information)
Dts.Variables("FileList").Value = listForEnumerator
Dts.TaskResult = ScriptResults.Success
End Sub
Private Sub GetFilesInFolder(ByVal folderPath As String)
Dim localFiles() As String
Dim localFile As String
Dim fileChangeDate As Date
Dim fileAge As TimeSpan
Dim fileAgeInDays As Integer
Dim childFolder As String
Try
localFiles = Directory.GetFiles(folderPath, FILE_FILTER)
For Each localFile In localFiles
fileChangeDate = File.GetLastWriteTime(localFile)
fileAge = DateTime.Now.Subtract(fileChangeDate)
fileAgeInDays = fileAge.Days
CheckAgeOfFile(localFile, fileAgeInDays)
Next
If Directory.GetDirectories(folderPath).Length > 0 Then
For Each childFolder In Directory.GetDirectories(folderPath)
GetFilesInFolder(childFolder)
Next
End If
Catch
' Ignore exceptions on special folders such as System Volume Information.
End Try
End Sub
Private Sub CheckAgeOfFile(ByVal localFile As String, ByVal fileAgeInDays As Integer)
If isCheckForNewer Then
If fileAgeInDays <= fileAgeLimit Then
listForEnumerator.Add(localFile)
End If
Else
If fileAgeInDays > fileAgeLimit Then
listForEnumerator.Add(localFile)
End If
End If
End Sub
End Class
using System;
using System.Data;
using System.Math;
using Microsoft.SqlServer.Dts.Runtime;
using System.Collections;
using System.IO;
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
private const int FILE_AGE = -50;
private const string FILE_ROOT = "C:\\";
private const string FILE_FILTER = "*.xls";
private bool isCheckForNewer = true;
int fileAgeLimit;
private ArrayList listForEnumerator;
public void Main()
{
fileAgeLimit = (int)(Dts.Variables["FileAge"].Value);
// If value provided is positive, we want files NEWER THAN n days.
// If negative, we want files OLDER THAN n days.
if (fileAgeLimit<0)
{
isCheckForNewer = false;
}
// Extract number of days as positive integer.
fileAgeLimit = Math.Abs(fileAgeLimit);
ArrayList listForEnumerator = new ArrayList();
GetFilesInFolder(FILE_ROOT);
// Return the list of files to the variable
// for later use by the Foreach from Variable enumerator.
System.Windows.Forms.MessageBox.Show("Matching files: "+ listForEnumerator.Count, "Results",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Dts.Variables["FileList"].Value = listForEnumerator;
Dts.TaskResult = (int)ScriptResults.Success;
}
private void GetFilesInFolder(string folderPath)
{
string[] localFiles;
DateTime fileChangeDate;
TimeSpan fileAge;
int fileAgeInDays;
try
{
localFiles = Directory.GetFiles(folderPath, FILE_FILTER);
foreach (string localFile in localFiles)
{
fileChangeDate = File.GetLastWriteTime(localFile);
fileAge = DateTime.Now.Subtract(fileChangeDate);
fileAgeInDays = fileAge.Days;
CheckAgeOfFile(localFile, fileAgeInDays);
}
if (Directory.GetDirectories(folderPath).Length > 0)
{
foreach (string childFolder in Directory.GetDirectories(folderPath))
{
GetFilesInFolder(childFolder);
}
}
}
catch
{
// Ignore exceptions on special folders, such as System Volume Information.
}
}
private void CheckAgeOfFile(string localFile, int fileAgeInDays)
{
if (isCheckForNewer)
{
if (fileAgeInDays <= fileAgeLimit)
{
listForEnumerator.Add(localFile);
}
}
else
{
if (fileAgeInDays > fileAgeLimit)
{
listForEnumerator.Add(localFile);
}
}
}
}
|