Vbs batch rename files & Extract file path information

Source: Internet
Author: User

From http://hi.baidu.com/sunch721521/blog/item/1a872173039887148701b0db.html

 

Dim FSO, my_file, directory, tmpname, num, URL, newname
Set FSO = Createobject ("scripting. FileSystemObject ")
'Set the location of the file directory
Url = inputbox ("directory of the input file, such as D: \ floder ")
Set Directory = FSO. getfolder (URL)
'Set the rename prefix
Newname = inputbox ("enter the new name prefix ")
'Create a text file and store the file name
Set file = FSO. createtextfile ("name.txt", 2, ture)
For each my_file in directory. Files
'Read file name
Tmpname = lcase (my_file.name)
'Save the original file name
File. writeline tmpname
'Rename the file
Num = num + 1
My_file.name = newname & "_" & num & "." & FSO. getextensionname (my_file)
Next

Supplement: extract the file path, file name, and file suffix.
{
Set objfso = Createobject ("scripting. FileSystemObject ")
Set my_file = objfso. GetFile ("D: \ Folder \ zsc.txt ")

Wscript. Echo "drive letter of the file:" & objfso. getdrivename (my_file)
Wscript. Echo "file path:" & objfso. getabsolutepathname (my_file)
Wscript. Echo "file directory:" & objfso. getparentfoldername (my_file)
Wscript. Echo "file name:" & objfso. getfilename (my_file)
Wscript. Echo "file basic name:" & objfso. getbasename (my_file)
Wscript. Echo "File Extension:" & objfso. getextensionname (my_file)

'Wscript. Echo "file name:" & my_file.name
'Wscript. Echo "Short file name in 8.3 format:" & my_file.shortname
}

for example, there are many shortcuts in disk D folder named "shortcut *. EXE. lnk, changed to "*. "lnk", that is, remove "shortcuts to ingress and cmd.exe"
{< br> dim FSO, my_file, directory, tmpname, str1, str2
set FSO = Createobject ("scripting. fileSystemObject ")
Set Directory = FSO. getfolder ("D: \ folder")
str1 = "shortcut to"
str2 = ". EXE. "
for each my_file in directory. files
tmpname = lcase (my_file.name)
If instr (tmpname, str1)> 0 then
tmpname = Replace (tmpname, str1 ,"")
end if
If instr (tmpname, str2)> 0 then
tmpname = Replace (tmpname, str2 ,". ")
end if
my_file.name = tmpname
next
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.