Vbs script. (Batch Modify file names and Back Up Files in the original folder before execution)

Source: Internet
Author: User

'================================================ ==========================================
'
'Vbscript source file -- created with sapien technologies primalscript 4.0
'
'Name:
'
'Author: Microsoft, Microsoft
'Date: 2014/7/9
'
'Comment: 'batch modify all file names in the folder
'
'================================================ ==========================================
'Select my computer as the root directory to select the Directory

'Const my_computer = & H11 &
'Const window_handle = 0
'Const Options = 0
'
'Set objshell = Createobject ("Shell. Application ")
'Set objfolder = objshell. namespace (my_computer)
'Set objfolderitem = objfolder. Self
'Strpath = objfolderitem. Path
'
'Set objshell = Createobject ("Shell. Application ")
'Set objfolder = objshell. browseforfolder _
'(Window_handle, "select a folder:", options, strpath)
'
'If objfolder is nothing then
'Wscript. Quit
'End if
'
'Set objfolderitem = objfolder. Self
'Objpath = objfolderitem. Path
'
'Msgbox objpath


'================================================ ======================================
'Select the directory under the specified drive letter
'Const window_handle = 0
'Const Options = 0
'
'Set objshell = Createobject ("Shell. Application ")
'Set objfolder = objshell. browseforfolder _
'(Window_handle, "select a folder:", options, "C :\")
'
'If objfolder is nothing then
'Wscript. Quit
'End if
'
'Set objfolderitem = objfolder. Self
'Objpath = objfolderitem. Path
'
'Msgbox objpath

'================================================ ========================================
'Define variables

Dim file_path, prefix_name, suffix_name, repeat_name, repeat_edit
Dim oneline, twoline, threeline, fourline, fiveline
I = 0

File_path = "C: \ Users \ Administrator \ Desktop \ 1 \ music" 'path of the target folder
Dst_file_path = "C: \ Users \ Administrator \ Desktop \ 1 \ music_bak"

 

'----- Get the folder path and open the configuration file
Set FSO = Createobject ("scripting. FileSystemObject ")
Set folder = FSO. getfolder (file_path)
Set FS = folder. Files
Set file = FSO. opentextfile ("E: \ practice \ vbs \ config. ini", 1)

'---------------- Back up the original file before the operation

FSO. copyfolder file_path, dst_file_path, true


'----------------------------------
'Retrieve the two numbers in the first row.
Oneline = file. Readline
Onelinestr = Split (oneline, "= ")
Onelinecount = ubound (split (oneline, "= "))

For I1 = 0 to onelinecount
Wscript. Echo onelinestr (I1)
Next
'-------------------------------------
'Retrieve the two numbers in the second row.
Twoline = file. Readline
Twolinestr = Split (twoline, "= ")
Twolinecount = ubound (split (twoline, "= "))

For I2 = 0 to twolinecount

Wscript. Echo twolinestr (I2)
Next
'-------------------------------------------
'Retrieve the two numbers in the third row.
Threeline = file. Readline
Threelinestr = Split (threeline, "= ")
Threelinecount = ubound (split (threeline, "= "))

For I3 = 0 to threelinecount
Wscript. Echo threelinestr (I3)
Next
'-------------------------------------------
'Retrieve the two numbers in the fourth row.
Fourline = file. Readline
Fourlinestr = Split (fourline, "= ")
Fourlinecount = ubound (split (fourline, "= "))

For I4 = 0 to fourlinecount
Wscript. Echo fourlinestr (I4)
Next

'-----------------------------------------
'Retrieve the two numbers in the fifth row.
Fiveline = file. Readline
Fivelinestr = Split (fiveline, "= ")
Fivelinecount = ubound (split (fiveline, "= "))

For I5 = 0 to fivelinecount
Wscript. Echo fivelinestr (I5)
Next

'---------------------------------------------
'Call Process
Function_main ()
Function function_main ()

If onelinestr (1) = "true" then

Function_prefix_name ()

Elseif onelinestr (1) = "false" then

Function_suffix_name ()

Elseif onelinestr (1) = "Number" then

Function_number_value ()

Elseif onelinestr (1) = "array" then

Function_myarrayrename ()

Elseif onelinestr (1) = "" then

Wscript. Quit

End if

End Function
'-----------------------------------------
'Add the prefix before the original name
Function function_prefix_name ()

For each file in FS

File. Name = twolinestr (1) & file. Name

Next

End Function

'--------------------------------------
'Add a suffix before the original name
Function function_suffix_name ()

For each file in FS

Name = mid (file. Name, 1, faster Rev (file. Name, ".")-1) 'get the file name before.
Format = mid (file. Name, file. Name (file. Name, ".") '.
File. Name = Name & threelinestr (1) & format

Next

End Function


'--------------------------------------------
'Add an ordered auto-increment number before the original name
Function function_number_value ()

For each file in FS

Fourlinestr (1) = fourlinestr (1) + 1
File. Name = fourlinestr (1) & file. Name

Next

End Function
'Function _ suffix_name ()

'--------------------------------------------------
'Batch change the name
Function_myarrayrename ()

Function function_myarrayrename ()

Const beforalarm = "the prisoner has been in jail. Please observe"
Const afteralarm = "in the event of prisoner violence, various teams should handle it as planned"
Dim myarray (12)
N = 1
Y = 0
For I = 0 to 12
If I = 11 then

Myarray (I) = "door guard"

Elseif I = 12 then

Myarray (I) = "self-defense whistle"

Else

Myarray (I) = N & "no"
N = n + 1
End if


Wscript. Echo myarray (I)
Next

For each file in FS

Format = mid (file. Name, faster Rev (file. Name ,"."))
'Msgbox format
'Msgbox myarray (y)

If fivelinestr (1) = "before" then

File. Name = myarray (y) & beforalarm & format

Elseif fivelinestr (1) = "after" then

File. Name = myarray (y) & afteralarm & format

Else

Msgbox "Please set before confirmation or after confirmation! "," Prompt"

Wscript. Quit

End if

Y = Y + 1
Wscript. ECHO file. Name

Next


End Function

 

 

'================================================ ======================================

'If prefix_name <> "" then' batch prefix
'For each F in FS
'F. Name = prefix_name & F. Name
'Next
'End if
'
'If suffix_name <> "" then' batch suffix
'For each F in FS
'Name = mid (F. Name, 1, Rev (F. Name, ".")-1)
'Format = mid (F. Name, Rev (F. Name ,"."))
'F. Name = Name & suffix_name & format
'Next
'End if
'
'If repeat_name <> "" then' batch Delete identical characters
'For each F in FS
'On error resume next
'F. Name = Replace (F. Name, repeat_name, repeat_edit)
'Next
'End if
''----- File operation ended
'
'Set FSO = nothe' to release memory
'
'Msgbox ("finished! ")

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.