我的上一篇文章是我在论坛上发表的第一篇文章,在我回来检查之前就已经关闭了,所以这里再次详细讨论我的问题。
Const SrcFilePath = "C:\Folder 1\Temp\"
WScript.echo "SrcFilePath = " & SrcFilePath
Const FileExtension = ".txt"
newdate = date()-1
Set fso = CreateObject("Scripting.FileSystemObject")
If Day(newdate)>9 Then
ExtensionDay = Day(newdate)
Else
ExtensionDay = "0"&Day(newdate)
End If
If Month(newdate)>9 Then
ExtensionMonth = Month(newdate)
Else
ExtensionMonth = "0"&Month(newdate)
End If
If Year(newdate)>9 Then
ExtensionYear = Year(newdate)
Else
ExtensionYear = Year(newdate)
End If
Mon = MonthName(ExtensionMonth, true)
Yer = Right(ExtensionYear,2)
DateTag = ExtensionDay & "_" & ExtensionMonth & "_" & ExtensionYear
DateTag1 = ExtensionYear
DestFileName = "Test File_" & DateTag & FileExtension
WScript.echo DestFileName
SrcFile = SrcFilePath & "Test File_" & DateTag & FileExtension
Dest_File = "D:\Test 1\" & ExtensionYear & "\"
WScript.echo "Copy from =" & SrcFile, "Copy to =" & Dest_File
Fso.CopyFile SrcFile, Dest_File
上面的代码将获得一个文件名中包含昨天日期的文件,并将其移动到文件中包含年份的文件夹。
以下是我希望它做的事情
“C:\ Folder 1 \ Temp \”文件夹中包含以下文件
C:\ Folder 1 \ Temp \ Test 1_2012_10_25.txt
C:\ Folder 1 \ Temp \ Test 2_2013_08_25.txt
C:\ Folder 1 \ Temp \ Test 3_2011_10_25.txt
C:\ Folder 1 \ Temp \ Test 4_2010_10_25.txt
我希望这些文件根据文件名和文件名移动到文件夹,如下所示。我上面的代码只为昨天的日期做了1个文件。我希望它循环并组织文件夹中的所有文件。
C:\ Folder 1 \ Temp \ Test 1_2012_10_25.txt> D:\ Test 1 \ 2012 \ Test 1_2012_10_25.txt
C:\ Folder 1 \ Temp \ Test 2_2013_08_25.txt> D:\ Test 2 \ 2013 \ Test 2_2013_08_25.txt
C:\ Folder 1 \ Temp \ Test 1_2012_10_25.txt> D:\ Test 3 \ 2011 \ Test 3_2012_10_25.txt
C:\ Folder 1 \ Temp \ Test 1_2012_10_25.txt> D:\ Test 1 \ 2012 \ Test 1_2012_10_25.txt
如果文件夹On D:\不存在则创建它们。
由于
答案 0 :(得分:1)
我仍然声称稍微修改了my answer here中的代码:
Const csSrc = "..\data\in2"
Const csDst = "..\data\out2"
Dim f, n, d
For Each f In goFS.GetFolder(csSrc).Files
n = Split(f.Name, "_") ' split on _ instead of -
If 3 = UBound(n) Then ' 4 parts instead of 2
d = goFS.BuildPath(csDst, n(1))
If Not goFS.FolderExists(d) Then goFS.CreateFolder d
f.Move goFS.BuildPath(d, f.Name)
End If
Next
解决您的问题。证据:
tree /a /f ..\data\in2
Test 7_2012_11_25.txt
Test 1_2010_11_25.txt
Test 2_2010_12_25.txt
Test 9_2012_13_25.txt
Test 8_2012_12_25.txt
Test 3_2010_13_25.txt
Test 6_2011_13_25.txt
Test 4_2011_11_25.txt
Test 5_2011_12_25.txt
tree /a /f ..\data\out2
+---2011
| Test 6_2011_13_25.txt
| Test 4_2011_11_25.txt
| Test 5_2011_12_25.txt
|
+---2010
| Test 1_2010_11_25.txt
| Test 2_2010_12_25.txt
| Test 3_2010_13_25.txt
|
\---2012
Test 7_2012_11_25.txt
Test 9_2012_13_25.txt
Test 8_2012_12_25.txt
更新 - 使用年份目录的父文件夹的文件名的第一个(“测试n”)部分:
只需应用相同的策略:
For Each f In goFS.GetFolder(csSrc).Files
n = Split(f.Name, "_")
If 3 = UBound(n) Then
d = goFS.BuildPath(csDst, n(0))
If Not goFS.FolderExists(d) Then goFS.CreateFolder d
d = goFS.BuildPath(d, n(1))
If Not goFS.FolderExists(d) Then goFS.CreateFolder d
f.Move goFS.BuildPath(d, f.Name)
End If
Next