Find files after name changed.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asedt
    New Member
    • Jun 2008
    • 130

    Find files after name changed.

    I have a program that stores the paths to files, sometimes these files change names when they are updated. And then my program can't find the file any more. The name is always changed in the same manner. Like if it was 12345.pdf and it's updated it is changed to something like 12345_rev2.pdf.

    Can I in some way make so my program it looking for 12345*.pdf in the same folder as the old file was in if it does not find the old one.

    Working with Visual basic in Visual studio 2005.

    thx
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    well that is quite a situation...

    You could use regular expressions to search the directory with the 12345*.pdf approach, but there is a issue you need to consider:

    What if several files match the criteria? if your program automatically opens a file, this would be a problem.

    my suggestion would be to use the open file dialog. perhaps have a functionality where the user can specify what a filename has changed to. Where the user can select a file form the program then navigate to the new filename and then update the programs internal data.

    hope this helped.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      There is a String.StartsWi th method ...

      Comment

      • asedt
        New Member
        • Jun 2008
        • 130

        #4
        Originally posted by cloud255
        well that is quite a situation...

        You could use regular expressions to search the directory with the 12345*.pdf approach, but there is a issue you need to consider:

        What if several files match the criteria? if your program automatically opens a file, this would be a problem.

        my suggestion would be to use the open file dialog. perhaps have a functionality where the user can specify what a filename has changed to. Where the user can select a file form the program then navigate to the new filename and then update the programs internal data.

        hope this helped.
        Thanks, now I think I know how I can do. I will try with list the filles in a directory and check them with regular expressions or the "starts with" method for a match and then tell the user the file was not found and give the match as an option.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          *IF* your program is always running, maybe minimized or as a service you could use a FileSystemWatch er with an event for FileSystemWatch er.Changed. This would give you the old name and the new name so you could change your saved settings at the time the name changed.

          Of course you have to ask yourself *how* does the file's name get changed? If the document is opened, worked on, closed, renamed then you're fine.
          But if a second file is saved with the new name (_rev2), then the old file is deleted then the filename isn't really being changed so the FileSystemWatch er.Changed event doesn't raise. You would have to watch for a more complex sequence of events.

          Again, this only works if your program is running when the filename is changed.
          You could of course write a seperate service that just watches the given folders for name changes. Let the tiny app run all the time and only load your "full fledged" application when the user wants it. I might go this route because its so little work. You have already coded the routines for how you save your settings. Copy/paste that into a new project, give it a FileSystemWatch er and you're all but done. No complex logic about how to handle multiple similar named files, no mistakes about which file was changed.

          Comment

          • asedt
            New Member
            • Jun 2008
            • 130

            #6
            I have now a working solution, maybe not the best but i have tested it for some time and it's working for me.

            The code use regular expressions, and when the old file is not found it loks in the folder of the old file for a "rev". If the old file was a "rev" it also finds newer ones like rev2. And then it add the path to a listbox.

            Code:
            Dim RegexObj2 As System.Text.RegularExpressions.Regex
            RegexObj2 = New System.Text.RegularExpressions.Regex("[\x20\w\.-]*[^\\]$", RegexOptions.IgnoreCase)
            
            Dim RegexObj3 As System.Text.RegularExpressions.Regex
            RegexObj3 = New System.Text.RegularExpressions.Regex("^[\x20\w-]*[^\.]", RegexOptions.IgnoreCase)
            
            Dim RegexObj4 As System.Text.RegularExpressions.Regex
            RegexObj4 = New System.Text.RegularExpressions.Regex("_rev", RegexOptions.IgnoreCase)
            
            Dim RegexObj5 As System.Text.RegularExpressions.Regex
            RegexObj5 = New System.Text.RegularExpressions.Regex("^[\x20\w-]*(?:_rev)", RegexOptions.IgnoreCase)
            
            
            If Not File.Exists(TheFilePathString) Then
               If Directory.Exists(Path.GetDirectoryName(TheFilePathString)) Then
                  Dim di As New DirectoryInfo(Path.GetDirectoryName(TheFilePathString))
                  Dim fi As FileInfo() = di.GetFiles()
                  Dim fiTemp As FileInfo
                  If Not RegexObj4.IsMatch(TheFilePathString) Then
                     For Each fiTemp In fi
                        If RegexObj.IsMatch(fiTemp.Extension) And fiTemp.Name.StartsWith(RegexObj3.Match(RegexObj2.Match(TheFilePathString).ToString).ToString) Then
                           ListBox1.Items.Add(fiTemp.FullName)
                           MsgBox("This file was not found: " & vbCrLf & TheFilePathString & vbCrLf & vbCrLf & "This file was used: " & vbCrLf & fiTemp.FullName, MsgBoxStyle.Information)
                        End If
                     Next fiTemp
                  Else 
                     For Each fiTemp In fi
                        If RegexObj.IsMatch(fiTemp.Extension) And fiTemp.Name.StartsWith(RegexObj5.Match(RegexObj2.Match(TheFilePathString).ToString).ToString) Then
                           ListBox1.Items.Add(fiTemp.FullName)
                           MsgBox("This file was not found: " & vbCrLf & TheFilePathString & vbCrLf & vbCrLf & "This file was used: " & vbCrLf & fiTemp.FullName, MsgBoxStyle.Information)
                        End If
                     Next fiTemp
                  End If
               Else
                  MsgBox("The folder " & Path.GetDirectoryName(TheFilePathString) & " does not exist." & vbCrLf & "The file: " & TheFilePathString & " was not found.", MsgBoxStyle.Critical)
               End If
            Else
               ListBox1.Items.Add(TheFilePathString)
            End If
            Sry for no comments in code, I did't write them in english so i just removed them.

            MSDN get filnames from folder:
            http://msdn.microsoft. com/en-us/library/system.io.filei nfo.name(VS.80) .aspx

            MSDN regular expressions:
            http://msdn.microsoft. com/en-us/library/az24scfc(VS.71) .aspx

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              You can use the Directory object to search for them. It takes a search pattern string as an argument
              [code=c#]
              string path=@"c:\files \";
              string startofname="12 345";
              string[] files=Directory .GetFiles(Path, startofname+"*. pdf");
              [/code]
              That will return any files matching your search pattern. You should be able to adapt that to suit your needs.

              Comment

              • asedt
                New Member
                • Jun 2008
                • 130

                #8
                Originally posted by Plater
                You can use the Directory object to search for them. It takes a search pattern string as an argument
                [code=c#]
                string path=@"c:\files \";
                string startofname="12 345";
                string[] files=Directory .GetFiles(Path, startofname+"*. pdf");
                [/code]
                That will return any files matching your search pattern. You should be able to adapt that to suit your needs.
                Thanks i will se what I can do.

                That looks like globbing?


                That may work in some of the cases maybe i can simplifie the code some, but I still need to use some regular expressions in many cases one to get the "startofnam e" because "1234.pdf" gives "1234" and "1234_rev1. pdf" also gives "1234"

                But I can use:
                Directory.GetFi les(FolderPath, "*.pdf");

                To list pdfs in the folder... right?, thats usefull to know.
                edit: have to test if it do list .PDF, .Pdf and so on to...

                Comment

                Working...