0

フォーラムへの最初の投稿であった私の最後の投稿は、確認する前に閉じられたので、ここでもう一度詳細に質問します。

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:\フォルダ 1\Temp\Test 1_2012_10_25.txt

C:\フォルダ 1\Temp\テスト 2_2013_08_25.txt

C:\フォルダ 1\Temp\Test 3_2011_10_25.txt

C:\フォルダ 1\Temp\Test 4_2010_10_25.txt

これらのファイルを、以下のようにファイル名とファイル名から年に基づいてフォルダに移動してください。上記の私のコードは、昨日の日付に対して 1 つのファイルのみを実行します。フォルダー内のすべてのファイルをループして整理したいと思います。

C:\Folder 1\Temp\Test 1_2012_10_25.txt > D:\Test 1\2012\ テスト 1_2012_10_25.txt

C:\Folder 1\Temp\Test 2_2013_08_25.txt > D:\Test 2\2013\ テスト 2_2013_08_25.txt

C:\Folder 1\Temp\Test 1_2012_10_25.txt > D:\Test 3\2011\ テスト 3_2012_10_25.txt

C:\Folder 1\Temp\Test 1_2012_10_25.txt > D:\Test 1\2012\ テスト 1_2012_10_25.txt

また、D:\ のフォルダーが存在しない場合は、それらを作成します。

ありがとう

4

1 に答える 1

1

ここでの回答のコードをわずかに変更したと今でも主張しています。

  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

更新 - year ディレクトリの親フォルダーのファイル名の最初の部分 (「Test 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
于 2013-01-21T17:12:31.540 に答える