Results 1 to 9 of 9

Thread: [RESOLVED] [02/03] Renaming... (really confused)

  1. #1

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Resolved [RESOLVED] [02/03] Renaming... (really confused)

    I have a program that monitors a directory and if any new files come in, they get renamed and are moved to another file.

    The way I move the files are that I set the full filename of the item like this

    VB Code:
    1. fullFilename = e.FullPath

    The new renamed filename is set to a variable like this:

    VB Code:
    1. newFilename = branchName & "_" & finalDate & "_" & sPicNum
    2. fullNewFilename = selectedPath & "\" & newFilename

    Now, when the images are found in the folder, the first item goes and the path gets stored, but once it renames that one file and finishes the job, the next one doesn't work. It is still keeping the path of the old filename in e.FullPath when it is set to fullFilename. Because this is still set in e.FullPath, it screws up my entire program after the first image is renamed. Anyone know what I can do to clear this up? Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Renaming... (really confused)

    I'm assuming that this is all happening in an event handler for a FileSystemWatcher but it would be nice if I didn't have to assume. How about you show us the entire method?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] Renaming... (really confused)

    I didn't want to because it was pretty long...I took half of the code out of the renaming process. Everything after is just writing to a database.

    Here is the code:

    VB Code:
    1. Private Sub fsWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsWatcher.Created
    2.         'If a new file is created in the folder that is being monitored,
    3.         'Then this code will execute
    4.  
    5.         'Write activity to log
    6.         txtActivity.Text = txtActivity.Text & DateAndTime.Now & " File Created: " & e.FullPath & vbNewLine
    7.         writeToLogFile(DateAndTime.Now & " File Created: " & e.FullPath)
    8.         txtActivity.Update()
    9.  
    10.         'Determine long and truncated filenames
    11.         fullFilename = e.FullPath
    12.         shortFilename = Mid$(fullFilename, InStrRev(fullFilename, "\") + 1)
    13.         truncatedFilename = Split(shortFilename, "_")
    14.  
    15.         If shortFilename.Substring(Len(shortFilename) - 3) = "jpg" Or shortFilename.Substring(Len(shortFilename) - 3) = "JPG" Or shortFilename.Substring(Len(shortFilename) - 4) = "JPEG" Or shortFilename.Substring(Len(shortFilename) - 4) = "jpeg" Then
    16.             Application.DoEvents()
    17.             StatusBar.Panels(0).Text = "File found! Starting Renaming Process..."
    18.         ElseIf shortFilename.Substring(Len(shortFilename) - 3) = "CSV" Or shortFilename.Substring(Len(shortFilename) - 3) = "csv" Then
    19.             txtActivity.Text = txtActivity.Text & "*** Log File Found.  Moving to log directory... ***" & vbNewLine
    20.             writeToLogFile("*** Error: Invalid file detected.  Moved to Exception Directory. ***")
    21.             IO.File.Move(fullFilename, csvFilePath & "\" & shortFilename)
    22.             Exit Sub
    23.         Else
    24.             txtActivity.Text = txtActivity.Text & "*** Error: Invalid file detected.  Moved to Exception Directory. ***" & vbNewLine
    25.             writeToLogFile("*** Error: Invalid file detected.  Moved to Exception Directory. ***")
    26.             IO.File.Move(fullFilename, selectedExceptionPath & "\" & shortFilename)
    27.             Exit Sub
    28.         End If
    29.  
    30.         'Split filename into different parts
    31.         sOrder = truncatedFilename(0)
    32.         sBranch = truncatedFilename(1)
    33.         sDateTime = truncatedFilename(2)
    34.         sPicNum = truncatedFilename(3)
    35.         nDate = sDateTime.Substring(0, 6)
    36.         nTime = sDateTime.Substring(6, 6)
    37.         tmpDate = nDate.Insert(2, "-")
    38.         finalDate = tmpDate.Insert(5, "-")
    39.  
    40.         'Determine from the branch number the state or city abbreviation
    41.         Select Case sBranch
    42.             Case "01"
    43.                 branchName = "Corp"
    44.             Case "02"
    45.                 branchName = "PA"
    46.             Case "03"
    47.                 branchName = "GA"
    48.             Case "04"
    49.                 branchName = "MA"
    50.             Case "05"
    51.                 branchName = "CO"
    52.             Case "06"
    53.                 branchName = "MO"
    54.             Case "07"
    55.                 branchName = "SF"
    56.             Case "08"
    57.                 branchName = "TX"
    58.             Case "09"
    59.                 branchName = "LA"
    60.             Case "10"
    61.                 branchName = "FL"
    62.         End Select
    63.  
    64.         'Store new filename into variable
    65.         newFilename = branchName & "_" & finalDate & "_" & sPicNum
    66.  
    67.         'Log current activity
    68.         txtActivity.Text = txtActivity.Text & DateAndTime.Now & " Begin File Renaming: " & e.FullPath & vbNewLine
    69.         writeToLogFile(DateAndTime.Now & " Begin File Renaming: " & e.FullPath)
    70.         txtActivity.Update()
    71.  
    72.         Dim fullNewFilename As String
    73.  
    74.         'Determine full new filename (i.e. C:\input\fl_5-22-06_03.jpg)
    75.         fullNewFilename = selectedPath & "\" & newFilename
    76.  
    77.         'Lets the files finish writing before being accessed by the scanning program
    78.         Pause(100)
    79.  
    80.         'Rename files
    81.         If IO.File.Exists(fullNewFilename) = True Then
    82.             txtActivity.Text = txtActivity.Text & "*** Error: File exists in Monitoring Directory! File " & fullFilename & " has not been renamed. *** " & vbNewLine
    83.             writeToLogFile("*** Error: File exists in Monitoring Directory! File " & fullFilename & " has not been renamed. *** ")
    84.             txtActivity.Update()
    85.         Else
    86.             If IO.File.Exists(fullNewFilename) = True Then
    87.                 txtActivity.Text = txtActivity.Text & "*** Error writing file " & fullNewFilename & ".  File already exists! ***" & vbNewLine
    88.                 writeToLogFile("*** Error writing file " & fullNewFilename & ".  File already exists! ***")
    89.                 txtActivity.Update()
    90.             Else
    91.                 MsgBox("Full Filename: " & fullFilename & vbNewLine & "Full New Filename: " & fullNewFilename)
    92.                 IO.File.Move(fullFilename, fullNewFilename)
    93.             End If
    94.         End If
    95.  
    96.         'Database Code here
    97. End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Renaming... (really confused)

    You should learn to use the IO.Path class. Methods like GetFileName, GetFileNameWithoutExtension and GetExtension make your code cleaner and self-documenting. Also, Pause(100) is a very inaccurate way of doing what you want there I would think. There's no guarantee that you've waited long enough. You should pause and then check the file's attributes to see if the OffLine attribute is set. If so then it has not finished being written and you should pause again. You should keep pausing and checking in a loop until the OffLine attribute is not set.

    Now, what exactly is the original issue? I'm not really sure what you're asking.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] Renaming... (really confused)

    Ah, I didn't know about the offline tag. The pause was just there to allow the program to wait for a split second and catch up. I didn't think it was that clean.

    Also, I'm still getting used to .net. There are so many new good tools that i'm getting used to.

    The original question was, everytime this code executes it will work for the first file, but after it executes the old filename still gets stored in the variable. The e.FullPath function doesn't catch up and I don't know why. I added a Pause(100) to the end of the function and it worked, but I know this isn't a clean way of doing such a thing.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Renaming... (really confused)

    I still don't really understand what you mean. FileSystemEventArgs.FullPath is a property, not a method, and it is ReadOnly. It's value shouldn't change over the course of the method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] Renaming... (really confused)

    It doesn't change over the method. Each time it's executed meaning if it detects 2 files that are created, it will parse through one time using the directory of the first file it found. One that has completed, it will execute again and it is supposed to use the directory of the second file. For some reason, it won't use the second directory and only use the directory of the first file.

  8. #8

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] Renaming... (really confused)

    Anyone know?

  9. #9

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: [02/03] Renaming... (really confused)

    I found out it was the Application.DoEvents(). It kept jumping up and rereading and looping each file through e.FullPath. I took out the Application.DoEvents() line and it all worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width