SoFunction
Updated on 2025-04-10

vbs file operation collection code

Here is a vbs file I wrote:


'Keyword configuration file address 
Const config = "E:\cleandata\"

'Folders to be checked 
Const dir = "D:\Log\html\"

'Log saving path 
Const LogDir = "E:\cleandata\Log\"

'Global Object 
set fso=createobject("")  

Dim keywordList(10000)

Rem : =========== Start the main program 
Dim starttime , Endtime

starttime = Now 
Call main()
endtime = Now 

Set fso = Nothing 

msgbox  "Congratulations! The operation has been completed. Time from:" & starttime & " arrive " & endtime   ,4096,"File Rename"

Rem :  =========== Main program
Sub main()
     "start..." & Now 
    Call GetKeyWord()
    Call getFiles(dir)
End Sub 

Rem :  ===========  Read configuration files
Sub GetKeyWord()
    set sdir = createobject("")  
    set file = (config)  
    do while <>true  
        m=m+1  
         m,  
        Dim word
        word = sdir(m)
'         word 
        If Len(Trim(word) )>0 Then 
            KeywordList(m)= word
        End If 
    Loop 
      
    Set file = Nothing 
End Sub 

Rem :  =========== Get the file list 
Sub getFiles(path)
    Set folder = (path)
    Set subfolder = 
    Set file = 
    For Each s_file In file
        ' s_file.path
        checkWord s_file.path
    Next 

    For Each s_subfolder In subfolder
        getFiles(s_subfolder.path)    'Recursive call 
    Next 
End Sub 

Rem :  ===========  Compare configuration files,Determine whether keywords are included 
Sub checkWord(path)
    ' path
    Dim content , file 
    Set file = (path, 1, false) 
    content = 
    
    Set file = Nothing 
    For i=0 To UBound(keywordList)
        word = keywordList(i)
        If InStr(content, word )>0 And Len(word)>0 Then 
             path & "Matched:" & word
'            Set file = Nothing 
            RenameSubPage path
            Exit For 
        End If 
    Next 
End Sub 

Rem : =========== Rename the file
Sub RenameSubPage(path)
    If (path) =True Then 
        Dim target , ext
        ext = ".bak"
        target = path & ext
        ' ===== Method 1 
         path , target

        ' ===== Method 2 
        'Set f = ( path)
        ' =  & ext 
        ' 
        'Set f = Nothing 

        WriteLog target
    End If 
End Sub 

Rem :  ===========  Processing logs
Sub WriteLog(strmsg)
    Dim logtxt
    logtxt = LogDir & "dellog-" & Year(Now) & "-" & Month(Now) & "-" & Day(Now) & ".txt"
    
    Dim f 
    If (logtxt) Then 
        Set f = (logtxt, 8 )
    Else
        Set f = (logtxt, 2, true)
    End If 

     strmsg 
     
    Set f = Nothing 
    
    ' =========================================� 
 '    Set objShell = CreateObject("") 
'    cmd = "%comspec% /k echo " & strmsg & " >> " &  logtxt & "  && exit"
'    (cmd) ,vbhide
    'Pushing allows to prevent too many processes from occurring in the task manager. If there are multiple processes, please use taskkill /f /im to close it.
 '    Set objShell = Nothing 

     5    
End Sub

Contents of the file:

Keyword 1
Keyword 1

That is, one keyword per line.

This is an improved version of the VBS version batch renaming.

 rem Read configuration files
 Dim config 
 config = ""
 set fso=createobject("")  
 set a=createobject("")  
 set file=(config)  
 do while <>true  
  m=m+1  
   m,  
 src =  a(m)
 RenameSubPage src
loop  
  
Set fso =Nothing 
msgbox  "The operation has been completed" ,4096,"File Rename"

Sub RenameSubPage(strURL)
 Dim path
 For i=19 To 100
  path = Replace(strURL , ".html", "_"& i & ".html")
  If (path) =True Then 
   target = path & ".tmp"
    path , target
  Else 
   ' do nothing 
  End If 
 Next 
End Sub 

Note: The file content is as follows:
D:\a\b\
D:\d\e\

This is the end of this article about the vbs file operation collection code. For more related vbs file operation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!