根据上次访问的日期移动文件 [英] Moving files based on date last accessed
问题描述
我似乎在让这段代码工作时遇到问题
Dim目录为新DirectoryInfo( C:\\\archive \)
Dim files As FileInfo()= directory.GetFiles()
对于每个文件在文件中
If(file.LastAccessTime< DateTime.Now.AddDays(-1))然后
Dim fp As String
fp = file.FullName
System.IO.File.Move(fp, C:\ test\delete \ )
结束如果
下一步
我不能似乎找到了原因,但它在mscorlib.dll中遇到了第一次机会异常
我尝试过:
我尝试了几种不同的方法来尝试移动文件,但它只是不想。
我在尝试什么要做的是非常基本的 - 如果目录中的文件超过一天,将其转移到另一个目录。
我一直在寻找一些不同的论坛尝试找到不同的方法,但他们都做同样的事情。我尝试过只使用file.moveto而不将路径转换为字符串,并且在调试时遇到了同样的问题
,它会选择正确的完整文件的路径和名称,以及要复制的目录是可用的。
我犯了一个愚蠢的错误!
我仍然需要有我要复制的文件的名称。
应该如下
Dim目录As New DirectoryInfo( C:\ test \ archive \)
Dim files As FileInfo()= directory.GetFiles()
For Each file in files
If(file.LastAccessTime< DateTime.Now.AddDays(-1))Then
Dim fp As String
fp = file.FullName
System.IO.File.Move(fp, C:\ test\delete \ & file.Name& )
结束如果
下一步
I seem to be having problems getting this code to work
Dim directory As New DirectoryInfo("C:\test\archive\")
Dim files As FileInfo() = directory.GetFiles()
For Each file In files
If (file.LastAccessTime < DateTime.Now.AddDays(-1)) Then
Dim fp As String
fp = file.FullName
System.IO.File.Move(fp, "C:\test\delete\")
End If
Next
I can't seem to find why, but it is hitting a first chance exception in mscorlib.dll
What I have tried:
I have tried a few different ways to try and shift the files, but it just doesn't want to.
What I am trying to do is very basic - if the file within the directory is older than one day, shift it to another directory.
I've been looking at a few different forums to try and find different ways to do it, but they all do the same thing. I've tried just using the file.moveto without converting the path to a string and it comes up with the same issue
while debugging, it picks up the correct full path and name for the file, and the directory being copied to is available.
I had made a silly mistake!
I still need to have the name of the file that I want to copy.
It should have been as follows
Dim directory As New DirectoryInfo("C:\test\archive\") Dim files As FileInfo() = directory.GetFiles() For Each file In files If (file.LastAccessTime < DateTime.Now.AddDays(-1)) Then Dim fp As String fp = file.FullName System.IO.File.Move(fp, "C:\test\delete\" & file.Name & "") End If Next
这篇关于根据上次访问的日期移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!