vbscript - Rename files using foldername as prefix and part of current filename VBS -


i have unique situation , i'd insight. have no programming background figured i'd turn here.

i have bunch of folders. inside each of folders folder. inside folder few files.

these files named gibberish letters , numbers, characters "-" (no quotes), , name i'd use new suffix.

i take top tier foldername , make prefix , above mentioned suffix create "prefix - suffix" each new filename.

my first thought via vbs, again, i'm unfamiliar. can shine light or provide script? assuming not of hassle.

an example of have , i'm looking for:

enter image description here

give try vbscript :

option explicit dim file,myrootfolder,rootfolder,prefix,suffix myrootfolder = browse4folder call scan4file(myrootfolder) msgbox "script done !",vbinformation,"script done !" '************************************************************************** function gettheparent(drivespec)     dim fso     set fso = createobject("scripting.filesystemobject")     gettheparent = fso.getparentfoldername(drivespec) end function '************************************************************************** function strippathfolder(path)        dim arrstr : arrstr = split(path,"\")        strippathfolder = arrstr(ubound(arrstr))    end function    '************************************************************************** function strippathfile(path)        dim arrstr : arrstr = split(path,"-")        strippathfile = replace(arrstr(ubound(arrstr)),"_","-") end function    '************************************************************************** function browse4folder()     dim objshell,objfolder,message     message = "please select folder in order scan , subfolders rename files"     set objshell = createobject("shell.application")     set objfolder = objshell.browseforfolder(0,message,0,0)     if objfolder nothing         wscript.quit     end if     browse4folder = objfolder.self.path end function '********************************************************************************************** function scan4file(folder)     dim fso,objfolder,arrsubfolders,file,subfolder,newfilename     set fso = createobject("scripting.filesystemobject")     set objfolder = fso.getfolder(folder)     set arrsubfolders = objfolder.subfolders     each file in objfolder.files         rootfolder = gettheparent(gettheparent(file))          prefix = strippathfolder(rootfolder)          suffix = strippathfile(file)         newfilename = prefix & suffix 'msgbox prefix,vbinformation,prefix 'msgbox suffix,vbinformation,suffix 'msgbox "new file name ==> " & newfilename,vbinformation,prefix & suffix         call renamefile(file,newfilename)     next     each subfolder in objfolder.subfolders         call scan4file(subfolder)     next end function '********************************************************************** sub renamefile(file1,file2)     dim ws,command,execution     set ws = createobject("wscript.shell")     command = "cmd /c ren "& dblquote(file1) &" "& dblquote(file2) &""     execution = ws.run(command,0,false) end sub '********************************************************************** function dblquote(str)     dblquote = chr(34) & str & chr(34) end function '********************************************************************** 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -