delete files which are more than one month old using c# - c#

how to delete files which are more than one month old using c# script.
i am using framework 2.0..

string path = #"C:\Temp\"; //"
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileInfo[] fileInfos = dirInfo.GetFiles();
foreach (FileInfo fileInfo in fileInfos)
{
if (fileInfo.LastWriteTime < DateTime.Now.AddMonths(-1))
fileInfo.Delete();
}

You can call Directory.GetFiles to find all files in a folder.
You can call File.GetLastWriteTime to check when the file was modified.
You can call File.Delete to delete a file.

Related

How to delete zip folder programmatically?

I want to delete a zip folder if it is exists. I have below code.
string zippath = #"C:\Neenu\Downloads.zip";
ZipFile.CreateFromDirectory(#"" + TemporaryFolder, #"" + zippath);
Just before the above code I want to check if folder exists or not. If exists I want to delete folder.
I think you meant How to delete zip file and not a folder.
Here, this should be easy:
File.Delete(zippath);
For deleting inner files and directories :
System.IO.DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
Refer below codeI am keeping a copy of txt file and after that creating a new one
If you wish to delete file without vreating backup than jst use File.Delete(path of file)
if (File.Exists(file_path))
{
new_file_path = file_path.Replace(".txt", " created on " + File.GetLastWriteTime(file_path).ToString("dd-MM-yyyy hh-mm-ss tt") + ".txt");
File.Move(file_path, new_file_path);
File.Delete(file_path);
}

Why dose File.Exists not work for exe's and shortcuts [duplicate]

I am using .NET 2.0 and Linq is out of question. I would like to check if file exists inside a directory without knowledge of the file extension.
I just need this logic done.
1.Check File exists in Directory using String Filename provided using search pattern leaving out the extension of the File
2.Get the Files if they exists and Databind to provide Download links.If file does not exist then start uploading the File.
Update:
Directory.GetFiles() and DirectoryInfo.GetFiles() indeed solves the part where in i check for File existence. As for the performance regarding FileInfo objects, these were only solution to my requirements of databinding to provide download links
DirectoryInfo root = new DirectoryInfo("your_directory_path");
FileInfo[] listfiles = root.GetFiles("dummy.*");
if (listfiles.Length > 0)
{
//File exists
foreach (FileInfo file in listfiles)
{
//Get Filename and link download here
}
}
else
{
//File does not exist
//Upload
}
Hope this works
To see if a file exists with that name, can you not just use..
However, Directory.GetFiles already includes the full path
string [] files = Directory.GetFiles(Path,"name*");
bool exists = files.Length > 0;
if ( exists)
{
//Get file info - assuming only one file here..
FileInfo fi = new FileInfo(files[0]);
//Or loop through all files
foreach (string s in files)
{
FileInfo fi = new FileInfo(s);
//Do something with fileinfo
}
}
You can use DirectoryInfo.GetFiles() to have a FileInfo[] instead of a String[].

Visual C#: Move multiple files with the same extensions into another directory

guys. I've got a problem I can't solve:
I have a 2 folders I choose with folderBrowserDialog and tons of files in source directory I need to move to the target directory. But, I have only to move files with a specific extension like .txt or any other extension I can get from textbox.
So how can I do it?
First get all the files with specified extension using Directory.GetFiles() and then iterate through each files in the list and move them to target directory.
//Assume user types .txt into textbox
string fileExtension = "*" + textbox1.Text;
string[] txtFiles = Directory.GetFiles("Source Path", fileExtension);
foreach (var item in txtFiles)
{
File.Move(item, Path.Combine("Destination Directory", Path.GetFileName(item)));
}
Try this:
For copying files...
foreach (string s in files)
{
File.Copy(s, "C:\newFolder\newFilename.txt");
}
for moving files
foreach (string s in files)
{
File.Move(s, "C:\newFolder\newFilename.txt");
}
Example for Moving files to Directory:
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
DirectoryInfo d = new DirectoryInfo(filepath);
foreach (var file in d.GetFiles("*.txt"))
{
Directory.Move(file.FullName, filepath + "\\TextFiles\\" + file.Name);
}
will Move all the files from Desktop to Directory "TextFiles".

Recursively delete files from a Directory but keeping the dir structure intact

For cleaning up the test files, am trying to do the below. But its not clearing the files as well as not generating an error.
Am I missing something obvious?
private void CleanUpTempDirFiles()
{
var fileGenerationDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "TestFilesDir"));
fileGenerationDir.GetDirectories().ToList().ForEach(dir => dir.GetFiles().ToList().ForEach(file => file.Delete()));
}
You can get all the files in all the the subdirectories using SearchOption.AllDirectories
fileGenerationDir.GetFiles("*", SearchOption.AllDirectories).ToList().ForEach(file=>file.Delete());
You are using GetDirectories first which returns all sub-directories in your temp folder. Hence it does not return the files in this directory. So you might want to do this instead:
var tempDir = Path.Combine(Path.GetTempPath(), "TestFilesDir");
var allFilesToDelete = Directory.EnumerateFiles(tempDir, "*.*", SearchOption.AllDirectories);
foreach (var file in allFilesToDelete)
File.Delete(file);
Removed the ToLists and used SearchOption.AllDirectories which searches recursively.
How to: Iterate Through a Directory Tree (C# Programming Guide)
This will do it:
string[] filePaths = Directory.GetFiles(
Path.Combine(Path.GetTempPath(), "TestFilesDir")
,
"*",
SearchOption.AllDirectories);
foreach (var filePath in filePaths)
File.Delete(filePath);
If files are stored inside the TestFilesDir folder you don't have to get it's subdirectories, use simply:
fileGenerationDir.GetFiles().ToList().ForEach(file => file.Delete());
otherwise you are deleting only subfolder's files
private void CleanUpTempDirFiles() {
var fileGenerationDir = new DirectoryInfo(
Path.Combine(Path.GetTempPath(), "TestFilesDir"));
fileGenerationDir.GetFiles().ToList().ForEach(file => file.Delete());
}

how can i filter only .sql files using directory.getfiles

I am trying to filter the only.sql files from given Directory path
But it will compress the all files in the given directorypath
string directorypath = #"C:\access";
DirectoryInfo di = new DirectoryInfo(directorypath);
foreach (FileInfo fi in di.GetFiles().Where(.sql))
{
CompressionMethod(fi);
}
but , i want send only .sql files to this method CompressionMethod(fi) .... how can i do
that
would any one pls help on this...
many thanks....
You should use di.GetFiles("*.sql")
there exists an overload of GetFiles with a searchpattern: GetFiles(string searchpattern)
so use:
di.GetFiles("*.sql")

Categories