WindowsXPで扱えるファイルパス長は256文字までの認識です。
256文字を超えたファイルがある場合、削除やコピーなどができないようになってしまいます。
上記のようなファイルを探すためにVBScriptにて256文字を超えたファイルが存在する
対象フォルダを再回帰的に検索し、ファイル一覧を出したのですが、256文字を超えている
ファイルは検索できず、一覧に表示されない状態です。
256文字を超えたファイルを検索し、パスとファイル名を検索できる方法はありますでしょうか
出来ましたらVBScriptのサンプルを作っていただけると幸いです。
また、ポイントにつきましては基本的には1番回答に近いものにあてさせていただきます。
const ForReading = 1 ' -------------------------------------------------------------------- ' フォルダ内のファイル一覧の取得 Redim path_list(0) GetFDate "c:\", path_list ' フォルダ内のファイル一覧の出力 FListWrite "c:\out.txt", path_list ' -------------------------------------------------------------------- ' フォルダ内のファイル一覧の取得---------- Public Sub GetFDate(ByVal fol_path, ByRef path_list()) Set fso = CreateObject("Scripting.FileSystemObject") For Each f In fso.GetFolder(fol_path).files ' ファイルのPATH、更新日時、サイズの設定 ReDim Preserve path_list(UBound(path_list) + 1) if len(f.path)>255 then path_list(UBound(path_list)) = f.path end if ' ------------------------------------------------------ Next For Each f In fso.GetFolder(fol_path).SubFolders GetFDate f.path, path_list Next Set fso = Nothing End Sub ' -------------------------------------------------------------------- ' フォルダ内のファイル一覧の出力---------- Private Sub FListWrite(ByVal out_path, ByRef path_list()) Set fso = CreateObject("Scripting.FileSystemObject") Set txf = fso.CreateTextFile(out_path, True) For i = 1 To UBound(path_list) if len(path_list(i))>0 then txf.WriteLine path_list(i) end if Next txf.Close Set txf = Nothing Set fso = Nothing End Sub ' --------------------------------------------------------------------
ドライブと 出力ファイル名などを 変更して 使ってみてください。
http://homepage2.nifty.com/nonnon/Chinamini/20100001/20101012.html
どのような状況でそうなったのかにもよると思いますが、
ショートファイルネームでも取得できませんか?
(targetFolderとoutputFileの二箇所はそちらの環境に合わせて適宜変更)
Option Explicit ' 宣言 Dim FS1, targetFolder, folder, file, subFolder Dim FS2, outputFile Dim fileList ' 初期設定 targetFolder = "z:\" outputFile = "filelist.txt" ' ファイル一覧取得 Set FS1 = WScript.CreateObject("Scripting.FileSystemObject") Set folder = FS1.GetFolder(targetFolder) Sub addFileList(folder) For Each file In folder.Files ReDim Preserve fileList(UBound(fileList) + 1) fileList(UBound(fileList)) = file.ShortName & ":" & file.Name Next For Each subFolder In folder.SubFolders addFileList(subFolder) Next End Sub fileList = array() addFileList(folder) ' ファイル出力 Set FS2 = FS1.OpenTextFile(outputFile, 2, True) FS2.Write join(fileList, vbCrLf) ' CloseやNothingなどは省略
規定の文字数を超えている場合は正常に取得することが出来ませんので、
ショートファイルネームでのアクセスを提案してみたのですが、それすらも、
ターゲットのファイルシステムがショートファイルネームをサポートしていなければ、
そもそも出来ない話なので、まずは目視確認用のコードを提示しました。
目視確認できたと仮定し、
フルパスでアクセスできるかどうかのテストを組み込む例も提示しておきます
変更前 fileList(UBound(fileList)) = file.ShortName & ":" & file.Name 変更後 If FS1.FileExists(file.Path) = True Then fileList(UBound(fileList)) = "OK" & file.Path Else fileList(UBound(fileList)) = "NG" & file.Path End IF
関連するプロパティとしては下記のようなものがあります
ロング | ショート | |
ファイルネーム | file.Name | file.ShortName |
パス | file.Path | file.ShortPath |
実施したのですが目視確認もできませんでした
ありがとうございました
ショートファイルで、しゅとくできたら、
取得してみてください
Option Explicit
' 宣言
Dim FS1, targetFolder, folder, file, subFolder
Dim FS2, outputFile
Dim fileList
' 初期設定
targetFolder = "z:\"
outputFile = "filelist.txt"
' ファイル一覧取得
Set FS1 = WScript.CreateObject("Scripting.FileSystemObject")
Set folder = FS1.GetFolder(targetFolder)
Sub addFileList(folder)
For Each file In folder.Files
ReDim Preserve fileList(UBound(fileList) + 1)
fileList(UBound(fileList)) = file.ShortName & ":" & file.Name
Next
For Each subFolder In folder.SubFolders
addFileList(subFolder)
Next
End Sub
fileList = array()
addFileList(folder)
' ファイル出力
Set FS2 = FS1.OpenTextFile(outputFile, 2, True)
FS2.Write join(fileList, vbCrLf)
' CloseやNothingなどは省略
ショートファイルで、しゅとくできたら、
取得してみてください
Option Explicit
' 宣言
Dim FS1, targetFolder, folder, file, subFolder
Dim FS2, outputFile
Dim fileList
' 初期設定
targetFolder = "z:\"
outputFile = "filelist.txt"
' ファイル一覧取得
Set FS1 = WScript.CreateObject("Scripting.FileSystemObject")
Set folder = FS1.GetFolder(targetFolder)
Sub addFileList(folder)
For Each file In folder.Files
ReDim Preserve fileList(UBound(fileList) + 1)
fileList(UBound(fileList)) = file.ShortName & ":" & file.Name
Next
For Each subFolder In folder.SubFolders
addFileList(subFolder)
Next
End Sub
fileList = array()
addFileList(folder)
' ファイル出力
Set FS2 = FS1.OpenTextFile(outputFile, 2, True)
FS2.Write join(fileList, vbCrLf)
' CloseやNothingなどは省略
あと、
2012/04/12 13:07:11>ファイル名は100文字程度、入っているフォルダを200文字とかにしていただければ
これだと ファイル名は 100文字なので 私が作ったものだと 出力されません。
あくまでフルパス指定なので要件とちょっとずれてます
2012/04/14 10:21:48ありがとうございました