writing to a text file is working good...But my problem is text file is created in different path....
Here is my code...
var _logFolderPath = Path.Combine(textboxPath.Text.Trim(), "log");
string[] subdirectoryEntries = Directory.GetDirectories(Path.Combine(txtBoxInput.Text, "RM"));
foreach (string subdirectory in subdirectoryEntries)
{
string[] x = Directory.GetFiles(subdirectory);
DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
............
}
and
using (var dest = File.AppendText(Path.Combine(_logFolderPath,subdirectory+ ".txt")))
Here i have to store the subdirectory named text file in _logFolderPath path....But the text files are generared in the subdirectory path...I just need the name of the subdirectory to be created as a text file in _logFolderPath path...How to get the name of the subdirectory alone....Any suggestion?
Have a look at using DirectoryInfo and DirectoryInfo.Name Property
You should use DirectoryInfo instead. It can give you just the directory name. Here is a sample
DirectoryInfo root = new DirectoryInfo("C:\\");
foreach (DirectoryInfo s in root.GetDirectories())
{
Console.Out.WriteLine(s.Name);
}
Related
My requirement is I have a main folder under which has a set of subfolder. These subfolders are EXPECTED to have a "FileServer.config" file. I need to validate this and Out put a message if the File is missing.
MainFolder--> Contains subfolder 1 ,sub folder 02 , subfolder 03.
So when the user clicks say for example SubFolder01 I want t validate if this file exist in that folder
Currently in the Code I have the code scans for all the folders at once and the output is based on the first folder
string path =# "D:\TEST\PROJ\Repo\";
DirectoryInfo directory = new DirectoryInfo(path);
DirectoryInfo[] subDirectories = directory.GetDirectories();
foreach(DirectoryInfo folder in subDirectories)
{
string subpath = Path.Combine( # "D:\TEST\PROJ\Repo\", folder.Name);
string[] filePaths = Directory.GetFiles(subpath, "fileserver.config");
if(filePaths.Any())
Console.WriteLine(subpath);
}
This will work:
string path = #"D:\TEST\PROJ\Repo\";
DirectoryInfo directory = new DirectoryInfo(path);
foreach (DirectoryInfo folder in directory.GetDirectories())
{
var files = folder.GetFiles("fileserver.config");
if (files.Length > 0)
Console.WriteLine(folder.Name);
}
You said the user clicks on a subdirectory (element), so I assume that you get a concrete folder path. To validate if a certain file is existing in a specific folder try
public bool FileExistsInFolder(string folderPath, string filename)
{
return Directory.Exists(folderPath)
&& Directory.GetFiles(folderPath, fileName).Any();
}
I am trying to recursively parse a directory ,look for .tar.bz2 files in the subdirectories , untar and create a list of all the files inside the tar file,following is the detailed info,how to accomplish this?
path = \\location\tarballfiles\
lets say the following .tar.bz2 files are present inside "path" in the mentioned locations
RA900B\hw.1\cqq-tech-fw-RA900B_hw_2-DATA.3.4-00028-S-1.tar.bz2
CQQ9888\hw.2\cqq-tech-fw-CQQ9888_hw_2-DATA.3.4-00028-S-1.tar.bz2
......
EXPECTED OUTPUT:-
RA900B\hw.1\top.bin
RA900B\hw.1\mtp.bin
CQQ9888\hw.2\test.txt
CQQ9888\hw.2\data.txt
.......
PSEUDO CODE:-
for each subfolder under path(ignore any files)
parse recursively to look for .tar.bz2 files
untar
create a list of all the files
To find your tarball files in a directory, you can use:
string[] files = Directory.GetFiles("\\\\location\\tarballfiles\\", "*.tar.bz2", SearchOption.AllDirectories);
and then recurse the files, open them, untar then and add the files of the tar to a list.
Example (with some pseudo code):
var allFiles = new List<string>();
string[] files = Directory.GetFiles("\\\\location\\tarballfiles\\", "*.tar.bz2", SearchOption.AllDirectories);
foreach (var file in files)
{
// untar
var filesFromTar = file.untar();
foreach (var fileNameFromTar in filesFromTar)
{
allFiles.Add(fileNameFromTar);
}
}
foreach (var allFile in allFiles)
{
Console.WriteLine(allFile);
}
How can I list the text files in a certain directory (C:\Users\Ece\Documents\Testings) in a listbox of a WinForm(Windows application)?
// What directory are the files in?...
DirectoryInfo dinfo = new DirectoryInfo(#"C:\TestDirectory");
// What type of file do we want?...
FileInfo[] Files = dinfo.GetFiles("*.txt");
// Iterate through each file, displaying only the name inside the listbox...
foreach( FileInfo file in Files )
{
listbox1.Items.Add(file.Name);
}
// A statement, followed by a smiley face...
That oughta do it. ;o)
To get the txt files, try this:
var folder = #"C:\Users\Ece\Documents\Testings";
var txtFiles = Directory.GetFiles(folder, "*.txt");
listBox.Items.AddRange(txtFiles);
I have a web directory that contains files and I want to delete them all. I've looked online but all the answers rely on the file system and I want to use the website's directories. I tried this:
foreach (string file in HttpContext.Current.Server.MapPath("\\MyDirectory"))
{
File.Delete(file);
}
The foreach statement is underlined and the error is 'cannot convert type char to string'.
What's the syntax to delete all files in a directory?
Your might need to correct your MapPath parameter (\\MyDirectory), but the syntax you need is shown below.
System.IO.DirectoryInfo di= new DirectoryInfo(HttpContext.Current.Server.MapPath("\\MyDirectory"));
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
Server.MapPath gives you a directory path, not an array of files/folders. IF you want to remove all files in the folder you would do this:
var folderPath = HttpContext.Current.Server.MapPath("\\MyDirectory");
foreach (string file in Directory.GetFiles(folderPath))
{
File.Delete(file);
}
If you wanted do delete the folder, then
var folderPath = HttpContext.Current.Server.MapPath("\\MyDirectory");
Directory.Delete(folderPath);
To delete all folders within the main folder
var folderPath = HttpContext.Current.Server.MapPath("\\MyDirectory");
foreach (string file in Directory.GetDirectoriesfolderPath))
{
File.Delete(file);
}
Directory.GetFiles(targetDirectory);
Using the above code we get the names(i.e full path) of all the files in the directory. but i need to get only the name of the file and not the path. So how can I get the name of the files alone excluding the path? Or do I need to do the string operations in removing the unwanted part?
EDIT:
TreeNode mNode = new TreeNode(ofd.FileName, 2, 2);
Here ofd is a OpenFileDialog and ofd.FileName is giving the the filename along with it's Path but I need the file name alone.
You could use:
Path.GetFileName(fullPath);
or in your example:
TreeNode mNode = new TreeNode(Path.GetFileName(ofd.FileName), 2, 2);
Use DirectoryInfo and FileInfo if you want to get only the filenames without doing any manual string editing.
DirectoryInfo dir = new DirectoryInfo(dirPath);
foreach (FileInfo file in dir.GetFiles())
Console.WriteLine(file.Name);
Use:
foreach(FileInfo fi in files)
{
Response.Write("fi.Name");
}