What I have:
I have a simple batch file with ASCII characters to make a semi graphical menu for MS DOS, drawing a box and placing text with "#echo off" at the start of my file and each line in editor beginning with "echo". I have not implemented color codes, nothing fancy yet.
Is there a way to display the output of the 'ver' command on the same line as something that is 'echo'd?
Basically, I would like to attempt and achieve and output on a line similar to:
|| <---MS-DOS ver. 6.22---> ||
Where the pipes are ASCII characters (ALT-186) and 'escape' the echo to run 'ver' then return to printing the end character (alt-186)
I remember doing something like it YEARS ago when I was fumbling around on my computer, but unlike riding a bike, I have forgotten many tricks I had at the time.
I have Googled for quite some time, the last few days between work and sleep on my free time, but everything is geared toward the Windows XP or newer command line instead of DOS. I have read many articles on batch scripting and while helpful in relearning other tricks, they are all still geared toward newer CLI. I have read up on escaping and for some reason I am just not getting it. Maybe I have spent too much time trying to figure this out on my own and have burned out? Any help or link to the proper articles will be appreciated. Sample code even better as that is how I learned way back then.
For the case of MSDOS-Version exists a simple solution.
echo #prompt $b$b $l--$V--$g $b$b > temp.bat
%comspec% /c temp.bat
This works, because $V will be translated into the MSDOS-Version, for output of other programs it's more complicated.
Attention: It only works inside a batch program, because variable expansion isn't supported on the command line.
On the command line you could use:
echo #prompt $b$b $V $b$b > temp.bat
C:\command.com /c temp.bat
Output: || <--MS-DOS Version 6.22--> ||
In MS-DOS 6.22 it's tricky to output text without line feed.
In the most cases you should build the complete line before you try to output it.
Or you can try predefined files without a linefeed.
Today I do the echo without line feed this way, ...
echo.|set /P =text without CR/LF
... but I can't say if this would have worked in the early nineties.
Related
I have a file (let's call it version.txt) that contains a version number and some text:
v5.02
Some text explaining
where and how this
number is used
Based on this answer, I use
set /p version=<version.txt
to store the first line of the file in the version variable. Now I'm trying to write a batch script that operates on folders that contain this version number in their name. However, I get unexpected results because something seems to go wrong when I insert the variable in a path. For example, this script
#set /p version=<version.txt
#echo C:\some\folder\%version%\some\file.exe
prints
C:\some\folder\v5.02
instead of
C:\some\folder\v5.02\some\file.exe
What's going on? I have a feeling there are hidden characters of some sort at the end of the text in the variable, because setting the variable by hand to a constant in the script works.
Edit: I'm using Windows 10 with Notepad++ as my editor, if it helps.
I can only replicate your issue, when version.txt uses Unix line endings (LF) instead of Windows (CRLF). for /f is immune to this issue:
for /f "delims=" %%a in (version.txt) do set "verion=%%a" & goto :skip
:skip
echo C:\some\folder\%version%\some\file.exe
goto :skip breaks the loop after reading the first line.
Since everything I tried didn't seem to work, the solution I found in the end is to call the batch script from a Python script. The Python script reads the first line of the version file and passes it as an argument to the batch script. Out of context, it is a bit of an inelegant solution, but in my case the batch script was already called by a Python script, so it's not that terrible.
Here is a minimal example:
version.txt
v5.02
Some text explaining
where and how this
number is used
script.bat
#echo C:\some\folder\release\%1\some\file.exe
script.py
import os
with open("version.txt") as f:
version = f.readline().rstrip()
os.system("cmd /c script.bat %s" % version)
Edit: Following Stephan's comment, I tried to change the line ending in the text file from LF to CRLF and it indeed solves the problem. However, since I don't really have control over everything that writes in that file, the solution above remains the most feasible in my case.
Edit 2: Stephan's answer (with the for loop) is actually a better solution than this one since it avoids having to transfer part of the work to the calling Python script.
I've had this small problem for quite some time now and I haven't been able to find a solution even after excessive googling.
In this guide it is described how LaTeX can be written and compiled using npp++ and nppExec together with SumatraPDF.
Through some clever scripts and the use of DDE commands (through CMCDDE.exe), it is even possible to use sumatra's ForwardSearch to jump back and forth from the .tex to the .pdf. Everything works great, unless the path to the .tex file contains a character that's not in the English alphabet (for example åäö).
The CMCDDE command then fails because the path sent to sumatra's ForwardSearch reads (for example) C:\†„” instead of C:\åäö. This has to be an encoding issue and I haven't been able to find a working solution.
So, if I have this .tex file C:\åäö\MWE.tex with
\documentclass{article}
\begin{document}
Hello World!
\end{document}
it won't open correctly when using the batch script described in the link above. These are the problematic batch file lines:
::Writes the commands that are to be executed using CMCDDE.exe (through cmcdde.tmp)
echo SUMATRA>"%~dp1build\cmcdde.tmp"
::%~dp1build transforms into C:\åäö\build for the MWE
echo control>>"%~dp1build\cmcdde.tmp"
echo [ForwardSearch("%~dp1build\%~n1.pdf", "%~f1", %2, 0, 0, 0)]>>"%~dp1build\cmcdde.tmp"
:: This gives the following line in cmcdde.tmp: [ForwardSearch("P:\Documents\†„”\build\MWE.pdf", "P:\Documents\†„”\MWE.tex", 3, 0, 0, 0)] (in ANSI encoding)
"P:\Documents\localtexmf\cmcdde.exe" #"%~dp1build\cmcdde.txt"
:: This fails because the path P:\Documents\†„”\build doesn't exist (P:\Documents\åäö\build does)
Ideas I've had and tried:
I tried changing the encoding by using chcp 65001 and chcp 865 but
haven't been able to get it to work.
I have also tried using a
search and replace
script to go through the cmcdde.tmp file before executing it but it didn't
succeed (I'm not sure which signs I should search for to exchange for
åäö, †„” or åäö or ├Ñ├ñ├ or other?)
I have also tried putting
cmd /c or cmd /a /c before the echo commands (as sort of described
here)
but it doesn't seem to make a difference.
Please let me know if something which parts of the question that are unclear.
I managed to find a solution. Turns out using the code page 1252 solves the problem.
So putting chcp 1252 at the beginning of the batch file is the solution. However, if you're using other none standard characters than me (other then the ones in the nordic alphabet), I'm guessing you might need another code page.
Experiment and test different ones until you get it working.
I'm making a batch program that is supposed to be like a simplified(-ish) command prompt for commands that I make. I am wondering, though, about this line of batch coding that I have narrowed down as a fatal error (causes the cmd.exe running the program to close) causing line: echo One(1) application is within this folder. When I attempt to go to that section via inputting the command to run that section it states: application was unexpected at this time followed by immediately closing. I have also tried replacing "application" with "program", to no avail. I was wondering: What are all of the mysterious echo parameters/rules. for instance echo text >> name.extention is possible, but typing echo /? does not give you anything besides #echo on/off and echo text.
I believe it is something to do with the (), as in other languages it is used to call a function with the arguments within the (), but I do not understand why it would be this, as it is not running the "One" function because it is inside an echo, meant only to display it literally. Also, I don't believe batch has functions that can be called like this (I've only seen them in VB, lua, java, and C++ [of the ones I use])
If anyone knows why the program failed at this line, and/or (preferably and) all of the other hidden echo rules, please list them out for us; they really need to be known (I've seen so many questions on this website [and others] specifically about the echo command).
Within a block statement where you have if expression (whatever1) else (whetever2) then you need to escape a closing parentheseis ) with a caret ^ thusly : ^)
The escape tells batch that the ) is NOT the termination of the then/else
I have a Windows batch file which has an instruction to execute an EXE file in a location whose path contains accented characters. Following are the contents of the batch file.
#echo off
C:\español\jre\bin\java.exe -version
C:\español\jre\bin\java.exe - This path exists and is proper. I can run this command directly on cmd.exe. But when I run the command from a bat/cmd file it fails saying "The system cannot find the path specified"
One way to fix this is by setting code page to 1252 (that works for me). But I'm afraid we'd have to set code pages for any non-English locale and figuring out which code page to use is pretty difficult.
Is there an alternative approach to fix this problem? Maybe a command-line option or something else?
Another way of doing this, in Windows, is by using wordpad.exe:
Run wordpad.exe
Write your script as you usually do, with accents
Choose Save as > Other formats
Choose to save it as Text document MS-DOS (*.txt)
Change the file extension from .txt to .bat
I had the same problem, and this answer solved it. Basically you have to wrap your script with a bunch of commands to change your terminal codepage, and then to restore it.
#echo off
for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
chcp 1252>nul
:: your stuff here ::
chcp %cp%>nul
Worked like a charm!
I'm using Notepad++ and it has an option to change "character sets", OEM-US did the trick. ;)
Since you have #echo off you can't see what your batch is sending to the command prompt. Reproducing your problem with that off it seems like the ñ character gets misinterpreted since the output I see is:
C:\espa±ol\jre\bin\java -version
The system cannot find the path specified.
I was able to get it to work by echoing the command into the batch file from the command prompt, i.e.
echo C:\español\jre\bin\java.exe -version>>test.bat
This seems to translate the character into whatever the command prompt is looking for, though I've only tested it with English locale set so I don't know if it'll work in all situations for you. Also, if you open the batch in a text editor like notepad it looks wrong (C:\espa¤ol\jre\bin\java.exe)
Use Alt + 0164 for ¤ instead of Alt + 164 ñ in a batch file... It will look odd, but your script should run.
You can use Visual Studio Code and it will let you select the encoding you want to use. Lower right corner, you select the encoding and will display option "save with encoding". Select DOS and will save the accented chars.
I also had the same problem. I was trying to create a simple XCOPY batch file to copy a spreadsheet from one folder to another. Its name had the "é" character in it, and it refused to copy.
Even trying to use Katalin's and Metalcoder's suggestions didn't work on my neolithic Windows XP machine. Then I suddenly thought: Why not keep things as simple as possible (as I am myself extremely simple-minded when it comes to computers) and just substitute, in the batch file code, "é" with the wildcard character "?".
And guess what? It worked!
On a cmd prompt or bat file, I issue the following:
start textpad myfile.txt and it works fine.
If the program textpad does not exist on the computer, then an error sound and a popup occurs which the OK button must be pushed.
I desire to trap this error so that I could do something like
start textpad myfile.txt || start notepad myfile.txt
where the || implies that if the start of textpad is not successful, then the start of notepad should occur. HOWEVER, I still get the error sound and requirement of hitting OK.
My intent is to avoid the sound and the requirement of any user intervention.
I have also tried the following bat approach below, to no avail.
start textpad
if not %ERRORLEVEL% == 0 GOTO END
start notepad
:END
Any help would be great.
thanks
ted
You can use the following little snippet to find out whether the program you intend to launch exists:
for %%x in (textpad.exe) do set temp=%%~$PATH:x
if [%temp%]==[] echo Didn't exist.
But may I suggest that you simply use
start foo.txt
instead of forcing a specific editor onto the user? File type associations are there for a reason.
I do not believe you will find a way to make that work. Perhaps look for the existence of textpad.exe on the machine? If you can predict what directory it would be loaded from, this should be pretty easy using IF EXIST.
There are some techniques to detect the presence of a specific tool, but this only works for command line tool, or with GUI applications also supporting command line options.
For these tricks, take a look at this page.
"/wait" parameter would do the trick for you..
START /wait NOTEPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives zero as output.
START /wait TEXTPAD.EXE SOME.TXT
echo %ERRORLEVEL%
# This gives non-zero output.
You probably already have an answer, but my over-sized ego has forced me to post my answer.
So, this should work.
start textpad 2> nul||start notepad
This will start notepad if the command start texpad fails, while also redirecting any error message you may get from the first command.