Guest User

Untitled

a guest
Jan 14th, 2017
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Script to list/rename files (sub-dirs included) based off of a file called rename.txt in the same directory.
  2. ' Set bRename to true to actually rename files rather than just echo what will take place.
  3.  
  4. 'Constants
  5. RENAME_FILE = "rename.txt"                      ' case sensitive 'special' file to find in each directory.
  6.  
  7. 'Globals
  8. bRename        = false                          ' always prints out, set this to true to perform rename
  9. objStartFolder = "C:\Users\user\Desktop\Test"  ' top level folder to search from, no trailing backslash
  10.  
  11. wscript.echo ""
  12. wscript.echo "Top folder to process:   " & objStartFolder
  13. wscript.echo "Action Mode:             " & bRename & vbcrlf
  14.  
  15. dim objFSO    : set objFSO = CreateObject("Scripting.FileSystemObject")
  16. dim objFolder : set objFolder = objFSO.GetFolder(objStartFolder)
  17.  
  18. ' Note: If the 'special' file (RENAME_FILE) doesn't exist then strNewFile is set to -1 otherwise the new filename from the file contents
  19. strNewFile = GetNewFileName (objFolder.Path)  
  20.  
  21. if strNewFile <> "-1" then
  22.  'we have a rename.txt
  23.  wscript.echo RENAME_FILE & " found in '" & objFolder.Path & "' and contains the text '" & strNewFile & "' to base filenames off."
  24.  
  25.   'Process root level files in the top directory
  26.  Set colFiles = objFolder.Files
  27.     dim intRoot : intRoot = 1
  28.     For Each objFile in colFiles
  29.        if objFile.Name <> RENAME_FILE then
  30.           strNewFileName = strNewFile & intRoot & GetExtension (objFolder.Path & "\" & objFile.Name)
  31.           wscript.Echo "found file " & objFolder.Path & "\" & objFile.Name & " rename to " &  strNewFileName
  32.           if bRename then
  33.              objFSO.MoveFile objFolder.Path & "\" & objFile.Name, strNewFileName
  34.           end if     
  35.          
  36.           intRoot = intRoot + 1
  37.        end if
  38.     Next
  39. else
  40.   wscript.echo "No " & RENAME_FILE & " in " & objFolder.Path & ".  Skipping..."
  41. end if
  42.  
  43. wscript.echo ""
  44.  
  45. ShowSubfolders objFSO.GetFolder(objStartFolder)
  46.  
  47. Sub ShowSubFolders(Folder)
  48.     For Each Subfolder in Folder.SubFolders
  49.  
  50.         wscript.echo "Folder to process " & Subfolder.Path
  51.         strNewFile2 = GetNewFileName (Subfolder.Path)
  52.  
  53.         if strNewFile2 <> "-1" then
  54.         'we have a rename.txt
  55.          wscript.echo RENAME_FILE & " found in '" & Subfolder.Path & "' and contains the text '" & strNewFile2 & "' to base filenames off."
  56.           Set colFiles = Subfolder.Files
  57.             dim intRoot : intRoot = 1
  58.             For Each objFile in colFiles
  59.                if objFile.Name <> RENAME_FILE then
  60.                   strNewFileName2 = strNewFile2 & intRoot & GetExtension (objFolder.Path & "\" & objFile.Name)
  61.                   wscript.Echo "found file " & Subfolder.Path & "\" & objFile.Name & " rename to " &  strNewFileName2
  62.                   if bRename then
  63.                     objFSO.moveFile Subfolder.Path & "\" & objFile.Name, strNewFileName2
  64.                   end if     
  65.                  
  66.                   intRoot = intRoot + 1
  67.                end if
  68.             Next
  69.         else
  70.           wscript.echo "No " & RENAME_FILE & " in " & Subfolder.Path & ".  Skipping..."
  71.         end if
  72.  
  73.         Wscript.Echo ""
  74.         ShowSubFolders Subfolder
  75.  
  76.     Next
  77.  
  78. End Sub
  79.  
  80. '=======================
  81. 'Functions
  82. '=======================
  83. Function GetNewFileName (strLocation)
  84.  
  85.     If (objFSO.FileExists(strLocation & "\" & RENAME_FILE)) Then
  86.         Set file = objFSO.OpenTextFile (strLocation & "\" & RENAME_FILE , 1)
  87.         Do Until file.AtEndOfStream
  88.           GetNewFileName = file.Readline
  89.         Loop
  90.     else
  91.         GetNewFileName = "-1"
  92.     end if
  93.  
  94. End Function
  95. '=======================
  96.  
  97. '=======================
  98. Function GetExtension (strFullFilePath)
  99.  
  100.     strExtension = objFSO.getextensionname(strFullFilePath)
  101.  
  102.     if  strExtension <> "" then
  103.        GetExtension = "." & strExtension
  104.     else
  105.        GetExtension = ""
  106.     end if
  107.  
  108. End Function
  109. '=======================
Advertisement
Add Comment
Please, Sign In to add comment