Using below program u can move top 5 size files from one folder to other folder
XAML CODE
XAML.CS Code
Output:
If you have any queries or suggestions, please feel free to ask in comments section.
XAML CODE
<Window x:Class="Cleanup.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid Margin="0,0,13,0"> <DatePicker Name="StartDatePicker" HorizontalAlignment="Left" Margin="112,156,0,0" VerticalAlignment="Top" Height="23" Width="107"/> <DatePicker Name="EndDatePicker" HorizontalAlignment="Left" Margin="344,156,0,0" VerticalAlignment="Top" Height="23" Width="108"/> <Label Content="Start Date" x:Name="StartDate" HorizontalAlignment="Left" Margin="43,156,0,0" VerticalAlignment="Top" Height="28" Width="67"/> <Label Content="End Date" Name="EndDate" HorizontalAlignment="Left" Margin="278,154,0,0" VerticalAlignment="Top" Background="Transparent" Height="27" Width="61"/> <Label Content="Clean up" HorizontalAlignment="Left" Margin="149,20,0,0" VerticalAlignment="Top" FontSize="18" FontWeight="Bold"/> <Button Content="Delete" HorizontalAlignment="Left" Margin="225,233,0,0" VerticalAlignment="Top" Width="80" Click="DeleteButtonClick" Height="23"/> <Label Content="" Name="ErrReqStartDate" HorizontalAlignment="Left" Margin="112,184,0,0" VerticalAlignment="Top" Background="#00000000" Foreground="#FFEC0D0D" Height="31" Width="163"/> <Label Content="" Name="ErrReqEndDate" HorizontalAlignment="Left" Margin="344,184,0,0" VerticalAlignment="Top" Foreground="#FFEC0D0D" Height="31" Width="150"/> <Label Content="Source Folder" HorizontalAlignment="Left" Margin="37,80,0,0" VerticalAlignment="Top"/> <TextBox Name="txtSourceFolder" HorizontalAlignment="Left" Height="23" Margin="149,80,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="345"/> <Label Content="Destination Folder" HorizontalAlignment="Left" Margin="37,111,0,0" VerticalAlignment="Top"/> <TextBox Name="txtDestinationFolder" HorizontalAlignment="Left" Height="23" Margin="149,111,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="345"/> <Label HorizontalAlignment="Left" Name="mainErrLabel" Height="28" Margin="55,205,0,0" Content="" VerticalAlignment="Top" Width="430" Foreground="#FFE20D0D"/> </Grid> </Window>
XAML.CS Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace CleanUp { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } /// <summary> /// Delete button click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteButtonClick(object sender, RoutedEventArgs e) { bool validate; //validate=Validate(); TransferFiles(); } /// <summary> /// Transfer files from one location to other /// </summary> private void TransferFiles() { string startFolder = @"C:\Users\nrang\Desktop\zipfiles\"; // Take a snapshot of the file system. string destFolder = @"C:\Users\nrang\Desktop\zipfilesdest\"; DateTime? startDatePicker,endDatePicker; startDatePicker = StartDatePicker.SelectedDate; endDatePicker = EndDatePicker.SelectedDate; System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder); // This method assumes that the application has discovery permissions // for all folders under the specified path. IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories); //Return the FileInfos for the 5 largest files // query for top 5 is an IEnumerable<System.IO.FileInfo> var queryFiveLargest = (from file in fileList let date=GetFileCreatedDate(file) let len = GetFileLength(file) where date >=startDatePicker && date <= endDatePicker orderby len descending select file).Take(5); Console.WriteLine("The 10 largest files under {0} are:", startFolder); foreach (var v in queryFiveLargest) { Console.WriteLine("{0}: {1} bytes", v.FullName, v.Length); //System.IO.File.Copy(v.FullName, destFolder+v.Name, true); System.IO.File.Move(v.FullName, destFolder + v.Name); } mainErrLabel.Content = "Data Transfer Completed Successfully"; ////Query to get other files for deletion //var queryAllFiles = // (from file in fileList // let date = GetFileCreatedDate(file) // where date >=startDatePicker &&date<=endDatePicker // select file); ////delete other extra files. //foreach (var v in queryAllFiles) //{ // Console.WriteLine("{0}: {1} bytes", v.FullName, v.Length); // //System.IO.File.Copy(v.FullName, destFolder+v.Name, true); // System.IO.File.Delete(v.FullName); //} } static long GetFileLength(System.IO.FileInfo fi) { long retval; try { retval = fi.Length; } catch (System.IO.FileNotFoundException) { // If a file is no longer present, // just add zero bytes to the total. retval = 0; } return retval; } static DateTime GetFileCreatedDate(System.IO.FileInfo fi) { DateTime fileCreatedDate; try { fileCreatedDate = fi.CreationTime; } catch (Exception ex) { fileCreatedDate = DateTime.Today; } return fileCreatedDate; } } }
Output:
If you have any queries or suggestions, please feel free to ask in comments section.
Post a Comment
Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.