The sample code (T.vbs) is as follows:
Copy Code code as follows:
Set fso=wscript.createobject ("Scripting.FileSystemObject")
Flrname= "D:\Workspace\src\" ' dir, need deal with
Count=0 ' Get the Count of modified files
function Traversal (dir)
Set Flr=fso.getfolder (dir)
Set Fs=flr.files
Findstr1= "[assembly:securitytransparent]" ' Find string of that need to be replaced
Findstr2=replace (Findstr1, "", "") ' Becasuse there is spaces char in findstr,so add this FINDSTR2
Replacestr= "[Assembly:allowpartiallytrustedcallers ()]" ' Destination string in replace
For each F in FS
If LCase (f.name) = "AssemblyInfo.cs" Then
Set Findf=fso.opentextfile (f)
Do While Findf.atendofstream=false
D=f.datelastmodified ' Get modified datetime of current file
Alltext=findf.readall
if (INSTR (ALLTEXT,FINDSTR1) > 0) Then
S=replace (ALLTEXT,FINDSTR1, REPLACESTR)
Count=count+1
Findf.close ()
' If no FINDSTR2, delete the following ElseIf block
ElseIf (INSTR (ALLTEXT,FINDSTR2) > 0) Then
S=replace (ALLTEXT,FINDSTR2, REPLACESTR)
Count=count+1
Findf.close ()
Else
Findf.close ()
Exit Do
End If
Set R=fso.opentextfile (F, 2, true)
R.write s
D2=f.datelastmodified
If D2>=d then Exit Do
Loop
End If
Next
Set Fs=flr.subfolders
For each F in FS
Traversal (F.path)
Next
End Function
Traversal (Flrname)
' MsgBox ("done!" & Count & "Files were modified successfully.")
WScript.Echo "Done!" & Count & "Files were modified successfully." ' Value popup by ' WScript.Echo ' Can is received by. Bat easily
Wscript.Quit count ' This variable ' count ' in order to get a return value by%errorlevel%
The above action only performs the procedure for processing files
If you do not need to extract the running results, you can run the VBS file directly in the batch or keynote VBS script.
If the VBS script is called in bat and you want to print out the message
There are two ways to implement this:
1, add the following code to the bat
Copy Code code as follows:
@echo off
for/f "delims="%%a in (' cscript//nologo c:\t.vbs ') do (
Set scriptout=%%a)
echo Result:%scriptout%
The above echo prints the format of the string itself as needed, and the C:\t.vbs file directory is modified to your VBS file directory.
Join in 2,bat
Copy Code code as follows:
@echo off
cscript//nologo C:\t.vbs
echo-----Modify%errorlevel% "AssemblyInfo.cs" files successfully.-----
The above echo text format defines%errorlevel% to get the value of count at the end of the example VBS file as needed, and also needs to modify the directory of the VBS file C:\t.vbs