移动文件时的错误处理 [英] Error handling when moving files
问题描述
我已经环顾网络,但找不到任何可以帮助我的东西。
我能够获得一些代码将一些文件从一个目录移到
另一个,工作正常:
======================== ============
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
尝试
Dim SourceDir As String =" C:\ test \"
Dim DestinationDir As String = " \\\\\\\\\\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\
对于i as Integer = 0到UBound(f)
System.IO.File.Move(f(i),DestinationDir&" \"& ;
FileNameWithoutThePath(f(i)))
下一页i
Catch ex As Exception
MsgBox(ex.Message.ToString,MsgBoxStyle.Critical," Error!")
结束尝试
结束子
公共函数FileNameWithoutThePath(ByVal b As String)As String
Dim j As Int16
j = Convert.ToInt16(b.LastIndexOf(" \"))
返回b.Substring(j + 1)
结束函数
====================================
我遇到的问题是文件已存在于目标
目录中,或者它正在尝试移动的文件是否正在使用(已锁定)且错误
发生。我需要能够跳过问题文件并转到下一个要移动的
文件。我已经尝试了各种各样的东西,比如在代码的各个部分递增i(i = i +1)
但是这没有用。
Any我将不胜感激!
TIA,
Jim
I''ve looked around the web but can''t find anything to help me out.
I was able to get some code to move some files from one directory to
another, which works fine:
====================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim SourceDir As String = "C:\test\"
Dim DestinationDir As String = "\\server1\share\test\"
Dim f() As String = System.IO.Directory.GetFiles(SourceDir)
For i As Integer = 0 To UBound(f)
System.IO.File.Move(f(i), DestinationDir & "\" &
FileNameWithoutThePath(f(i)))
Next i
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error!")
End Try
End Sub
Public Function FileNameWithoutThePath(ByVal b As String) As String
Dim j As Int16
j = Convert.ToInt16(b.LastIndexOf("\"))
Return b.Substring(j + 1)
End Function
====================================
The problem I run into is when the file already exists in the destination
directory or if the file it''s trying to move is in use (locked) and error
occurs. I need to be able to skip the problem file and move on to the next
file to be moved. I have tried various things like incrementing i (i = i +1)
in various parts of the code but this hasn''t worked.
Any assistance or direction would be appreciated!
TIA,
Jim
推荐答案
吉姆
移动你的for i 下一个以便尝试,捕捉,结束尝试在
循环里面
这应该可以解决你的问题....它过去对我有用
问候
Michael Bond
吉姆在亚利桑那州写道:
Jim
move your "for i" "next i" so that the "try, catch, end try" is inside the
loop
That should solve your problem....it has for me in the past
Regards
Michael bond
"Jim in Arizona" wrote:
我已经环顾网络,但找不到任何可以帮助我的东西。
我能够得到一些代码将一些文件从一个目录移动到
另一个,这样可以正常工作:
========= ===========================
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
尝试
Dim SourceDir As String =" C:\ test \
Dim DestinationDir As String =" \\server1\share\test\"
Dim f()As String = System.IO.Directory.GetFiles(SourceDir)
对于i as Integer = 0到UBound(f)
System.IO.File.Move( f(i),DestinationDir&" \"&
FileNameWithoutThePath(f(i)))
Next i
Catch ex As Exception
MsgBox(ex.Message.ToString,MsgBoxStyle.C ritical,错误!)
结束尝试
结束子
公共函数FileNameWithoutThePath(ByVal b As String)As String
Dim j As Int16
j = Convert.ToInt16(b.LastIndexOf(" \))
返回b.Substring(j + 1)
结束函数
====================== ==============
我遇到的问题是文件已存在于目的地
目录中或如果它正在尝试移动的文件正在使用(锁定)并且出现错误
。我需要能够跳过问题文件并转到下一个要移动的
文件。我已经尝试了各种各样的东西,比如在代码的各个部分递增i(i = i +1)
但是这没有用。
Any感谢您的帮助或指导!
TIA,
Jim
I''ve looked around the web but can''t find anything to help me out.
I was able to get some code to move some files from one directory to
another, which works fine:
====================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim SourceDir As String = "C:\test\"
Dim DestinationDir As String = "\\server1\share\test\"
Dim f() As String = System.IO.Directory.GetFiles(SourceDir)
For i As Integer = 0 To UBound(f)
System.IO.File.Move(f(i), DestinationDir & "\" &
FileNameWithoutThePath(f(i)))
Next i
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error!")
End Try
End Sub
Public Function FileNameWithoutThePath(ByVal b As String) As String
Dim j As Int16
j = Convert.ToInt16(b.LastIndexOf("\"))
Return b.Substring(j + 1)
End Function
====================================
The problem I run into is when the file already exists in the destination
directory or if the file it''s trying to move is in use (locked) and error
occurs. I need to be able to skip the problem file and move on to the next
file to be moved. I have tried various things like incrementing i (i = i +1)
in various parts of the code but this hasn''t worked.
Any assistance or direction would be appreciated!
TIA,
Jim
Jim
不习惯回复...更习惯问......所以忘了给你
的建议....其内容如下
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
Dim SourceDir As String =" C:\\\\"
Dim DestinationDir As String =" \\\\\\\\\\\\\\ br />
Dim f()As String = System.IO.Directory.GetFiles(SourceDir)
For i As Integer = 0 To UBound(f )
尝试
System.IO.File.Move(f(i),DestinationDir&" \"&
FileNameWithoutThePath(F(1)))<无线电通信/>
Catch ex As Exception
MsgBox(ex.Message.ToString,MsgBoxStyle.Critical," Error!")
结束尝试
接下来我
结束次级
吉姆在亚利桑那州写道:
Jim
not used to replying...more used to asking....so forgot to give you the
suggestion....its as follows
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim SourceDir As String = "C:\test\"
Dim DestinationDir As String = "\\server1\share\test\"
Dim f() As String = System.IO.Directory.GetFiles(SourceDir)
For i As Integer = 0 To UBound(f)
Try
System.IO.File.Move(f(i), DestinationDir & "\" &
FileNameWithoutThePath(f(i)))
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error!")
End Try
Next i
End Sub
"Jim in Arizona" wrote:
我已经环顾网络,但找不到任何可以帮助我的东西。
我能够得到一些代码将一些文件从一个目录移动到
另一个,这样可以正常工作:
========= ===========================
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
尝试
Dim SourceDir As String =" C:\ test \
Dim DestinationDir As String =" \\server1\share\test\"
Dim f()As String = System.IO.Directory.GetFiles(SourceDir)
对于i as Integer = 0到UBound(f)
System.IO.File.Move( f(i),DestinationDir&" \"&
FileNameWithoutThePath(f(i)))
Next i
Catch ex As Exception
MsgBox(ex.Message.ToString,MsgBoxStyle.C ritical,错误!)
结束尝试
结束子
公共函数FileNameWithoutThePath(ByVal b As String)As String
Dim j As Int16
j = Convert.ToInt16(b.LastIndexOf(" \))
返回b.Substring(j + 1)
结束函数
====================== ==============
我遇到的问题是文件已存在于目的地
目录中或如果它正在尝试移动的文件正在使用(锁定)并且出现错误
。我需要能够跳过问题文件并转到下一个要移动的
文件。我已经尝试了各种各样的东西,比如在代码的各个部分递增i(i = i +1)
但是这没有用。
Any感谢您的帮助或指导!
TIA,
Jim
I''ve looked around the web but can''t find anything to help me out.
I was able to get some code to move some files from one directory to
another, which works fine:
====================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim SourceDir As String = "C:\test\"
Dim DestinationDir As String = "\\server1\share\test\"
Dim f() As String = System.IO.Directory.GetFiles(SourceDir)
For i As Integer = 0 To UBound(f)
System.IO.File.Move(f(i), DestinationDir & "\" &
FileNameWithoutThePath(f(i)))
Next i
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error!")
End Try
End Sub
Public Function FileNameWithoutThePath(ByVal b As String) As String
Dim j As Int16
j = Convert.ToInt16(b.LastIndexOf("\"))
Return b.Substring(j + 1)
End Function
====================================
The problem I run into is when the file already exists in the destination
directory or if the file it''s trying to move is in use (locked) and error
occurs. I need to be able to skip the problem file and move on to the next
file to be moved. I have tried various things like incrementing i (i = i +1)
in various parts of the code but this hasn''t worked.
Any assistance or direction would be appreciated!
TIA,
Jim
对不起应该添加我的建议样本:
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1。点击
Dim SourceDir As String =" C:\ test \"
Dim DestinationDir As String =" \\\\\ \\ share\test \"
Dim f()As String = System.IO.Directory.GetFiles(SourceDir)
对于i As Integer = 0到UBound(f)
尝试
System.IO.File.Move(f(i),DestinationDir&" \" &
FileNameWithoutThePath(f(i)))
Catch ex As Exception
MsgBox(ex.Message.ToString,MsgBoxStyle.C ritical,错误!)
结束尝试
下一页我
结束子
>
Michael
吉姆在亚利桑那州写道:
Sorry should have added a sample of my suggestion :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim SourceDir As String = "C:\test\"
Dim DestinationDir As String = "\\server1\share\test\"
Dim f() As String = System.IO.Directory.GetFiles(SourceDir)
For i As Integer = 0 To UBound(f)
Try
System.IO.File.Move(f(i), DestinationDir & "\" &
FileNameWithoutThePath(f(i)))
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error!")
End Try
Next i
End Sub
Michael
"Jim in Arizona" wrote:
我已经环顾网络,但找不到任何可以帮助我的东西。
我能够得到一些代码将一些文件从一个目录移动到
另一个,这样可以正常工作:
========= ===========================
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理Button1.Click
尝试
Dim SourceDir As String =" C:\ test \
Dim DestinationDir As String =" \\server1\share\test\"
Dim f()As String = System.IO.Directory.GetFiles(SourceDir)
对于i as Integer = 0到UBound(f)
System.IO.File.Move( f(i),DestinationDir&" \"&
FileNameWithoutThePath(f(i)))
Next i
Catch ex As Exception
MsgBox(ex.Message.ToString,MsgBoxStyle.C ritical,错误!)
结束尝试
结束子
公共函数FileNameWithoutThePath(ByVal b As String)As String
Dim j As Int16
j = Convert.ToInt16(b.LastIndexOf(" \))
返回b.Substring(j + 1)
结束函数
====================== ==============
我遇到的问题是文件已存在于目的地
目录中或如果它正在尝试移动的文件正在使用(锁定)并且出现错误
。我需要能够跳过问题文件并转到下一个要移动的
文件。我已经尝试了各种各样的东西,比如在代码的各个部分递增i(i = i +1)
但是这没有用。
Any非常感谢您的帮助或指导!
TIA,
Jim
I''ve looked around the web but can''t find anything to help me out.
I was able to get some code to move some files from one directory to
another, which works fine:
====================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Dim SourceDir As String = "C:\test\"
Dim DestinationDir As String = "\\server1\share\test\"
Dim f() As String = System.IO.Directory.GetFiles(SourceDir)
For i As Integer = 0 To UBound(f)
System.IO.File.Move(f(i), DestinationDir & "\" &
FileNameWithoutThePath(f(i)))
Next i
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error!")
End Try
End Sub
Public Function FileNameWithoutThePath(ByVal b As String) As String
Dim j As Int16
j = Convert.ToInt16(b.LastIndexOf("\"))
Return b.Substring(j + 1)
End Function
====================================
The problem I run into is when the file already exists in the destination
directory or if the file it''s trying to move is in use (locked) and error
occurs. I need to be able to skip the problem file and move on to the next
file to be moved. I have tried various things like incrementing i (i = i +1)
in various parts of the code but this hasn''t worked.
Any assistance or direction would be appreciated!
TIA,
Jim
这篇关于移动文件时的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!