[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Renames in Subdirectories

Posted on 2011-10-03
11
Medium Priority
?
377 Views
Last Modified: 2012-05-12
We have a vended app that we are moving away from.  The system stores links to videos.  The vendor changed the file names to guids so they don't make sense if you look at them through windows explorer.  I found the translation table in the database.  Here is an example:

MediaElementID                        displayName
abcd123456789xxx                 vs TeamA

Got a solution from someone on a query I wrote to build a batch file for the rename.  My question is using a batch file how can I rename name files in a subdirectory?

For ex the file structure is like this:

FOLDER1
      |  FOLDER name with GUID
           |  FILE name with GUID
FOLDER2

So in my batch file I have:

ren "6C8F91E5-B5EB-4EA5-9952-0002EA39C4E4" "GameClip1"

I need to look for that GUID in all folder levels and change the name.  Hope that makes sense.
0
Comment
Question by:martyuni
  • 4
  • 3
  • 3
  • +1
11 Comments
 

Expert Comment

by:Stuart_Page
ID: 36904568
So, to be clear, the real names for the folders is contained within a database? If so, your script would have to query the database to find the human-readable name, correct?
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36904690
Will all or most of the files in the folder structure you will scan need to renamed, or just a few?  (might impact the approach I suggest)

~bp
0
 

Author Comment

by:martyuni
ID: 36905529
I wrote a query and created records like this

ren "6C8F91E5-B5EB-4EA5-9952-0002EA39C4E4" "GameClip1"

Was going to put that in a batch file.

Billprew:  There will be a lot of files to be renamed, over 6000.
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 36905588
And are you only renaming files, or folders as well (it looked like both have been changed to a GUID in your original post).

~bp
0
 

Author Comment

by:martyuni
ID: 36905637
would like to do both
0
 
LVL 10

Accepted Solution

by:
ReneGe earned 1000 total points
ID: 36911248
Here you go.

It will create test files and folders, rename them and create a log file in a csv format.

All in the same folder as your batch file.

@ECHO OFF

SET BaseFolder=%~dp0TEST
SET Table=%~dp0Table.csv
SET LogFile=%~dp0TableOfRenamed.csv

REM Goto Production

REM CREATING TEST TABLE
	ECHO "6C8F91E5-B5EB-4EA5-9952-0002EA39C4E1","GameClip1">>"%Table%"
	ECHO "6C8F91E5-B5EB-4EA5-9952-0002EA39C4E2","GameClip2">>"%Table%"
	ECHO "6C8F91E5-B5EB-4EA5-9952-0002EA39C4E3","GameClip3.txt">>"%Table%"
	ECHO "6C8F91E5-B5EB-4EA5-9952-0002EA39C4E4","GameClip4.txt">>"%Table%"
	ECHO "6C8F91E5-B5EB-4EA5-9952-0002EA39C4E5","GameClip5.txt">>"%Table%"

REM CREATING TEST FOLDERS
	MD "%BaseFolder%\6C8F91E5-B5EB-4EA5-9952-0002EA39C4E1"
	MD "%BaseFolder%\6C8F91E5-B5EB-4EA5-9952-0002EA39C4E2"

REM CREATING TEST FILES
	ECHO TEST>"%BaseFolder%\6C8F91E5-B5EB-4EA5-9952-0002EA39C4E3"
	ECHO TEST>"%BaseFolder%\6C8F91E5-B5EB-4EA5-9952-0002EA39C4E1\6C8F91E5-B5EB-4EA5-9952-0002EA39C4E4"
	ECHO TEST>"%BaseFolder%\6C8F91E5-B5EB-4EA5-9952-0002EA39C4E2\6C8F91E5-B5EB-4EA5-9952-0002EA39C4E5"


:Production
REM RENAMING FOLDERS
	FOR /F "delims=" %%A IN ('DIR /B /S /AD "%BaseFolder%\*.*"') DO (
		FOR /F "usebackq tokens=1,2 delims=," %%B IN ("%Table%") DO IF "%%~B" EQU "%%~nA" (
			ECHO RENAMING FOLDER: [%%~nA] to [%%~C]
			ECHO "%date%","%time%","Folder","%%~fA","%%~C">>"%LogFile%"
			REN "%%~fA" "%%~C"
		)
	)
ECHO.

REM RENAMING FILES
	FOR /F "delims=" %%A IN ('DIR /B /S /A-D "%BaseFolder%\*.*"') DO (
		FOR /F "usebackq tokens=1,2 delims=," %%B IN ("%Table%") DO IF "%%~B" EQU "%%~nA" (
			ECHO RENAMING FILE: [%%~nA] to [%%~C]
			ECHO "%date%","%time%","File","%%~fA","%%~C">>"%LogFile%"
			REN "%%~fA" "%%~C"
		)
	)
ECHO.

PAUSE

Open in new window

0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 total points
ID: 36914339
Okay, I'm going to approach this with a VBS solution.  Given the number of mappings you need to process I think this will be a lot faster than the BAT approach above (no offense Rene).  It loads the mapping file into a dictionary one time, and then recursively searches for all files and folders under the parent.  For each it checks the dictionary to see if it needs to be renamed, and if so renames (moves) it.

You didn't mention file extensions at all, so I am assuming there are no extensions.  If that is not true then you will need to include more information on file names.

Edit the file names near the top to reference your mapping file and base folder.  Also, use a text editor or recreate the mapping file into the following format:

"C4DE5C95-EA16-404E-88B7-BECC09922E16","dir2"

Option Explicit

' Define Needed constants
Const cForReading = 1
Const cForWriting = 2
Const cInFile = "C:\EE\EE27377443\map.txt"
Const cBaseDir = "C:\EE\EE27377443\Base"

' Define needed variables
Dim oFSO, oInFile, dMap, sLine, aFields

' Create dictionary to store name mappings in
Set dMap = CreateObject("Scripting.Dictionary")

' Open input mapping file to load
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oInFile = oFSO.OpenTextFile(cInFile, cForReading)

' Read input lines, add to dictionary
' Format should be:
' "C4DE5C95-EA16-404E-88B7-BECC09922E16","dir2"
Do Until oInFile.AtEndOfStream
    sLine = oInFile.Readline
    aFields = Split(Replace(sLine, """", ""), ",")
    If Not dMap.Exists(aFields(0)) Then
        dMap.Item(aFields(0)) = aFields(1)
    End If
Loop

' Close input file, open output file
oInFile.Close

' Run recursive routine to process all files and subfolders under base folder
DoRename oFSO.GetFolder(cBaseDir)

Sub DoRename(oFolder)
   Dim oFile, oSubFolder

   ' See if we need to rename this folder
   If dMap.Exists(oFolder.Name) Then
      Wscript.Echo "Renaming folder [" & oFolder.Path & "] to [" & oFolder.ParentFolder & "\" & dMap.Item(oFolder.Name) & "]"
      oFolder.Move(oFolder.ParentFolder & "\" & dMap.Item(oFolder.Name))
   Else
      Wscript.Echo "No rename for folder [" & oFolder.Path & "]"
   End If

   For Each oFile in oFolder.Files
      ' See if we need to rename this file
      If dMap.Exists(oFile.Name) Then
         Wscript.Echo "Renaming file [" & oFile.Path & "] to [" & oFile.ParentFolder & "\" & dMap.Item(oFile.Name) & "]"
         oFile.Move(oFile.ParentFolder & "\" & dMap.Item(oFile.Name))
      Else
         Wscript.Echo "No rename for file [" & oFile.Path & "]"
      End If
   Next

   ' Recursively drill down into all subfolders (and their files)
   For Each oSubFolder In oFolder.Subfolders
      DoRename(oSubFolder)
   Next
End Sub

Open in new window

~bp
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36914358
Don't worry Bill. We'r just having fun helping others, hoping we'll learn something in the process :)

Cheers,
Rene
0
 

Author Closing Comment

by:martyuni
ID: 36917769
Thank you both for your help
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36917781
Your welcome!
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36917826
Welcome, good luck.

~bp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question