Sure,
I use FileInfo and DirectoryInfo instead of File and Directory so that will
differ in my example. I also use a regular expression to isolate the
extension.
To change the extension: InputString is the original file name, new extension
is "working" in my case, and Expression is (^.+\.)([^?.]+)($)
public string ChangeFileExtention(String InputString, String NewExtention,
String Expression) {
if (InputString.LastIndexOf('.') > 0) {
Regex myExpression = new Regex(Expression);
InputString = Regex.Replace(InputString,
myExpression.ToString(), "$1" + NewExtention);
}
return InputString;
}
copy method:
public void CopyTheFile(String FullPathToFile, String NewPath){
FileInfo myFile = new FileInfo(FullPathToFile);
if (myFile.OpenRead().CanRead) {
myFile.Copy(NewPath + ChangeFileExtention(myFile.Name, "working",
"(^.+\.)([^?.]+)($)");
}
rename method:
public void RenameTheFile(String FullPathToFile){
FileInfo myFile = new FileInfo(FullPathToFile);
if (myFile.OpenRead().CanRead) {
myFile.Copy(NewPath + ChangeFileExtention(myFile.Name, "doc",
"(^.+\.)([^?.]+)($)");
}
Basically I check to make sure I can read the file (not locked) if not I start
copying it. I work with HUGE files so I change the extension so my FileWatcher
doesn't try to try to work with the file before I am done with it. I have the
file watcher looking at a tight mask such as myFile*.doc so the myFile.working
doesn't trigger it. I would first write a method that attempts to copy from
C:\temp to C:\temp\new\ This way you aren't messing with spaces in the file
path and can validate that the copy works. I just coded this in outlook so
don't chastise me if there are a few bugs in the code :-)
Travis D. Falls | Consultant RAFT.Net IT | 860.547.4070 | [EMAIL PROTECTED]
-----Original Message-----
From: [email protected]
[mailto:[EMAIL PROTECTED] Behalf Of
e_throckmorton
Sent: Monday, December 12, 2005 2:44 PM
To: [email protected]
Subject: [AspNetAnyQuestionIsOk] Re: File System watcher
I see what you mean. I can get the file to copy but not the shole
thing. So, I kind of follow you in what you are talking about...
name the new file:
file.copy(existingfile, newfile)
well thats really all I have so far but its not coppying the whole
file to the newfile. could you possibly go over the .working scheme
for me a little more in depth?
Thanks,
Eric
--- In [email protected], "Falls, Travis D
(HTSC, CASD)" <[EMAIL PROTECTED]> wrote:
>
> Eric,
> Are you sure that the file is finished being copied before you
start to use it? I wrote a pretty extensive system based on the
file system watcher and ran into that issue. I ended up changing
extensions to .working until I was done copying the file. That way
if my mask was StockInfo_*.dat I wouldn't even see the
TEMP_StockInfo_*.working and simply renamed it when finished. Just
a thought.
>
> Travis D. Falls | Consultant RAFT.Net IT | 860.547.4070 |
[EMAIL PROTECTED]
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[EMAIL PROTECTED] Behalf Of
> e_throckmorton
> Sent: Monday, December 12, 2005 1:43 PM
> To: [email protected]
> Subject: [AspNetAnyQuestionIsOk] Re: File System watcher
>
>
> I tried that code and got the following error:
> System.UnauthorizedAccessException: Access to the path is denied.
at
> System.IO.__Error.WinIOError(Int32 errorCode, String str) at
> System.IO.__Error.WinIOError() at System.IO.FileInfo.MoveTo(String
> destFileName) at readtextfile.WebForm1.MoveFile(String slocFile,
> String sdesFile) in
> c:\inetpub\wwwroot\readtextfile\WebForm1.aspx.vb:line 170
>
> Any ideas why?
> Thanks, Eric
>
>
>
> --- In [email protected], shannon hall
> <[EMAIL PROTECTED]> wrote:
> >
> >
> > Private Sub MoveFile(ByVal slocFile As String,
> > ByVal sdesFile As String)
> > Try
> > ' Create a reference to a file, which
> > might or might not exist.
> > ' If it does not exist, it is not yet
> > created.
> > Dim fi As New FileInfo(slocFile)
> > ' Create a writer, ready to add
> > entries to the file.
> >
> > If File.Exists(sdesFile) Then
> > ' Ensure that the target file does
> > not exist, since this is disallowed.
> > File.Delete(sdesFile)
> > fi.MoveTo(sdesFile)
> > Else
> > ' Move this file to another file.
> > fi.MoveTo(sdesFile)
> > End If
> >
> > File.Delete(slocFile)
> > fi = Nothing
> > Catch ex As Exception
> > SendErrorMail(ex.Message)
> > End Try
> >
> >
> > End Sub
> > --- Eric Throckmorton <[EMAIL PROTECTED]>
> > wrote:
> >
> > > Hello,
> > > I have a file system watcher looking for a
> > > creation of new files then running some code to use
> > > that file, then deleteing it.
> > > The first thing I do is copy the file to another
> > > location using the File.copy method but the entire
> > > file does not copy to the second location. What Am
> > > I doing wrong here?
> > >
> > > Private Sub objWatcher_FileCreated(ByVal
> > > FullPath As String) Handles objWatcher.FileCreated
> > > TextBox1.Text &= "File Created: " & FullPath &
> > > vbCrLf
> > > path = "C:\Documents and Settings\Eric\Local
> > > Settings\Application
> > >
> > Data\Identities\{CD27FCB2-9E3C-45F7-BC37-93C84C481291}
> \Microsoft\Outlook
> > > Express\Inbox.dbx"
> > > path2 = "C:\Documents and Settings\Eric\My
> > > Documents\Test\filecopy.txt"
> > > Try
> > > ' Ensure that the target does not exist.
> > > File.Delete(path2)
> > > ' Copy the file.
> > > File.Copy(path, path2)
> > > Console.WriteLine("{0} copied to {1}", path,
> > > path2)
> > > ' Try to copy the same file again, which should
> > > fail.
> > > File.Copy(path, path2)
> > > Console.WriteLine("The second Copy operation
> > > succeeded, which was not expected.")
> > > Catch exp As Exception
> > > Console.WriteLine("The second Copy operation
> > > failed, as expected.")
> > > End Try
> > > 'gatherdata1()
> > > End Sub
> > >
> > >
> > > Thank-you,
> > >
> > > Eric
> > >
> > >
> > >
> > > ---------------------------------
> > > Yahoo! Shopping
> > > Find Great Deals on Holiday Gifts at Yahoo!
> > > Shopping
> > >
> > > [Non-text portions of this message have been
> > > removed]
> > >
> > >
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam? Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
> >
>
>
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
>
*********************************************************************
****
> This communication, including attachments, is
> for the exclusive use of addressee and may contain proprietary,
> confidential and/or privileged information. If you are not the
intended
> recipient, any use, copying, disclosure, dissemination or
distribution is
> strictly prohibited. If you are not the intended recipient,
please notify
> the sender immediately by return e-mail, delete this communication
and
> destroy all copies.
>
*********************************************************************
****
>
Yahoo! Groups Links
------------------------ Yahoo! Groups Sponsor --------------------~-->
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/KIlPFB/vlQLAA/TtwFAA/saFolB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/