Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 Other Development Tools
 rename in vb.net

Author  Topic 

jhermiz

3564 Posts

Posted - 2005-05-10 : 15:53:12
I need a way to rename a file without knowing the EXACT name of the file..for instance I need to use wild card *.

For instance say I have c:\temp\test1242325365.txt

I would like to be able to rename this in my app
like:


fi = New FileInfo("c:\temp\test*.txt")

If fi.Exists() Then
fi.MoveTo(c:\temp2\zgpsout.txt")
End If


I want to be able to say test* rather than the actual name of the file because I will never know the exact name..all I will know is that it may start with "test" and end with ".txt".


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]

MichaelP
Jedi Yak

2489 Posts

Posted - 2005-05-10 : 16:22:04
[code]
Dim dirInfoRoot As DirectoryInfo
Dim dirInfo As DirectoryInfo
Dim aryDirInfo As DirectoryInfo()
Dim fileInfo As FileInfo
Dim aryFileInfo As FileInfo()
If dir.Exists("insert path here or variable for path") Then
dirInfoRoot = New DirectoryInfo("the search path here too")
aryFileInfo = dirInfoRoot.GetFiles
("test*.txt")

For Each fileInfo In aryFileInfo
'perform action
Next
[/code]

Let me know if you have questions.
Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-05-10 : 16:44:55
Hmm I dont see how that will help me consider this:


Private Sub RenameFiles()
Dim fi As FileInfo

Try
fi = New FileInfo(strDownloadsPath & "\ZGPSIN0001_OUTBOUND_.txt")

'zgpsout text file
If fi.Exists() Then
fi.MoveTo(strDownloadsPath & "\zgpsout.txt")
End If

fi = Nothing
fi = New FileInfo(strDownloadsPath & "\ZGCOIN0001_OUTBOUND1_.txt")

If fi.Exists Then
fi.MoveTo(strDownloadsPath & "\zgcoout1.txt")
End If

fi = Nothing
fi = New FileInfo(strDownloadsPath & "\ZGCOIN0001_OUTBOUND2_.txt")

If fi.Exists Then
fi.MoveTo(strDownloadsPath & "\zgcoout2.txt")
End If
Catch ex As Exception
MailException("An error occurred when renaming text files." & Chr(13) & Chr(13) & ex.ToString())
Application.Exit()
Finally
fi = Nothing
End Try

End Sub


After every underscore right before ".txt" I need to wild card it so that it uses that file. In addition, I just want the app to pick up the file name closest to that name....and not worry about the rest of the name.



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2005-05-10 : 18:41:45
Sounds like you need to do some regular expression stuff to make that happen.

I'm afraid I can't really help you there man.
Sorry!

Mike

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-05-10 : 22:05:23
:(

Anyone else ?




Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2005-05-11 : 11:27:27
Jon,
I think what you should be able to do is use the code I provided to find the list of files like the ones you want.
Then, for each file do some string parsing on the full name of that file to determine what the destination file name should be. Then do a move / rename on the file to get the name you want. Should be able to be done without Regular Expressions. If I had a bit of time, I'd write it fo ya, but I'm really short on time these days.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-05-11 : 11:34:51
I got it mike, had to do some string manip.

thanks for the heads up.


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page
   

- Advertisement -