C# help - File exists

Soldato
Joined
6 Mar 2008
Posts
10,082
Location
Stoke area
Hi all,

We've an issue at work that it appear some staff are incapable of reading a process and checking files exists, so i'm knocking something up quick:

Code:
Console.WriteLine(file_date);
         
            if (File.Exists(@t1_directory_sent1 + "acbd_1234_" + file_date + ".txt"))
            {
                Console.WriteLine("acbd_1234_" + file_date + ".txt - " + "The file exists.");
            }
            if (File.Exists(@t1_directory_sent1 + "acbd_1234_" + file_date + ".aud"))
            {
                Console.WriteLine("acbd_1234" + file_date + ".aud - " + "The file exists.");
            }

If it was just these 2 that'd be easy but in total we've a total of around 53 files to check. They could be in 1 of 3 locations, have txt, aud, gpg and csv file extensions.

Some also have a file_date of yyyyMMdd and some yyyyMMdd HHMMSS so I'll need to wildcard those ones.

Can anyone suggest a quicker way of doing this without manually typing them all out?

Obviously, a simple loop through but I'm unsure how to store all the variations to get this to function as expected.

Thanks
 
Managed to make it a little more elegant and easier to read.

Basically, there are 2 teams receiving 4 chunks of extracts from 2 different places, each place has a storage and a sent area, so 4 areas in total.

Each of the 4 directories into a variable for each one.
there are 4 lists, 3 for file names, 1 for a filename that only runs on a Thursday
4 loops that go through the data and output whether it's in location 1, location 2 or hasn't been created/sent
A counter to show the total number of files in the lists vs the total sent / not sent


under 300 line of code, not too bad for my 2nd c# console app
 
Ok, my Thursday files are different format: ABCD_Q1_YYYYMMDD_HHMMSS.CSV

Code:
            List<string> filesThurs = new List<string>();
            filesThurs.Add("ABCD_Q1_" + file_date + ?? + ".csv");
            filesThurs.Add("ABCD_Q2_" + file_date + ?? + ".csv");
            filesThurs.Add("ABCD_Q3_" + file_date + ?? + ".csv");
            filesThurs.Add("ABCD_Q4_" + file_date + ?? + ".csv");
            filesThurs.Add("ABCD_Q5_" + file_date + ?? + ".csv");

So, I need to add a wildcard to each add.

I'd seen a webpage a few days ago where substituting the ?? for the * wildcard but I can't get it to work.

Am I going to be better just using the "filesThurs.Add("ABCD_Q1_" + file_date);" and then adding the wild card during the loop looking in the directory or can this be done as part of the add to the lists? I'm thinking it's the first option but wanted clarification from those with more experience (so most people :D )

A job for Regex? seeing it mentioned a lot on searches, something new to learn

It seems that File.Exists can't be used with wildcards, new approach needed
 
Last edited:
Back
Top Bottom