엑셀/vba

하위 폴더 포함하여 파일 이름 랜덤 변경

do121 2023. 11. 9. 15:46

https://software-solutions-online.com/list-files-and-folders-in-a-directory/

 

Find and List all Files and Folders in a Directory - VBA and VB.Net Tutorials, Education and Programming Services

In this article I will explain how you can  find and create a list of all the files and folder in a directory: Jump To: Get List of All Files Using, FileSystemObject Get List of All Folders Using, FileSystemObject Dir() Get List of All Files

software-solutions-online.com

https://www.automateexcel.com/vba/list-files-in-folder/

 

VBA List Files in Folder - Automate Excel

In this tutorial, you will learn how to get names of all files in a folder and put them into a Worksheet. Instead, if you want to learn how to check if a file exists, you can click on this link: VBA File Exists    Using the FileSystemObject to Get the Li

www.automateexcel.com

https://www.thespreadsheetguru.com/generate-random-characters/

 

Generate A String Of Random Characters With VBA Code

A VBA function that will create a string of random characters at any desired length.

www.thespreadsheetguru.com

 

파일 및 폴더의 이름을 랜덤하게 변경하는 코드임

 

특정 폴더내 파일 이름을 바꾸는 코드는 많은 데 하위폴더도 포함해서 바꾸는 코드가 없어서 위 사이트들의 코드들을 짜깁기해서 만들어봄

 

아래 코드에서 

Sub rename_all_dir()의 Set objFolder = objFSO.GetFolder("c:\temp")부분의 경로를 원하는 폴더로 변경하면

하위폴더 포함하여 파일 이름을 랜덤하게 바꿈

 

코드 플로우는 Sub rename_all_dir()에서 시작해서 폴더 리스트를 얻어오면  폴더내 파일 이름을 랜덤하게 바꾼후 Sub rename_all_dir_resursive(fld as string)를 호출해서 자식 폴더를 얻고 그 폴더내 파일이름을 바꾸고 다시 자식 폴더 이름을 Sub rename_all_dir_resursive(fld as string)에 넘겨주고 계속 순환적으로 Sub rename_all_dir_resursive(fld as string)에 넘겨 줌.

 

폴더이름도 바꾸고 싶으면 파일이름 변경하듯이 Name old_name as new_name으로 변경하면됨

 

Dim i As Integer

Sub rename_all_dir()
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object


'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder("c:\temp")
i = 1
'loops through each file in the directory and prints their names and path
For Each objSubFolder In objFolder.subfolders
    'print folder name
    Cells(i + 1, 1) = objSubFolder.Name
    'print folder path
    Cells(i + 1, 2) = objSubFolder.Path
    i = i + 1
    
    LoopThroughFiles( objSubFolder.Path)
    
    rename_all_dir_resursive(objSubFolder.Path)
Next objSubFolder
End Sub

Sub rename_all_dir_resursive(fld as string)
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object


'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(fld)
i = 1
'loops through each file in the directory and prints their names and path
For Each objSubFolder In objFolder.subfolders
    'print folder name
    Cells(i + 1, 1) = objSubFolder.Name
    'print folder path
    Cells(i + 1, 2) = objSubFolder.Path
    i = i + 1

    LoopThroughFiles( objSubFolder.Path)
    
    rename_all_dir_resursive(objSubFolder.Path)

Next objSubFolder

End Sub

Sub LoopThroughFiles (fld as string)

Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
'Dim i As Integer

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder(fld)

For Each oFile In oFolder.Files

'    Cells(i + 1, 1) = oFile.Name

'    i = i + 1

	Name oFolder.path + "\" + oFile.Name as oFolder.Path + "\" + RandomString(4)

Next oFile

End Sub

Function RandomString(Length As Integer)
'PURPOSE: Create a Randomized String of Characters
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim CharacterBank As Variant
Dim x As Long
Dim str As String

'Test Length Input
  If Length < 1 Then
    MsgBox "Length variable must be greater than 0"
    Exit Function
  End If

CharacterBank = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", _
  "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", _
  "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "!", "@", _
  "#", "$", "%", "^", "&", "*", "A", "B", "C", "D", "E", "F", "G", "H", _
  "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", _
  "W", "X", "Y", "Z")
  
'Randomly Select Characters One-by-One
  For x = 1 To Length
    Randomize
    str = str & CharacterBank(Int((UBound(CharacterBank) - LBound(CharacterBank) + 1) * Rnd + LBound(CharacterBank)))
  Next x

'Output Randomly Generated String
  RandomString = str

End Function