移动文件时的错误处理

编程入门 行业动态 更新时间:2024-10-26 15:14:01
本文介绍了移动文件时的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经环顾网络,但找不到任何可以帮助我的东西。 我能够获得一些代码将一些文件从一个目录移到 另一个,工作正常: ======================== ============ 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

更多推荐

移动文件时的错误处理

本文发布于:2023-08-01 20:16:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271367.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!