我有超过 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”。