vbscript - Rename part of file -
i require vbscript finds recent file in folder , renames it. have been able write script finds recent file. however, cannot figure out how correctly have file renamed once identified. have been able rename file basic name, confirming script works.
the file name needs letter "a" added in middle.
the file saved 20160229_titles
, needs become 20160229a_titles
.
below script tried pull year , add "a". figured if year add beginning, add in month , year. date current date. continues cause error message.
option explicit dim fso, folder, file, date, recentfile dim foldername, searchfilename, renamefileto foldername = "c:\ticket\test\" set fso = createobject("scripting.filesystemobject") set folder = fso.getfolder(foldername) set recentfile = nothing each file in folder.files if (recentfile nothing) set recentfile = file elseif formatdatetime(file.datelastmodified) = date set recentfile = file end if next recentfile.name = replace(recentfile.name, "_", "a_")
assuming filename consist of date followed underscore , other text several things:
replace underscores "a_" (if there 1 underscore in name):
file.name = replace(file.name, "_", "a_")
split name @ first underscore, append "a" first fragment , join fragments together:
arr = split(file.name, "_", 2) arr(0) = arr(0) & "a" file.name = join(arr, "_")
do regular expression replacement:
set re = new regexp re.pattern = "^(\d{8})_" file.name = re.replace(file.name, "$1a_")
Comments
Post a Comment