
각 폴더 안에 있는 첫 번째 파일의 처음 12자로 이름을 바꾸고 싶은 폴더가 12,000개가 넘습니다. 예를 들어 "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"를 제거할 때까지 아무 것도 변경되지 않습니다.