
我有超過 12,000 個資料夾,我想使用每個資料夾中第一個檔案的前 12 個字元來重命名這些資料夾。例如,我有一個名為“1”的資料夾,但裡面有幾個PDF 文件,列出的第一個文件的文件名為“201405090360.pdf”我希望將該資料夾重命名為“201405090360”這可能嗎?
答案1
以下內容被認為有所幫助。它可以透過更多的錯誤處理來改進,但應該完成這項工作或用作更完整的腳本的起點。
筆記:
VBScript 腳本應位於與父資料夾相同的目錄中,如下面的樹狀視圖所示。
頂部的 strPath 變數應變更為父目錄的名稱,即從「Start」變更為
將 blnDoIt 從 false 更改為 true 以實際進行更改。
建議先執行如下:cscript process.vbs > log.txt
如果日誌檔案看起來正確,請將 blnDoIt 變更為 true。
' Given this structure:
'
'│ process.vbs
'│
'└───Start
' ├───1
' │ 123456789012aaa.txt
' │ 123456789013bbb.txt
' │
' ├───2
' │ 223456789012ccc.txt
' │ 223456789013ddd.txt
' │
' ├───3
' │ 323456789012eee.txt
' │ 323456789013fff.txt
' │
' ├───4
' │ 423456789012ggg.txt
' │ 423456789013hhh.txt
' │
' ├───5
' │ 523456789012iii.txt
' │ 523456789013jjj.txt
' │
' └───6
' 623456789013kkk.txt
' 623456789012jjj.txt
'===================================================================================
dim strPath : strPath = "Start"
dim blnDoIt : blnDoIt = false
'===================================================================================
dim objFSO : set objFSO = CreateObject("Scripting.FileSystemObject")
dim objFolder : set objFolder = objFSO.GetFolder(strPath)
dim strPGetAbsolutePathName : strPGetAbsolutePathName = objFSO.GetAbsolutePathName(strPath)
dim colSubfolders : set colSubfolders = objFolder.Subfolders
'===================================================================================
for each objSubfolder in colSubfolders
strNameOfFolder = objSubfolder.Name
wscript.echo "Processing directory: " & strNameOfFolder
strFileNameToUse = GetFileNameToUseAsParentDir (strNameOfFolder)
if strFileNameToUse <> -1 then
wscript.echo " > Chosen file to represent directory: " & strFileNameToUse
strLeft12 = left (strFileNameToUse, 12)
wscript.echo " > First 12 characters of file: " & strLeft12
if blnDoIt then
wscript.echo " > Renaming directory " & strPGetAbsolutePathName & "\" & strNameOfFolder & " to " & strPGetAbsolutePathName & "\" & strLeft12
objFSO.MoveFolder strPGetAbsolutePathName & "\" & strNameOfFolder, strPGetAbsolutePathName & "\" & strLeft12
else
wscript.echo " > Rename directory " & strPGetAbsolutePathName & "\" & strNameOfFolder & " to " & strPGetAbsolutePathName & "\" & strLeft12
end if
else
wscript.echo " > Skipping."
end if
wscript.echo ""
next
'===================================================================================
'===================================================================================
Function GetFileNameToUseAsParentDir(strDir)
dim oFiles : Set oFiles = CreateObject("System.Collections.ArrayList")
dim oF : set oF = objFSO.GetFolder(strPGetAbsolutePathName & "\" & strDir).Files
wscript.echo " > " & oF.count & " files in directory."
if oF.count > 0 then
for each oFile In oF
oFiles.Add oFile.name
next
oFiles.Sort
GetFileNameToUseAsParentDir = oFiles(0)
else
GetFileNameToUseAsParentDir = -1
end if
set oFiles = nothing
End Function
'===================================================================================
答案2
我會使用 Powershell。如果我正確理解你的規格,這應該可以完成工作:
# Get List of subfolders
$Folders = Get-ChildItem <FOLDER> -Directory
# Process each subfolder
Foreach ($Subfolder in $Folders){
# Get the names all files in current subfolder
$FileNames = (Get-ChildItem $Subfolder.FullName -File).Name
# If folder was not empty
if ($FileNames) {
# Replace current name of folder with first 12 chars of the name of the first file
Rename-Item $Subfolder.FullName $FileNames[0].Substring(0,12) -WhatIf
}
}
替換為您的真實資料夾,另存為 *.ps1 文件,打開 powershell 並運行。
只會顯示結果,不會變更任何內容,直到您從底部第三行刪除「-Whatif」。