Home > 日記 > カオスなフォルダを整理するスクリプトを書いてみた

カオスなフォルダを整理するスクリプトを書いてみた

  • Posted by: logicerror
  • 2008年3月22日 19:07
  • 日記

|ω・)ノ 気がつくと、マイピクチャがカオスなことになっていたlogicerrorの中の人です。

ということで、整理するVBスクリプトを書いて見た。
実行するとカレントフォルダにあるファイルを順番にチェックし、ファイル更新日のフォルダ(yyyymmdd形式)を作ってそこへ移動します。
移動先に同じファイルがある場合はエラーメッセージを表示します。ファイルは移動しません。
フォルダは対象外です。

option explicit
Dim objFS   'ファイルシステムオブジェクト
Dim crrDir  'カレントディレクトリ
Dim crrPath 'カレントディレクトリのパス
Dim files   'カレントディレクトリのファイルコレクション
Dim modDate 'ファイル更新日時をYYYYMMDD形式で
Dim subDir  'カレントディレクトリのサブディレクトリコレクション
Dim muchFlg '更新日時の一致する名前のサブフォルダがあったかフラグ
Dim mvFile  '移動するファイル
DIm toDir   '移動先

Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
Set crrDir = objFs.GetFolder(".")
crrPath = crrDir.Path

On Error Resume Next

For Each files in crrDir.Files                  'カレントディレクトリにあるファイルをGet
  if (files.Name <> WScript.Scriptname) and (files.Name <> "Thumbs.db") then
    modDate = dateStr(files.DateLastModified)   'ファイルの更新日時をGet
    muchFlg = false
    For Each subDir in crrDir.Subfolders        '更新日時のフォルダがカレントディレクトリにあるか
      if subDir.Name = modDate then
        muchFlg = true
        exit For
      end if
    Next
    if muchFlg = false then                         'なかったら作る
      objFs.CreateFolder crrPath & "\" & modDate
    end if
    
    'ファイルを更新日時のフォルダへ移動
    mvFile = crrPath & "\" & files.Name
    toDir = crrPath & "\" & modDate & "\"
    objFs.MoveFile mvFile,toDir
    if Err Then
      msgBox Err.Description & vbcrlf _
        & "対象ファイル:" & mvFile & vbcrlf _
        & "移動先:" & toDir
    end if
  end if
Next

Function dateStr(tgtDate)                           '日付型をYYYYMMDD形式の文字列にする
  Dim s
  s = Year(tgtDate)
  if Month(tgtDate) < 10 then                       '1月~9月の場合はゼロを補う
    s = s & "0" & Month(tgtDate)
  else
    s = s & Month(tgtDate)
  end if
  if Day(tgtDate) < 10 then                         '1日~9日の場合はゼロを補う
    s = s & "0" & Day(tgtDate)
  else
    s = s & Day(tgtDate)
  end if
  
  dateStr = s
End Function

使い方:
整理したいフォルダにテキストファイルを作る。→上のスクリプトを全部コピーして、テキストファイルに貼り付け、保存する。→拡張子をvbsにする。→ダブルクリックする。→わはー。

|ω・) Win2kとXPなら動くんじゃないかな。vistaは知らにゃい。
使用した結果何があっても当方は一切関知しません。使う・使わないの判断は自己責任ですにょろ。

Trackbacks:0

TrackBack URL for this entry
http://logicerror.sakura.ne.jp/mt42/mt-tb.cgi/1616
Listed below are links to weblogs that reference
カオスなフォルダを整理するスクリプトを書いてみた from logicerror

Home > 日記 > カオスなフォルダを整理するスクリプトを書いてみた

Twitter Updates
    follow me on Twitter
    web拍手
    web拍手を送る [?]
    Feeds

    track feed

    Return to page top