admin管理员组文章数量:1122846
I have an environmental property file in Gauge:
env
-> Local
-> local.properties
where there is a property VDI_NAME
like VDI_NAME=N
as default value.
I want to set it to host name of the computer.
I have tried using:
set host=%COMPUTERNAME%
For /F "tokens=1* delims==* %%A IN (env/LOCAL/local.properties) DO (
IF "%%A"=="VDI_NAME" set VDI_NAME=%COMPUTERNAME%
)
I have an environmental property file in Gauge:
env
-> Local
-> local.properties
where there is a property VDI_NAME
like VDI_NAME=N
as default value.
I want to set it to host name of the computer.
I have tried using:
set host=%COMPUTERNAME%
For /F "tokens=1* delims==* %%A IN (env/LOCAL/local.properties) DO (
IF "%%A"=="VDI_NAME" set VDI_NAME=%COMPUTERNAME%
)
Share
Improve this question
edited yesterday
Mofi
48.9k18 gold badges87 silver badges153 bronze badges
asked yesterday
Thonas1601Thonas1601
1,4072 gold badges10 silver badges12 bronze badges
1
- Please take Stack Overflow (SO) tour to learn how to say thanks on SO by accepting an answer on being helpful by clicking on gray check mark symbol ✓ left to an answer. See also How does accepting an answer work? A question with no accepted answer is rated as a not answered question even if there are good answers like here on this question. This teaching comment will be deleted by me in 2 days at the latest. So please don't reply on this comment. Thanks for reading. – Mofi Commented 12 hours ago
2 Answers
Reset to default 1It is of no use to define an environment variable VDI_NAME
on processing the lines of the file env\LOCAL\local.properties
referenced with a path relative to the current working directory which can be any directory on processing a line with VDI_NAME
as first substring between beginning of line or a sequence of =
or *
and end of the line or another sequence of =
or *
if the goal is to update the value of such a line in the file itself.
There should be read the Microsoft documentation about Naming Files, Paths, and Namespaces explaining how to reference files and folders on Windows with \
as directory separator and not /
as on Linux/Mac.
The Windows Command Processor cmd.exe
processing a batch file is the only script interpreter installed by default on Windows which does not support searching in a text file for a string and replacing it with a different string. PowerShell with a PowerShell script or the Windows Script Host with a VBScript or a JScript script would be much better for such a task.
See also: How can you find and replace text in a file using the Windows command-line environment?
Here is a batch file solution for the task using only Windows commands and no other script interpreter.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "PropertiesFile=%~dp0env\LOCAL\local.properties"
if not exist "%PropertiesFile%" exit /B
%SystemRoot%\System32\findstr.exe /I /L /X /C:"VDI_NAME=%COMPUTERNAME%" "%PropertiesFile%" 1>nul && exit /B
(for /F delims^=^ eol^= %%I in ('%SystemRoot%\System32\findstr.exe /N "^" "%PropertiesFile%"') do (
set "Line=%%I"
setlocal EnableDelayedExpansion
if "!Line:*:=!" == "" (echo(!Line:*:=!) else for /F "tokens=2 delims=:=" %%J in ("!Line!") do if /I "%%J" == "VDI_NAME" (echo VDI_NAME=!COMPUTERNAME!) else echo(!Line:*:=!
endlocal
)) 1>"%PropertiesFile%.tmp"
if not exist "%PropertiesFile%.tmp" exit /B
%SystemRoot%\System32\fc.exe /B "%PropertiesFile%" "%PropertiesFile%.tmp" 1>nul && goto DelTempFile
for %%I in ("%PropertiesFile%") do set "PropertyAttributes=%%~aI"
if "%PropertyAttributes:r=%" == "%PropertyAttributes%" (move /Y "%PropertiesFile%.tmp" "%PropertiesFile%" 1>nul) else echo File "%PropertiesFile%" is read-only!
:DelTempFile
del "%PropertiesFile%.tmp" 2>nul
endlocal
NOTE: This batch file does not work for a UTF-16 encoded properties file.
There is defined with the first two lines the required execution environment with:
- disabled command echo mode,
- enabled command extensions,
- disabled delayed variable expansion.
Next is defined the environment variable PropertiesFile
with its fully qualified file name based on the batch file directory path referenced with %~dp0
which always ends with a backslash.
Then is checked if the properties file exists at all. An immediate exit of the batch file processing is done as nothing else to do in case of the properties file does not exist where it is expected.
There is used next FINDSTR to check if the line beginning with VDI_NAME=
already has the computer name as string value in which case no modification is necessary, and the processing of the batch file is exited too.
If VDI_NAME
is not the current computer name, there is a loop executed processing all lines of the properties file including empty lines usually skipped by FOR /F to create a copy of the properties file with file extension .tmp
in which the line beginning with VDI_NAME=
is written into the temporary file with current computer name. See my answer on How to read and print contents of text file line by line? for a description of most parts of the FOR /F loop.
The creation of the temporary file could fail for some reason like a write-protected directory for the current user. In this case the processing of the batch file is exited without modifying the properties file as not possible.
A binary file comparison is done to determine if the temporary file is byte by byte identical to the properties file. The properties file is kept as is if the properties file is identical with the temporary file like on properties file not containing case insensitive VDI_NAME
at the beginning of a line.
The file attributes of the properties file are determined next. If a user set the read-only attribute on the properties file to prevent its modification, this user configuration is taken into consideration and the properties file is not updated in this case.
The properties file is replaced with the temporary file with the modified line beginning with VDI_NAME
on not being read-only. The last modification date of the properties file is changed only if the file content is really changed by the batch file.
The temporary file should not exist anymore on typical use cases. The still existing temporary file is deleted by the batch file if the properties file does not contain a line beginning with VDI_NAME
, or it is read-only, or it is opened in another application which prevents replacing the properties file with the temporary file.
The initial environment is restored with the last command line as it is also done implicit by the Windows Command Processor on exiting the processing of this batch file. The last line could be removed for that reason.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
… explains%~dp0
… full batch file directory pathdel /?
echo /?
endlocal /?
exit /?
fc /?
findstr /?
for /?
if /?
move /?
set /?
setlocal /?
Assuming you have a file .\env\Local\local.properties
which contains a line VDI_NAME=N
that you want changed to VDI_NAME=the current COMPUTERNAME
,
@ECHO OFF
SETLOCAL
rem The following setting for the directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names.
rem in your case, since you seem to be operating on a file in the current directory,
rem this shoulf be set to . instead of u:\your files
SET "sourcedir=u:\your files\env\local"
SET "filename1=%sourcedir%\local properties"
(
FOR /f "usebackqdelims=" %%e IN ("%filename1%") DO (
FOR /f "delims==" %%y IN ("%%e") DO (
IF "%%y"=="VDI_NAME" (ECHO VDI_NAME=%computername%) ELSE (ECHO %%e)
)
)
)>"%filename1%.new"
FC /w "%filename1%.new" "%filename1%"
MOVE /y "%filename1%.new" "%filename1%" >NUL 2>NUL
GOTO :EOF
First, read the file, line-by-line to %%e
. Note that empty lines will be ignored.
Then see whether the line starts VDI_NAME=
If so, echo
the modified line. If not, regurgitate the line read.
(
for ... do (
)
)>filename
will gather all echo
ed output to a new file filename
.
The fc
command is for testing, comparing the original and new files. The /w
switch ignores whitespace so any empty lines from the original, which will be skipped by for/f
will not show as a difference.
The move
will silently overwrite the the original file. Note that the destination filename in the (for...())>filename
cannot be the source filename %filename1%
.
It would be prudent to comment-out the move
line for testing.
本文标签: How to set a gauge environmental property using batch fileStack Overflow
版权声明:本文标题:How to set a gauge environmental property using batch file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281276a1926268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论