I am using string[] dirs = Directory.GetDirectories("instances", "*") to get all the directories.
But it returns the directories as followed:
instances\\test01, instances\\test02
Then i use the following function to download a file to that directory:
FileDownloader downloader = new FileDownloader(dirs[0] + "/server/server.jar", "blabla");
But the file appear in the root directory instead.Any suggestions?
Instead of dirs[0] + "/server/server.jar"
use
Path.Combine(dirs[0], "/server/server.jar")
I'd imagine that the string appending could be treating \t as a tab
Related
I receive every day a file with a specific pattern and extension, which I want to run under certain process. I want to check if there is any file in my folder, because otherwise I will do another task. So far I found that you can use a Script Task and do a File.Exist. However, I'm doing something wrong because it doesn't take the * as a wildcard.
Devoluciones_source is "C:\Users\us1\Folder\"
FileToSearch is "return"
My files:
return_20200102.csv
return_20200203.csv
String Filepath = Dts.Variables["$Project::Devoluciones_source"].Value.ToString() + Dts.Variables["User::FileToSearch"].Value.ToString() + "*csv";
if (
File.Exists(Filepath))
{
Dts.Variables["User::IsFound"].Value = 1;
}
I don't think File.Exits() accepts wildcards, it checks the literal filepath and will return false because C:\Users\us1\Folder\*.csv is not found.
What you could do instead is get the files in the folder C:\Users\us1\Folder\ and checking those agains a searchPattern using Directory.GetFiles(path, searchPattern)
Like this:
string dirPath = Dts.Variables["$Project::Devoluciones_source"].Value.ToString();
string fileName = Dts.Variables["User::FileToSearch"].Value.ToString();
// if you want to make sure the directory exists:
if(Directory.Exists(dirPath) {
string[] files = Directory.GetFiles(dirPath, fileName + "*.csv");
if(files.lenght > 0) {
// you can now iterate over each file that is found in the users directory that matches your pattern and do your logic.
}
}
Some more info on the Directory.GetFiles method: Directory.GetFiles on docs.Microsoft.com
Some more info on the Files.Exists method: Directory.GetFiles on docs.Microsoft.com
I am trying to create a folder with a variable that can change, I am using
System.IO.Directory.CreateDirectory
When I hardcode a value like so:
var folder = #"C:\Users\Laptop\Documents\bot\random string here"
It creates that directory, but when I pass data to my method and try and use it like so:
var folder = #"C:\Users\Laptop\Documents\bot\" +
articlename.Replace(" ", "_");
System.IO.Directory.CreateDirectory(folder);
It doesn't create it nor break. How can I use the variable to create a folder like that?
My article is a random string like "hello this is a article yadda"
I solved by adding my paths together correctly as below
string path = root +"/"+ newfolder;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
// To move a file or folder to a new location:
//System.IO.File.Move(fi.FullName, path);
}
Why am I getting this error? I am using the correct path.
Problem : You are providing the Path of File
Solution : You need to provide the path of Directory to get all the files in a given Directory based on your search pattern.
From MSDN: Directory.GetFiles()
Returns the names of files (including their paths) that match the
specified search pattern in the specified directory.
Try this:
string directoryName = Path.GetDirectoryName(e.FullPath);
foreach(String filename in Directory.GetFiles(directoryName,"*.eps"))
{
//your code here
}
You want the directory, not the filename.
At the moment, the value of e.FullPath is "C:\\DigitalAssets\\LP_10698.eps". It should be "C:\\DigitalAssets".
string[] fileNames = Directory.GetFiles(string path) requires a directory, you are giving it a directory + filename.
MSDN:
Returns the names of files (including their paths) that match the
specified search pattern in the specified directory.
foreach(string filename in Directory.GetFiles(e.FullPath, "*.eps"))
{
// For this to work, e.FullPath needs to be a directory, not a file.
}
You can use Path.GetDirectoryName():
foreach(string filename in Directory.GetFiles(Path.GetDirectoryName(e.FullPath), "*.eps"))
{
// Path.GetDirectoryName gets the path as you need
}
You could create a method:
public string GetFilesInSameFolderAs(string filename)
{
return Directory.GetFiles(Path.GetDirectoryName(filename), Path.GetExtension(filename));
}
foreach(string filename in GetFilesInSameFolderAs(e.FullPath))
{
// do something with files.
}
e.FullPath seems to be a file, not a directory. If you want to enumerate *.eps files, the first argument to GetFiles should be a directory path: #"C:\DigitalAssets"
the first argument of GetFiles should be only "C:\DigitalAssets"
e.FullPath include the file name in it.
Directory.GetFiles used to get file names from particular directory. You are trying to get files from file name which is not valid as it gives you error. Provide directory name to a function instead of file name.
You can try
Directory.GetFiles(System.IO.Path.GetDirectoryName(e.FullPath),"*.eps")
For deleting files from a directory
var files = Directory.GetFiles(directory)
foreach(var file in files)
{
File.Delete(file);
}
In short to delete the file use
File.Delete(filePath);
and to delete the directory
Directory.Delete(directoryPath)
I am listing pdf files using C#, but some files wont open because they have percentage(%) signs on their filenames, the user still wants the % to be shown on the filename, but I can't get it to work.
DirectoryInfo directory = new DirectoryInfo("mydirectory/News Files");
FileSystemInfo[] files = directory.GetFiles("*.pdf");
var orderedFiles = files.OrderByDescending(f => f.Name);
foreach (FileSystemInfo file in orderedFiles)
{
var link = new HyperLink { ID = file.FullName };
link.NavigateUrl ="/News Files/"+ file.Name;
link.Text = Regex.Split(file.Name, ".pdf")[0];
link.CssClass = "linkpdf";
newsListContainer.Controls.Add(link);
}
But with this code file with the name like my20%sign.pdf will not open in the browser.
You could try Uri.EscapeUriString.
Also, you shouldn't construct urls/filenames using string concatenation with /. You should usually use a Uri/filename parsing library, such as the Uri class
That's not surprising. The %20 is interpreted by the browser as a "white space", as it is the url encoded equivalent value. So if your file is named "My%20File.pdf", the browser will decode the url and actually look for "My File.pdf".
For further reference, check this: http://www.w3schools.com/tags/ref_urlencode.asp
You can replace %20 with " ". filename.replace("%20"," ");
I have a path "C:\Users\Web References"
Under the "Web References" folder, i have *.wsdl file
I want to get the full filename of the *.wsdl file
Thanks!
Something like this:
var files = Directory.GetFiles("C:\\Users\\Web References", "*.wsdl", SearchOption.AllDirectories);
This will return a collection of files - there could be more than one wsdl file in the directory. Take the first:
var wsdlFile = files.FirstOrDefault();
Seeing that no-one is mentioning it: Path.GetFullPath()
First, you have to find it:
var files = Directory.GetFiles(path, "*.wsdl");
and now files will contain full paths to all WSDL files (if any) in path.
// find files by filter
var result = Directory.GetFiles("C:\\Users\\Web References\\", "*.wsdl");
//if you have only one file
return System.IO.Path.GetFileName(result[0]); // "my.wsdl"
The Path class will help you extract just the filename from a file path:
string path = #"C:\Users\Web References";
string[] files = Directory.GetFiles(path, "*.wsdl");
foreach (string filePath in files) {
string filename = Path.GetFileName(filePath); // e.g. myFile.wsdl
}
or using linq
var files = from f in Directory.GetFiles((#"C:\Users\Web References")
where f.EndsWith(".wsdl")
select f;
foreach (var file in files)
...