jcunews

RenDir.vbs

Jan 20th, 2019
131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'VBScript: Rename a directory based on the file names it contains which are uniform
  2. 'Reference: https://www.reddit.com/r/Batch/comments/ai12q1/request_simple_script_to_sort_files_in_a_folder/
  3. '2019-01-21, https://www.reddit.com/user/jcunews1/
  4.  
  5. function isalphanumeric(c)
  6.   c = ascw(ucase(c))
  7.   isalphanumeric = (((c >= asc("0")) and (c <= asc("9"))) or _
  8.     ((c >= asc("A")) and (c <= asc("Z"))))
  9. end function
  10.  
  11. function isseparatorchar(c)
  12.   isseparatorchar = (c = "-") or (c = "_") or (c = "+") or (c = "=") or _
  13.     (c = ";") or (c = ",") or (c = ".")
  14. end function
  15.  
  16. if wsh.arguments.count = 0 then
  17.   wsh.echo "Usage: rendir {directory} [noprompt]" & vbcrlf & vbcrlf & _
  18.     "Optionally specify anything for [noprompt] to disable confirmation."
  19.   wsh.quit
  20. end if
  21. set fs = createobject("scripting.filesystemobject")
  22. if not fs.folderexists(wsh.arguments(0)) then
  23.   wsh.echo "Specified directory is not found or invalid."
  24.   wsh.quit
  25. end if
  26. set dir = fs.getfolder(wsh.arguments(0))
  27. mn = ""
  28. for each fl in dir.files
  29.   fn = fl.name
  30.   i = instrrev(fn, ".")
  31.   if i > 0 then
  32.     ext = ucase(mid(fn, i + 1))
  33.     fn = left(fn, i - 1)
  34.   else
  35.     ext = ""
  36.   end if
  37.   if ext = "MP4" then
  38.     if mn <> "" then
  39.       if len(mn) < len(fn) then
  40.         l = len(mn)
  41.       else
  42.         l = len(fn)
  43.       end if
  44.       do while l > 0
  45.         c = mid(fn, l + 1, 1)
  46.         mt = left(mn, l)
  47.         ft = left(fn, l)
  48.         if ucase(mt) = ucase(ft) then
  49.           exit do
  50.         else
  51.           l = l - 1
  52.         end if
  53.       loop
  54.       if l > 0 then
  55.         m = l
  56.         if isalphanumeric(right(mt, 1)) and isalphanumeric(c) then
  57.           do
  58.             l = l - 1
  59.             mt = left(mt, l)
  60.           loop until (l = 0) or (not isalphanumeric(right(mt, 1)))
  61.         end if
  62.         if l = 0 then mt = left(mt, l)
  63.         if isseparatorchar(right(mt, 1)) then mt = left(mt, len(mt) - 1)
  64.         mn = mt
  65.       else
  66.         wsh.echo "File names are too different. e.g." & _
  67.           vbcrlf & mn & vbcrlf & fn
  68.         wsh.quit
  69.       end if
  70.     else
  71.       mn = fn
  72.     end if
  73.   end if
  74. next
  75. if wsh.arguments.count > 1 then
  76.   c = 6
  77. else
  78.   c = msgbox("Rename this folder:" & vbcrlf & vbcrlf & dir.name & vbcrlf & _
  79.     vbcrlf & "Into this?" & vbcrlf & vbcrlf & mn, 4, "RenDir")
  80. end if
  81. if c = 6 then dir.name = mn
RAW Paste Data