Hey all,

I know this is probably easy but I'm going cross eyed looking at my code. When I used

List<string> SOID; //SOID is the database name for the filepath
       List<string> filesInDatabase = GetDatabaseFileName(out SOIDs);
       List<string> filesOnServer = GetServerFileNames();
       
       foreach ( string fileName in filesOnServer ){
            string fileWpath = fileName;
            string fileOnly = Path.GetFileNameWithoutExtension(fileName).ToString().ToLower();
            if (filesInDatabase.Contains(fileOnly) ){
                int index = filesInDatabase.IndexOf(fileOnly);
                if (index >= 0){
                    if ( !File.Exists(SOIDs[index]) ){

everything works fine. When I needed to check those which the database did not contain the fileOnly by changing:
if (filesInDatabase.Contains(fileOnly) ){
to
if (!filesInDatabase.Contains(fileOnly) ){
after adding the ! - the program doesn't work properly. While stepping through it I noticed that the index is -1 every time.

Um, first thing that comes to my mind is that you define SOID as List<String> but.. you then refer to SOIDs... whats SOIDs ?

sorry that's a typo.

for the record -the actual code says SOIDs in all places. The code works when I'm checking those files that do exist, it only breaks when I switch it to check those that do not.

Then we need more of the code, because you only show up to the start of the decision making process.. If the file was not found then yes the index will be -1 because its not on the list.. whats the actual error you get?

I'm not getting an error message at all it just doesn't copy out the missing files. That's where I'm confused - so you're saying that file.contains will show a negative 1 if the file isn't found?

So I switched it to

foreach ( string fileName in filesOnServer ){
            string fileWpath = fileName;
            string fileOnly = Path.GetFileNameWithoutExtension(fileName);
            int index = filesOnServer.IndexOf(fileOnly) ;
            if (index >= 0)
            {
           if (!filesInDatabase.Contains(fileOnly) ){

and index is 0 until it hits the !filesInDatabase line then it becomes -1. So that explains why it is -1 thanks.

any idea how to check then to see them if the file isn't in the database?

secondary - why doesn't index change to -1 when it gets to

if ( !File.Exists(SOIDs[index]) ){

List<string> SOIDs;
       List<string> filesInDatabase = GetDatabaseFileName(out SOIDs);
       List<string> filesOnServer = GetServerFileNames();
       foreach ( string fileName in filesOnServer ){
            string fileWpath = fileName;
            string fileOnly = Path.GetFileNameWithoutExtension(fileName);
            int index = filesOnServer.IndexOf(fileOnly) ;
            if (index >= 0){
           if (!filesInDatabase.Contains(fileOnly) ){                          
                    if ( !File.Exists(SOIDs[index]) ){
                        txtFiles.AppendText(fileName + "   " + SOIDs[index] +  "\r\n");
                        File.Copy(fileWpath,SOIDs[index] ); //have to finish this part of the code
                        Application.DoEvents(); 
                    }
                }
            }
        }
        MessageBox.Show("Completed File Matching");
      }
    protected static List<string> GetDatabaseFileName( out List<string> SOID){
        List<string> returnValue = new List<string>();
        SOID = new List<string>();
        using ( SqlConnection connection = new SqlConnection(@"Server=prod;UID=id;PWD=pass;Database=image")){
            connection.Open();                        
                using ( SqlCommand command = connection.CreateCommand() ){
                    command.CommandType = CommandType.Text;
                    command.CommandText = @"select FILENAME, SOID from obj";

                    using ( SqlDataReader dataReader = command.ExecuteReader(CommandBehavior.CloseConnection) ){
                        while ( dataReader.Read() ){
                            returnValue.Add(dataReader.GetString(0).ToLower());
                            SOID.Add(dataReader.GetString(1));
           
                        }
                    }
                }           
        }
        return returnValue;
        }
        protected static List<string> GetServerFileNames(){
            List<string> returnValue = new List<string>();
            if (Directory.Exists("\\\\disk2\\images\\Missing Images")){
                string[] files = Directory.GetFiles("\\\\disk2\\images\\Missing Images");
                returnValue.AddRange(files);
            }
            return returnValue;
        }
foreach ( string fileName in filesOnServer ){
            string fileWpath = fileName;
            string fileOnly = Path.GetFileNameWithoutExtension(fileName);
            int index = filesOnServer.IndexOf(fileOnly) ;
            if (index >= 0)
            {
           if (!filesInDatabase.Contains(fileOnly) ){

foreach ( string fileName in filesOnServer ){ string fileWpath = fileName; string fileOnly = Path.GetFileNameWithoutExtension(fileName); int index = filesOnServer.IndexOf(fileOnly) ; if (index >= 0) { if (!filesInDatabase.Contains(fileOnly) ){

and index is 0 until it hits the !filesInDatabase line then it becomes -1. So that explains why it is -1 thanks.

OK here you look at files on the server and find they arent in the db, so you need to add them, this is cool.

any idea how to check then to see them if the file isn't in the database?

Sure, now do the same tests but with the db and server roles reversed so get the filenames in the db and check if they are on the server or not. If not perhaps you need to delete the entry in db or resave the file to the server.. depending on your need.

secondary - why doesn't index change to -1 when it gets to

if ( !File.Exists(SOIDs[index]) ){

because thats a test, not a value setting.

before you had the code

int index = filesOnServer.IndexOf(fileOnly) ;

where you set index, if you dont set that to something else it wont change :)

Thanks!

I got it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.