cmd - Windows equivalent of "echo -n" no longer works in Win7 -
i had nifty trick in windows cmd.exe
(at least xp) emulate behaviour of unix echo without newline, echo -n
. example, command:
<nul: set /p junk= xyzzy
would result in 6 characters being output, leading space , string "xyzzy", , nothing else.
if you're interested in why works, it's input command outputs
" xyzzy"
prompt waits user input before assigning inputjunk
environment variable. in particular case, doesn't wait user input since grabs inputnul
device.
it rather useful in cmd
scripts when (for example) processing files in loop (one iteration per file) want list more 1 per line. using trick, output each file name followed space , no newline then, after loop, output newline finish up:
processing files: file1.txt file42.txt p0rn.zip
now discover that, under windows 7, spaces no longer output is:
processing files: file1.txtfile42.txtp0rn.zip
is there way can set /p
start honouring spaces again, or there way in win7 achieve same effect?
i've tried quoting, using .
(which works in echo
) , escaping string ^
, none of them seem work:
c:\pax> <nul: set /p junk= xyzzy xyzzy c:\pax> <nul: set /p junk=" xyzzy" xyzzy c:\pax> <nul: set /p junk=' xyzzy' ' xyzzy' c:\pax> <nul: set /p junk=. xyzzy . xyzzy c:\pax> <nul: set /p junk=^ xyzzy xyzzy
what need is:
c:\pax> some_magical_command_with_an_argument xyzzy xyzzy
which give me space(s) @ start and no newline @ end.
this similar paxdiablo's answer, except use hybrid jscript/batch file instead of temporary vbscript file.
my script called jeval.bat
- , evaluates valid jscript expression , writes result stdout, optionally trailing newline. silly little script extremely useful batch programming.
assuming jeval.bat
either in current folder, or somewhere in path, can like:
call jeval "' xyzzy'"
here script. simple. of code related documentation, error handling, , built in system.
@if (@x)==(@y) @end /* harmless hybrid line begins jscrpt comment ::************ documentation *********** ::: :::jeval jscriptexpression [/n] :::jeval /? ::: ::: evaluates jscript expression , writes result stdout. ::: ::: newline (cr/lf) not appended result unless /n ::: option used. ::: ::: jscript expression should enclosed in double quotes. ::: ::: jscript string literals within expression should enclosed ::: in single quotes. ::: ::: example: ::: ::: call jeval "'5/4 = ' + 5/4" ::: ::: output: ::: ::: 5/4 = 1.25 ::: ::************ batch portion *********** @echo off if "%~1" equ "" ( call :err "insufficient arguments" exit /b ) if "%~2" neq "" if /i "%~2" neq "/n" ( call :err "invalid option" exit /b ) if "%~1" equ "/?" ( setlocal enabledelayedexpansion /f "delims=" %%a in ('findstr "^:::" "%~f0"') ( set "ln=%%a" echo(!ln:~3! ) exit /b ) cscript //e:jscript //nologo "%~f0" %* exit /b :err >&2 echo error: %~1. use jeval /? help. exit /b 1 ************ jscript portion ***********/ if (wscript.arguments.named.exists("n")) { wscript.stdout.writeline(eval(wscript.arguments.unnamed(0))); } else { wscript.stdout.write(eval(wscript.arguments.unnamed(0))); }
Comments
Post a Comment