VBS batch modification file, Bat batch file invoke execute VBS, and print return value (VBS run result) in CMD window _vbs

Source: Internet
Author: User
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

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.