0

I have over 12,000 folders that I would like to rename with the first 12 characters of the first file inside each folder. For example I have a folder named "1" but inside there are several PDF files and the first one listed has a file name of, "201405090360.pdf" I want the folder to be renamed, "201405090360" is this possible?

  • 1
    Are all the directories subdirectories of a single one or are we talking about a complex directory structure here? First one listed as in alphabetically? – whatever Oct 4 '17 at 13:43
  • Currently the folders are located on an external drive then the structure has a few other folders before you get to the folders I want to rename looks like this: Computer > My Book (F:) > Sheriff Reports > Case Attachment Files > 57 Some of these folders contain one PDF file while others contain multiple PDF files, but all of the files inside began with the same 12 characters. They do appear to be in alphabetical order – SPChris Oct 4 '17 at 14:33
  • Would this help get you going: pastebin.com/0Qr9ksME Note: blnDoIt is set to false so it will not make any changes until you change it to true. This will allow you to test it first. To run, edit the strPath to be the top level directory name, the referenced vbscript should be in the same directory, save it as script.vbs for example. Then run in an administrative command prompt cscript script.vbs > log.txt hopefully the log file has the results you're expecting. – HelpingHand Oct 4 '17 at 20:55
  • Glad it helped. The Powershell example by @whatever also looks like it should work and it does test that there is a file each directory to use. – HelpingHand Oct 5 '17 at 17:20
  • @HelpingHand maybe post the code as answer here so SPChris can mark it as the accepted answer? – whatever Oct 6 '17 at 1:11
1

The following was acknowledged to have helped. It could be improved with more error handling but should do the job or used as a starting point for a more complete script.

Notes:

The VBScript script should be in the same directory as the parent folder as per the tree view below.

strPath variable at the top should be changed to the name of the parent directory, i.e. changed from "Start"

Change blnDoIt from false to true to actually make changes.

Suggest running it first as follows: cscript process.vbs > log.txt

If the log file looks correct, change blnDoIt to 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
'===================================================================================
0

I'd use Powershell. If I understood your specifications correctly, this should do the job:

# 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
    }
}

Replace with your real folder, save as *.ps1 file, open a powershell and run.

Will only display the results, not change anything until you remove "-Whatif" from the third line from the bottom.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.