Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'VBScript: Rename a directory based on the file names it contains which are uniform
- 'Reference: https://www.reddit.com/r/Batch/comments/ai12q1/request_simple_script_to_sort_files_in_a_folder/
- '2019-01-21, https://www.reddit.com/user/jcunews1/
- function isalphanumeric(c)
- c = ascw(ucase(c))
- isalphanumeric = (((c >= asc("0")) and (c <= asc("9"))) or _
- ((c >= asc("A")) and (c <= asc("Z"))))
- end function
- function isseparatorchar(c)
- isseparatorchar = (c = "-") or (c = "_") or (c = "+") or (c = "=") or _
- (c = ";") or (c = ",") or (c = ".")
- end function
- if wsh.arguments.count = 0 then
- wsh.echo "Usage: rendir {directory} [noprompt]" & vbcrlf & vbcrlf & _
- "Optionally specify anything for [noprompt] to disable confirmation."
- wsh.quit
- end if
- set fs = createobject("scripting.filesystemobject")
- if not fs.folderexists(wsh.arguments(0)) then
- wsh.echo "Specified directory is not found or invalid."
- wsh.quit
- end if
- set dir = fs.getfolder(wsh.arguments(0))
- mn = ""
- for each fl in dir.files
- fn = fl.name
- i = instrrev(fn, ".")
- if i > 0 then
- ext = ucase(mid(fn, i + 1))
- fn = left(fn, i - 1)
- else
- ext = ""
- end if
- if ext = "MP4" then
- if mn <> "" then
- if len(mn) < len(fn) then
- l = len(mn)
- else
- l = len(fn)
- end if
- do while l > 0
- c = mid(fn, l + 1, 1)
- mt = left(mn, l)
- ft = left(fn, l)
- if ucase(mt) = ucase(ft) then
- exit do
- else
- l = l - 1
- end if
- loop
- if l > 0 then
- m = l
- if isalphanumeric(right(mt, 1)) and isalphanumeric(c) then
- do
- l = l - 1
- mt = left(mt, l)
- loop until (l = 0) or (not isalphanumeric(right(mt, 1)))
- end if
- if l = 0 then mt = left(mt, l)
- if isseparatorchar(right(mt, 1)) then mt = left(mt, len(mt) - 1)
- mn = mt
- else
- wsh.echo "File names are too different. e.g." & _
- vbcrlf & mn & vbcrlf & fn
- wsh.quit
- end if
- else
- mn = fn
- end if
- end if
- next
- if wsh.arguments.count > 1 then
- c = 6
- else
- c = msgbox("Rename this folder:" & vbcrlf & vbcrlf & dir.name & vbcrlf & _
- vbcrlf & "Into this?" & vbcrlf & vbcrlf & mn, 4, "RenDir")
- end if
- if c = 6 then dir.name = mn
Add Comment
Please, Sign In to add comment