Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem moving files 1

Status
Not open for further replies.

cruise95

Programmer
Oct 7, 2003
56
US
I am looping through a directory and performing these actions on every file:

1. Getting the file's information via fileInfo
2. Grabbing data from MS SQL and making a dataTable
3. Reading the files via a streamReader
4. moving the file to another directory
5. Adding this a record with the file information to MS SQL

For 99% of the files this works fine. For 1 or 2 files (out of 100) the files are not moved. I put a try/catch around the move operation and output the error and this is what I get:
System.IO.IOException: The process cannot access the file "U:\UPSDBS\PAPRLESS\review\CB1\prnt1.R21845HP.J0062752.085847aq0000.txt" because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Move(String sourceFileName, String destFileName)
at PreProcessor.MainForm.ProcessReport(String filePath) in D:\Documents and Settings\dbb8fnd\My Documents\Visual Studio Projects\Infrastructure\Infrastructure4.6\MainForm.vb:line 631


I put a while around the move operation:

Dim moveFile = True
While (moveFile = True)
Try
System.IO.File.Move(sourceDir, destDir)
moveFile = False

Catch
moveFile = True
End Try
End While

and all the files are moved OK.

At first I thought that the file sizes were causing the problem but the file sizes (of those that are not being moved) are much smaller than many others that are moved over.

Question:
I don't understand why only 99% of the files are moved over without the While? Where am I going wrong?

Thanx
 
I'm not sure about this, but you need to make sure that the streamreader in
3. Reading the files via a streamReader
has closed.
 
Right before I move the files, I check that they can be read from. Upon stepping through, the files can be read fine. It's not until the move command executes that I receive an error. Here's what I am doing:


Code:
Dim filePathArray() As String = System.IO.Directory.GetFiles(sourceDirectory, "prnt1*.*")
Dim filePath As String

For Each filePath In filePathArray
...

  Dim moveFile = True
  While (moveFile = True)
    Dim sr As System.IO.StreamReader = New System.IO.StreamReader(filePath)
    sr.Close()

    Try
      System.IO.File.Move(sourceDir, destDir)
      moveFile = False

    Catch
      moveFile = True
    End Try
  End While
End For

Every time I am only getting the error:
MOVE EXCEPTION: System.IO.IOException: The process cannot access the file "U:\UPSDBS\PAPRLESS\review\CB\prnt1.R21845HP.J0062752.085847aq0000.txt" because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Move(String sourceFileName, String destFileName)
at PreProcessorLibrary.Report.MoveReport(String destinationDirectory)
at PreProcessor.MainForm.ProcessReport(String filePath) in D:\Documents and Settings\dbb8fnd\My Documents\Visual Studio Projects\Infrastructure\Infrastructure4.8 + multiThreading\MainForm.vb:line 622

Also the files are being moved from U:\UPSDBS\PAPRLESS\review to U:\UPSDBS\PAPRLESS\review\CB
and the error claims that this filename in the CB directory (the directory where the file is being moved to) cannot be read from.


I have no idea why this is happening! If I take out the while loop then the file is not moved. With the while loop, then after about 12 trys the file is moved successfully.

Does this make any sense to anybody?
 
Try this:

Code:
Imports System.IO

dim SourcePath As String = "C:\BlahBlah"
dim TargetPath As String = "D:\BlahBlah"
Dim myFileList As New DirectoryInfo(SourcePath)
Dim myFileInfo As FileInfo
For Each myFileInfo In myFileList.GetFiles
   Try
      File.Move(myFileInfo.FullName, TargetPath & myFileInfo.Name)
   Catch ex As Exception
      MsgBox(ex.Message)
   End Try
Next

It moves 339 files in 1 second on my P4 computer without any error.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top