473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Find files after name changed.

125 100+
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
Aug 19 '08 #1
7 1177
cloud255
427 Expert 256MB
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.
Aug 19 '08 #2
r035198x
13,262 8TB
There is a String.StartsWith method ...
Aug 19 '08 #3
asedt
125 100+
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.
Aug 19 '08 #4
tlhintoq
3,525 Expert 2GB
*IF* your program is always running, maybe minimized or as a service you could use a FileSystemWatcher with an event for FileSystemWatcher.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 FileSystemWatcher.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 FileSystemWatcher and you're all but done. No complex logic about how to handle multiple similar named files, no mistakes about which file was changed.
Aug 20 '08 #5
asedt
125 100+
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.

Expand|Select|Wrap|Line Numbers
  1. Dim RegexObj2 As System.Text.RegularExpressions.Regex
  2. RegexObj2 = New System.Text.RegularExpressions.Regex("[\x20\w\.-]*[^\\]$", RegexOptions.IgnoreCase)
  3.  
  4. Dim RegexObj3 As System.Text.RegularExpressions.Regex
  5. RegexObj3 = New System.Text.RegularExpressions.Regex("^[\x20\w-]*[^\.]", RegexOptions.IgnoreCase)
  6.  
  7. Dim RegexObj4 As System.Text.RegularExpressions.Regex
  8. RegexObj4 = New System.Text.RegularExpressions.Regex("_rev", RegexOptions.IgnoreCase)
  9.  
  10. Dim RegexObj5 As System.Text.RegularExpressions.Regex
  11. RegexObj5 = New System.Text.RegularExpressions.Regex("^[\x20\w-]*(?:_rev)", RegexOptions.IgnoreCase)
  12.  
  13.  
  14. If Not File.Exists(TheFilePathString) Then
  15.    If Directory.Exists(Path.GetDirectoryName(TheFilePathString)) Then
  16.       Dim di As New DirectoryInfo(Path.GetDirectoryName(TheFilePathString))
  17.       Dim fi As FileInfo() = di.GetFiles()
  18.       Dim fiTemp As FileInfo
  19.       If Not RegexObj4.IsMatch(TheFilePathString) Then
  20.          For Each fiTemp In fi
  21.             If RegexObj.IsMatch(fiTemp.Extension) And fiTemp.Name.StartsWith(RegexObj3.Match(RegexObj2.Match(TheFilePathString).ToString).ToString) Then
  22.                ListBox1.Items.Add(fiTemp.FullName)
  23.                MsgBox("This file was not found: " & vbCrLf & TheFilePathString & vbCrLf & vbCrLf & "This file was used: " & vbCrLf & fiTemp.FullName, MsgBoxStyle.Information)
  24.             End If
  25.          Next fiTemp
  26.       Else 
  27.          For Each fiTemp In fi
  28.             If RegexObj.IsMatch(fiTemp.Extension) And fiTemp.Name.StartsWith(RegexObj5.Match(RegexObj2.Match(TheFilePathString).ToString).ToString) Then
  29.                ListBox1.Items.Add(fiTemp.FullName)
  30.                MsgBox("This file was not found: " & vbCrLf & TheFilePathString & vbCrLf & vbCrLf & "This file was used: " & vbCrLf & fiTemp.FullName, MsgBoxStyle.Information)
  31.             End If
  32.          Next fiTemp
  33.       End If
  34.    Else
  35.       MsgBox("The folder " & Path.GetDirectoryName(TheFilePathString) & " does not exist." & vbCrLf & "The file: " & TheFilePathString & " was not found.", MsgBoxStyle.Critical)
  36.    End If
  37. Else
  38.    ListBox1.Items.Add(TheFilePathString)
  39. 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.fileinfo.name(VS.80).aspx

MSDN regular expressions:
http://msdn.microsoft.com/en-us/library/az24scfc(VS.71).aspx
Aug 20 '08 #6
Plater
7,872 Expert 4TB
You can use the Directory object to search for them. It takes a search pattern string as an argument
Expand|Select|Wrap|Line Numbers
  1. string path=@"c:\files\";
  2. string startofname="12345";
  3. string[] files=Directory.GetFiles(Path, startofname+"*.pdf");
  4.  
That will return any files matching your search pattern. You should be able to adapt that to suit your needs.
Aug 20 '08 #7
asedt
125 100+
You can use the Directory object to search for them. It takes a search pattern string as an argument
Expand|Select|Wrap|Line Numbers
  1. string path=@"c:\files\";
  2. string startofname="12345";
  3. string[] files=Directory.GetFiles(Path, startofname+"*.pdf");
  4.  
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?
http://en.wikipedia.org/wiki/Glob_(programming)

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 "startofname" because "1234.pdf" gives "1234" and "1234_rev1.pdf" also gives "1234"

But I can use:
Directory.GetFiles(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...
Aug 21 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
10
by: Chris Simon | last post by:
A bit puzzled by this... 1. Ask the validator to look at http://www.nantperis.org.uk/index.php.en (the exact filename of the page) and it brings back an error: 300 Multiple Choices. I could...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
4
by: Kathie via AccessMonster.com | last post by:
Hello, I have to import monthly, files that were once *.csv but due to commas in addresses, the interface program was changed to dump tab delimited. Now my code is not finding the files in the...
0
by: Xah Lee | last post by:
Interactive Find and Replace String Patterns on Multiple Files Xah Lee, 2006-06 Suppose you need to do find and replace of a string pattern, for all files in a directory. However, you do not...
5
by: peter | last post by:
Hello all, I'm looking for an advice. Example (one block in ascii file): $------------------------ NAME='ALFA' CODE='x' $------------------------
1
by: Steve Richter | last post by:
the win32 API documentation references a lot of symbolic values. FILE_SHARE_READ, GENERIC_WRITE, ... http://msdn2.microsoft.com/en-us/library/ms959950.aspx are the actual values enumerated at...
5
by: rogersw8n | last post by:
Some how, some way the account that creates folders under Temporary Internet files has been changed to a domain account for VS 2003 and VS 2005. I recently installed VS 2005. All seemed to be ok...
0
by: nimjerry | last post by:
i am using db2 udb V 9 on aix 5.3 and in db2diag.log alwas has this error occurr below is sample message 2008-03-03-09.45.34.366406+420 I306667A443 LEVEL: Warning PID : 835622 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.