I got a batchfile with a length of 4776 lines. Now i want the batchfile to show the commands and text from the batchfile itself I've written.
Anticipated thanks
VVW
I like adding this at the beginning of my batch scripts:
#ECHO OFF
IF /i {%1}=={ECHO} ECHO ON&SHIFT
This checks if the first argument is "ECHO" (case-insensitive). If it is, it turns ECHO ON, and then shifts the arguments such that %2 becomes %1, %3 becomes %2.
This way the script can behave as though you ran it normally, but with ECHO ON.
Remove the below line from the batch file.
#echo off
Or make it on by doing
#echo on
Try putting your code in a variable, then print it.
mycode = %Code Code Code%
echo %mycode%
Try putting this ain your batch file.
#type %0
Maybe I am misunderstanding things but it seems like everyone is misunderstanding where the "#" symbol is necessary. If you have this:
#echo off
Then you run commands like this:
#call mycommand.bat
But, if you have this at the top of your file:
echo off
Then, your commands are silenced like so:
call mycommand.bat
You could just open the command line and type this
type yourbatchfilename.bat >> code.txt
This would write your whole thing again in a txt file
Related
I've recently started working with .bat files, and I'm trying to redirect the output to a file.
I've found 2 options, so far:
echo aaa > out.txt - which sends the output of the single echo command to the specified file (can also be appended using >>)
calling the entire file from the cmd using somefile.bat > out.txt (which is actually similar to number 1, as it sends the output of the single command somefile.bat to out.txt)
What I'm looking for is something else - I'm trying to have a line in my file that sends all the output from that point forth to the file.
Thanks!
echo this goes to screen
(
echo this line goes to the file
echo also this line and the ping-output
ping www.stackoverflow.com
echo and this
)>file.txt
echo this goes to screen again
Note:
all inside the block (between ( and )) is parsed at once. If you use variables inside the block, you may need delayed expansion.
There is no universal solution. It depends of the batch file requirements.
For a lot of batch files, the answer from Stephan will work without problems, taking in consideration what he pointed: all the code is inside a block and any variable management inside it may require delayed expansion.
Other alternative is to move the code under a subroutine, calling it with the redirection
#echo off
call :mainProcess %* > outputFile
exit /b
:mainProcess
:: here the batch file begins
echo %1 %2 %3
I have a text file that looks something like this:
P4657_1
P1352_1
P3126_1
and so on.
I need a batch script that removes the "P" at the beginning and the "_1" at the end.
I found many script examples in this forum but none worked for my needs.
Can anyone help?
sed -E 's/^P//g; s/_1$//g' yourinfile > outfile
by virtue of the first substitution beginning P's get nuked, by the second final _1's get nuked and you are done.
For a list of options for modifying text files, see How can you find and replace text in a file using the Windows command-line environment?.
My favorite option is a hybrid JScript/batch utility I wrote called REPL.BAT. The script does not require installation of any 3rd party executables, and it will run on any modern version of Windows, starting with XP.
Using REPL.BAT, the solution is as simple as:
type "yourFile.txt" | repl "^P(.*)_1$" "$1" >yourFile.txt.new
move /y "yourFile.txt.new" "yourFile.txt" >nul
Thanks to everyone.
I did find a solution elsewhere: http://www.dostips.com/forum/viewtopic.php?f=3&t=1498
the code is very simple:
set "search=P"
set "replace="
for /F "delims=" %%a in (input.txt) DO (
set line=%%a
setlocal EnableDelayedExpansion
>> output2.txt echo(!line:%search%=%replace%!
endlocal
)
You can use echo apparently. So, print your file with type, and for each line, assign the line to a variable, edit the variable, and then echo the variable.
**Batch file **I am writing batch file that takes in command line parameters and do some stuff if the parameters match. But when the run the batch file again with the new parameters, the file still runs with the old parameters.
first run : file.bat -name hello -w 400 -r 320
second run : file.bat -name hello -w 400.
When i do the second run of the file, it still performs the first run, i mean to say it still considers the parameters from the first run. Does anybody know why this is the case?
Thanks in advance.
Sorry for not posting the batch file code.
below mentioned is the code.
set filename=
set rate=
set hidden=
set rate=
:recheck
if "%1"=="-help" goto :help
if "%1"=="-name" goto :filename
if "%1"=="-h" goto :hidden
if "%1"=="-w" goto :weight
if "%1"=="-r" goto :rate
if "%~1"=="" goto :endofcommandline
:filename
shift
set filename =%1
shift
goto :recheck
For the other part of the file, I use the variable "filename" to do other stuff. I also use setlocal and endlocal at the beginning and the end of the file.
I have other corresponding labels for the each if statement and set different variables to the values passed to them if any by the user.
Sometimes, I also get echo is off when I try to print out the value of some variable.
All the variables which I use are cleared in the beginning of the file as mentioned above.
There are a couple points here.
"I also use setlocal and endlocal at the beginning and the end of the file."
"All the variables which I use are cleared in the beginning of the file as mentioned above."
Setlocal command does NOT clear the variables, it preserve current variables from posterior modification. This mean that if in a previous execution of your Batch file, or even via typed SET commands, filename variable was defined, it value remains until it is deleted or modified.
"Ok!", you said, "filename variable is modified the second time the Batch file run, Right?"
Well, no... The following line:
set filename =%1
does NOT modify "filename" variable, but "filename " instead (filename and a space). Try this:
set filename=No space
echo %filename%
set filename =Bad name with trailing space
echo %filename %
echo %filename%
You must carefully check your program to avoid this type of errors.
How can I run a command in my dos script from an input argument ?
Simplified script bla.bat:
CALL %1
Call it:
bla.bat "echo 'hello'" (or bla.bat "git status")
Error:
'"git status"' is not recognized as an internal or external command,
operable program or batch file.
It works if I do "CALL git status".
The important thing to remember is that the expanded text must look exactly like it would if you were to simply key in the command from the command line. (actually there are a few exceptions, but this is a good starting point).
To debug your script, simply put an echo before your call: #echo call %1. Now try running as you did earlier: blah.bat "echo 'hello'" produces call "echo 'hello'". Try running that from the command line - it doesn't work. You want call echo 'hello'.
One fix would be to change your script slightly: The ~ modifier strips enclosing quotes from the argument
#echo off
call %~1
Or you might be able to ditch the call and simply use the following (as long as you are not calling another batch file from which you want to return)
#echo off
%~1
If there are no other arguments on the command line, you might be better off using %* which expands to all the arguments
#echo off
%*
REM or call %*
Now you can call your batch like so
blah.bat echo "hello"
Be aware that batch has all kinds of special case weirdness that will likely require extra or different coding to work around. Too many to list - just expect the unexpected.
It looks like the problem may be that you have surrounding quotes in your input, which you'll need to stop it being broken into the different %n arguments, but try:
%~1 Which will strip any surrounding quotes from the input.
%~$PATH:1 which will strip any surrounding quotes then search within the $PATH env-var for the first match, then expand the string to include the full path to the command, which won't work for git using the windows distro because git is a batch file, and cmd would look for git status.bat
If its to be used with git, you may as well use %~1 and %~2 to call git then provide the argument to the git batch file, or call git.exe directly by modifying your $PATH. But remember that git.bat does some enviroment setup of its own before calling git itself.
I think you'll need %1% to echo the parameters. Here's my lame script which I think does what you want, works with your echo test:
bla echo hello
Gives:
C:\tmp>echo bla
bla
C:\tmp>echo echo
echo
C:\tmp>CALL echo hello
hello
echo %0%
echo %1%
CALL %*
If you want to parse through the command line arguments, let me know.
The problem is the spaces between the parameters are throwing you off (which is why you were using the quotes around git status).
Modify your bla.bat to iterate through your command line paremeters. This works for me:
SETLOCAL ENABLEDELAYEDEXPANSION
SET VAR1=
FOR %%A IN (%*) DO (
SET VAR1=!VAR1! %%A
)
call %VAR1%
ENDLOCAL
Then, run your bla.bat without the quotes around git status.
bla git status
Essentially, what this does is iterate through your command line parameters, and then executes them all as one command. The challenge comes from FOR loops in DOS not allowing you to use a variable that you're setting within itself, so you need to enable "delayed expansion" of variables. Then, the variable that you're setting needs to be encapsulated in exclamation points (not %'s). And of course, the space between !VAR1! and %%A keeps the parameters from running together in the CALL.
I am trying to write a batch file to iteratively execute a fortran compiled executable. Normally one would go to the windows command prompt, type 'Model.exe'. This would bring up a dos command window asking the user to type a required file name directly in to the command window at the dos prompt.
I want to write a batch file that will do this bit for me, and also iterate this step so that I can run 10 simulations consecutively instead of having to do it by hand. This kind of shell operation would be straightforward in linux, but I do not have this available.
My pseudo code would look like this:
for /L %%run in (1,1,10) do
(set str=Sim%%run
echo.%str% > input.txt
Model.exe < input.txt)
You could break this down in to the following steps:
Assign variable 'run' a value. (e.g. 1)
Concatenate this with a string ("Sim") to make a new variable, "Sim1"
echo this to a text file ("input.txt")
Read the variable "Sim1" from file "input.txt"
Executable goes away and does its thing.
Repeat steps 1 -> 5, but with a new variable calle "Sim2" etc.
I can get the above to work if I use set str=Sim1 and then echo this directly to "input.txt", but I cannot get this to work as a loop. Am I missing something?
Best regards,
Ben
Ugh, cmd.exe's treatment of variable expansion is ugly. So, you need "delayed expansion", as follows:
setlocal enabledelayedexpansion
for /L %%i in (1,1,10) do (
set str=Sim%%i
echo !str! > input.txt
Model.exe < input.txt)
endlocal
(Of course in this particular case you could just say echo Sim%%i > input.txt but I assume there's a good reason why you want to go via another variable.)