1.

Solve : note.bat?

Answer»

So I've been working on this project to redirect stdout and stderr into a temporary text file so you can view help MENUS in a separate window easily.  It works for the most part at the moment, but for some reason it crashes with no error when executed on a batch file.  Does anyone have any idea's what could be going wrong or how to fix it?

Code: [Select]:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::
::: SCRIPT.........: note.bat
::: VERSION........: 3.0
::: DATE...........: 09/11/2014
::: AUTHOR.........:
::: DESCRIPTION....: Script that will TAKE input and execute
:::                  the input, sending all stdout to a temporary
:::                  file located at note.txt.  The script
:::                  will then open the file and display stdout
:::                  in as text in a notepad document.
:::                  This allows for movement and recollection of
:::                  help menus and other key information.
:::
:::      note [command]
:::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


rem echo off
setlocal

set _=%*
if not defined _ goto :help
if /i "%1"=="/?" goto :help

%_% >note.txt 2>&1
ping ::1 -n 2 >nul
start notepad note.txt
ping ::1 -n 2 >nul
del /f note.txt
goto :exit

:help
echo.
echo Enter command to send stdout to a notepad window.
echo.
echo.  note [command]
echo.
:exit
endlocal

How did you learn all that batch? Programming?

If you wouldn't mind also telling me what program do you use to DEBUG and run your scripts and programs?..See if this solves the issue.  Without sample input command lines it's unclear how you are using it.

Code: [Select]call %_% >note.txt 2>&1 Quote from: RedDos7665 on September 17, 2014, 09:35:38 AM

How did you learn all that batch? Programming?

If you wouldn't mind also telling me what program do you use to debug and run your scripts and programs?..
There really isn't a TRUE IDE for writing batch.  You basically can write your programs in any text editor and then double click on them to run them or run them from the cmd line by typing the name of the batch file.

Most people do chose to use a programming text editor which helps in highlighting key words that are commands or variables.  Some of these editors allow you to also run the program from editor.  I prefer to use Notepad++ for all my batch files. Quote from: foxidrive on September 17, 2014, 10:36:43 AM
See if this solves the issue.  Without sample input command lines it's unclear how you are using it.

Code: [Select]call %_% >note.txt 2>&1

I just tried that, and here is what I found:
*I edited it to write to %temp%\note.txt instead of just note.txt, Should not alter outcome.

Code: (cmd (no call)) [Select]Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\cmdPlugins>note repl /?

C:\cmdPlugins>rem echo off

C:\cmdPlugins>setlocal

C:\cmdPlugins>set _=repl /?

C:\cmdPlugins>if not defined _ goto :help

C:\cmdPlugins>if /I "repl" == "/?" goto :help

C:\cmdPlugins>repl /?  1>note.txt 2>&1
Code: (cmd (call %_%)) [Select]C:\cmdPlugins>note repl /?

C:\cmdPlugins>rem echo off

C:\cmdPlugins>setlocal

C:\cmdPlugins>set _=repl /?

C:\cmdPlugins>if not defined _ goto :help

C:\cmdPlugins>if /I "repl" == "/?" goto :help

C:\cmdPlugins>call repl /?  1>C:\Users\LEMONI~1\AppData\Local\Temp\note.txt 2>&1


C:\cmdPlugins>ping ::1 -n 2  1>nul

C:\cmdPlugins>start notepad C:\Users\LEMONI~1\AppData\Local\Temp\note.txt

C:\cmdPlugins>ping ::1 -n 2  1>nul

C:\cmdPlugins>del /f C:\Users\LEMONI~1\AppData\Local\Temp\note.txt

C:\cmdPlugins>goto :exit

C:\cmdPlugins>endlocal

So it runs when you use 'call %_%' but it writes the help menu to call and not repl (the batch file I'm testing with).


'start %_%' does not work, because it opens the help menu for repl in another cmd window, and does not redirect it to note.txt

Quote from: RedDos7665 on September 17, 2014, 09:35:38 AM
How did you learn all that batch? Programming?

If you wouldn't mind also telling me what program do you use to debug and run your scripts and programs?..

I learned mostly by reading these threads and downloading and examining other people's scripts. Quote
but it writes the help menu to call and not repl

That's funny. 

It's a bug in cmd that shows the help for the call command, but this is all you need:

Code: [Select]echo off
%* >"%temp%\note.txt" 2>&1 & notepad "%temp%\note.txt"


It will only ever have the one temp file and will overwrite it every time.

Quote from: foxidrive on September 17, 2014, 09:01:46 PM
That's funny. 

It's a bug in cmd that shows the help for the call command, but this is all you need:

Code: [Select]echo off
%* >"%temp%\note.txt" 2>&1 & notepad "%temp%\note.txt"


It will only ever have the one temp file and will overwrite it every time.

Wow, no idea why that fixed it, but it works now.  Thanks! Quote from: Lemonilla on September 18, 2014, 10:38:46 AM
no idea why that fixed it, but it works now.

If you write a batch file - and that script executes another batch file - then you need to use call to return to the next line in the script, otherwise the executed batch file will take full control and never return.

This is the reason why your batch file didn't continue when you used note repl /? because repl ran and then it exited.
Owing to the bug you found with call, you can't use call to simply fix your script.

The batch file I posted above only has one real line so call isn't needed to return control to the script - and notepad is launched using the & command separator.

HTH


Discussion

No Comment Found