Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

batch file - DOS echo %variable% "Echo is ON"

I have a dos batch file.

mycommand.exe>c:emp
find /B Serv c:emp>c:emp2
set /p var1=<c:emp2
SET var2=%var1:~-7%
echo %var2%

This is only DOS, not windows environment.

The problem is that, the batch file output is: "Echo is ON". Can't echo the VAR2 variable.

mycommand.exe is a simple app. Not important.

> type c:emp"
VERSION 45.2
TAG1 NUMBER is 1234567
Serv NUMBER is 9654754

> type c:emp2
c:temp Serv NUMBER is 9654754

What can I do, If I would like echo the VAR2 variable? I can't use "setlocal enabledelayedexpansion" because setlocal "Command or filename not recognized".

Edit: What am I want exactly? I would like to ECHO mycommand.exe output 3rd line last 7 characthers. Thats all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

"good old DOS" is old, but not very good.

Your problem can be solved, basicly by building a temporary .bat file.

It's described in detail here:

https://support.microsoft.com/en-us/kb/66292

I have no DOS available, so I can't test, but this should work for you:

mycommand.exe>c:emp.txt
find "Serv " c:emp.txt>c:emp2.txt

REM init.txt should already exist
REM to create it:
REM   COPY CON INIT.TXT
REM   SET VARIABLE=^Z
REM ( press Ctrl-Z to generate ^Z )
REM
REM also the file "temp2.txt" should exist.

copy init.txt+temp2.txt varset.bat
call varset.bat
for %%i in (%variable%) do set numb=%%i
echo Server number is: %numb%
REM just because I'm curious, does the following work? :
set var2=%variable%
echo var2 is now %var2%

The manual creation of init.txt has to be done once only, if you can live with, that it always creates a variable with the same name (therefore the last two lines, so you could use the same init.txt over and over again - please feedback, whether it works - I'm quite curious)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...