简体   繁体   English

用VBS替换文件中的字符串并进行批处理

[英]Replacing string inside a file with VBS And batching

I am trying to build a batch file using VBS that will replace certain string inside a file. 我正在尝试使用VBS构建一个批处理文件,该文件将替换文件中的某些字符串。 Its working but the problem is i want to make the station variable to indicate on the dir since the file exists in some folders. 它的工作,但问题是我想使工作站变量在目录上指示,因为该文件存在于某些文件夹中。

here is what i did: 这是我所做的:

the VBS code: VBS代码:

Const ForReading = 1    
Const ForWriting = 2

strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close

strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText  'WriteLine adds extra CR/LF
objFile.Close

and here is the run batch code: 这是运行批处理代码:

    :input
@echo off
CLS
ECHO   #############################################
ECHO   #                                           #
ECHO   #  1 - AM                                   #
ECHO   #  2 - PM                                   #
Echo   #  3 - Exit                                 #
ECHO   #                                           #
ECHO   #############################################


SET O=""
SET STATION =""
SET /P STATION=Set Your STATION number:
SET PATH ="C:\Envfile\%STATION%\Envfile.env"
SET /P O=Set Your Choice And Press Enter:
ECHO Loading .........
IF %O%==1 GOTO AM
IF %O%==2 GOTO PM
IF %O%==3 GOTO Exit
IF %O%=="" GOTO input
goto done

:AM

    cscript replace.vbs %PATH% "PM" "AM"
goto done

:PM

    cscript replace.vbs %PATH% "AM" "PM"
goto done


ELSE 
GOTO input

as you can see the user passes the "station number var" at the beginning of the code and I want it to endicate on the Envfile.env file that located in folder named after the station number(for ex:1123). 如您所见,用户在代码的开头传递了“站号var”,我希望它以以站号(例如:1123)命名的文件夹中的Envfile.env文件结尾。

for some reason it's not working. 由于某种原因,它无法正常工作。

help? 救命?

解决了这个问题,显然从计算机的环境路径中删除了vbscript编译器的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM