0

我编写了一些代码,旨在将我 PC 上的所有电影分类到字母表中每个字母的子文件夹中(例如,“An Example”将进入仅包含以字母“A”开头的电影的子文件夹中。

我编写的代码在我看来应该可以毫无问题地工作,尽管出于某种原因,这段代码:

'Declarations
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")

For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
    If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
        System.IO.Directory.Move(f.ToString, mainFolder.Name & UCase(Left(f.Name, 1)) & "\" & f.Name)
    ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
        System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
    ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
        System.IO.Directory.Move(f.ToString, TS.ToString & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
    End If
Next

在这一行不断抛出异常:

System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))

这是一个例外:

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in
 mscorlib.dll

Additional information: Could not find a part of the path 
'D:\Users\Yorrick\documents\visual studio 2012\Projects\filmsort\filmsort\bin\Debug\(CAM)The Internship'.

如上面的声明所示,我不知道这段代码如何或为什么试图访问该文件夹。

如果有人可以看看并希望发现我所犯的错误,那将不胜感激。

编辑:

解决了上述问题后,我遇到了一个新问题。使用下面的代码,我现在得到了同样的异常,除了这次附加信息说“找不到路径的一部分”。我所有的变量在调试时似乎都是正确的,所以我真的不明白为什么这不起作用。

注意:注释行是我尝试过的,它给了我System.IO.IOException: Cannot create a file when that file already exists.

代码:

System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")

For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
    If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
        System.IO.Directory.Move(f.FullName, mainFolder.FullName & UCase(Left(f.Name, 1)) & "\" & f.Name)
    ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
        'System.IO.Directory.CreateDirectory(CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
        System.IO.Directory.Move(f.FullName, CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
    ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
        System.IO.Directory.Move(f.FullName, TS.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
    End If
Next
4

2 回答 2

1

当您没有指定完整路径名(例如“c:\foo\bar”)而是指定相对路径名(例如“bar”)时,就会发生这种情况。通过预先添加 Environment.CurrentDirectory 将相对路径名转换为完整路径名。默认情况下,它是您项目的构建目录。

这是因为您使用了 DirectoryInfo.Name。对于路径为 c:\foo\bar 的目录,它是“bar”。您必须改用 FullName 属性。

于 2013-09-21T12:38:15.510 回答
0

我只是重写了一点,我认为这应该足以满足您的目的。

System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")

For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
    If f.Name.ToUpper.Contains("(TS)") Then
        System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(TS, f.Name))
    ElseIf f.Name.ToUpper.Contains("(CAM)") Then
        System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(CAM, f.Name))
    End If
Next
于 2013-09-21T13:01:15.670 回答