How to check if a file exists in a folder?
This is a way to see if any XML-files exists in that folder, yes.
To check for specific files use File.Exists(path)
, which will return a boolean indicating wheter the file at path
exists.
C# class to check if files exists in folder
No, it doesn't like return check;
at the end - what did you want it to do? Your method is check
- so "return true or false based on check
" doesn't really make sense. Did you actually mean to return isThere
instead?
It's not clear what the parameter is even really meant to be there for, to be honest... and if you've asked a DirectoryInfo
for the files in that directory, I'd personally expect them to exist - otherwise why would they be returned?
Oh, and your directory name contains a tab where I suspect you actually want a backslash followed by a t
.
I suspect your method would be better as:
public static bool TempDirectoryContainsXmlFiles()
{
DirectoryInfo di = new DirectoryInfo(@"c:\temp");
return di.GetFiles("*.xml").Length > 0;
}
or using LINQ for clarity:
public static bool TempDirectoryContainsXmlFiles()
{
DirectoryInfo di = new DirectoryInfo(@"c:\temp");
return di.GetFiles("*.xml").Any();
}
(You can use EnumerateFiles
as shown by BrokenGlass which is more efficient for a large directory, but it's only available in .NET 4 and upwards.)
how to check if ANY file exists in a certain folder with python?
will list the files in the folder folder
script is running from main
folder
files
...
main.py
will list only files, not .
and ..
import os
dir = os.path.dirname(__file__) or '.'
dir_path = os.path.join(dir, '../folder/')
onlyfiles = [f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f))]
How to check if file exist on current folder
File.Exists is checking file, not a folder, also you need to provide a full physical path for file like @"C:\mydata\TempFile.txt" to work.
I totally confused if you want to check for file or folder.
If you are testing for folder=> use Directory.GetDirectories it will return an array of all subdirectories.
If you are testing for file=>relative file will be save in [Solution_Path][Project_Name][filename.extention] I recommend you using below code to verify you looking to correct path or using an absolute path.
var test_file=File.Create("test.txt");
Console.WriteLine(test_file.Name);
How to Check if a File Exists in a Directory in Java
You can test whether a file or directory exists using the Java File exists()
method.
File file = new File("/temp.txt");
boolean cond1 = file.exists();
The method will return true when the file or directory is found and false if the file is not found.
You can also use the Files.exists()
and Files.notExists()
Java methods if that is easier too.
There are also the constants Constants.FILE_EXISTS
and Constants.FILE_EXISTS
you can use to test conditions.
C# Checking to see if files exist in folder
I am assuming you want to get all the files from a folder. You can use System.IO.Directory.GetFiles to do this:
string mypath = "C:/myfolder/";
string[] files = Directory.GetFiles(mypath);
You can then run through all the files like this:
foreach(string item in files){
if(item.Contains("my file name without the extension")){
//Do Something
}
}
-----------EDIT #1
string mypath = "C:/myfolder/";
string[] files = Directory.GetFiles(mypath);
if(!files.Length == 0){
foreach(string item in files){
//Run rest of code using the item
}
}else{}
the foreach statement's string variable contains the file path of the file.
-----------EDIT #2
If an error occurs at the first if statement saying that the "!" operator cannot be applied to integers, then instead put the foreach code into the else statement.
string mypath = "C:/myfolder/";
string[] files = Directory.GetFiles(mypath);
if(files.Length == 0){
//do nothing. No files are in the folder.
}else{
foreach(string item in files){
//Do something. Files are in the folder.
}
}
From my understanding of the question, inside the foreach statement would be the code to process and archive the file. The string called "item" should contain the path to the file.
How to Check file exists in Laravel
add one more additional condition to check given path is file but not a directory
public function file_fetch(Request $request) {
$file = request('routename');
$destinationPath = public_path('/folder/'.$file);
if(!File::exists($destinationPath) && !is_dir($destinationPath)){
$content = File::get($destinationPath);
return view('filefetch', compact('file','content'));
}
else {
return redirect('/')->witherrormessage('NO such File Exists');
}
}
Related Topics
Finding a Subsequence in Longer Sequence
Caliburn.Micro Serialization Issue When Implementing Propertychangedbase
Can the C# Interactive Window Interact with My Code
Automatically Update Version Number
: ? Operators Instead of If|Else
Case Statement Block Level Declaration Space in C#
Regex Split String But Keep Separators
Filesystemwatcher Fails to Access Network Drive
Uploading Files Not Working - Need Assistance
Horizontal Scroll for Stackpanel Doesn't Work
Queuing in Oneway Wcf Messages Using Windows Service and SQL Server
How to Draw Shapes in Winforms
Difference Between System.Datetime.Now and System.Datetime.Today
Private Inner Classes in C# - Why Aren't They Used More Often
How to Get the Path of the Current User's "Application Data" Folder
How to Create a Friendly Url in ASP.NET MVC
Reference Types Live on the Heap, Value Types Live on the Stack