Execute several commands in one line, and save stdout into a file - windows

In a batch file, I am trying to execute several commands in a single line:
start cmd /c "title SomeTitle & python SomeFile.py 1>SomeFile.txt & timeout /t -1"
The output file SomeFile.txt is created empty, and everything is printed directly to the terminal.
I've tried putting the python SomeFile.py 1>SomeFile.txt part within double-quotes.
However, I then get a The system cannot find the path specified error.
How can I work around this?

Related

Redirecting output of multiple bat files to one console window

I am running multiple microservices locally for development purposes. Currently I am doing it by a .bat script, where I am calling other .bat files, which are starting my services. It starts multiple console windows, and this is not handy to navigate between outputs.
The script which is starting services now looks like this:
start cmd /k call api_one.bat
start cmd /k call api_two.bat
...
In api_xxx.bat I am starting iisexpress with my api.
I am looking for a way to make it more comfortable. For example by redirecting all of the outputs to a single one, with for example a prefix to identify from which script output lines are from.
I had a similar problem, where I was calling a batch file from a batch file which started another batch file.. to complicated to explain but let me get you some code that helped me:
content from test.bat:
set logname=.\log.log
if exist %logname% ( type nul > %logname%
)
echo Now starting example.bat>>%logname%
call C:\example.bat >>%logname%
echo finished example.bat>>%logname%
so now here is content of example.bat:
echo starting integrated commands
start /b cmd /k "C:\example2.bat"
[some code to check for a process that starts in example2.bat]
echo finished integrated commands
and the content of example2.bat can be whatever you want and is also displayed in the logfile.
Note that my example2.bat has an exit at the end, because example.bat checks if example2.bat is running. only when it finishes it goes on with the script. For myself it worked out I got everything from all 3 scripts into one logfile.

PSEXEC without terminate batch job

I'm trying to make my windows service run bat file with
psexec -accepteula -h -i 1 myapp.exe --p1 lala --p2 lalala
but closing the myapp cmd window results with "Terminate batch job y/n" question and service doesn't get the fact that it needs to restart itself.
In order to ignore this question, I googled and tried add
< nul
to the end of the command, but that didn't help. I assume that psexec doesn't get the fact, that "< nul" part is not for myapp but for psexec itself.
So how can I make my psexec finish without any questions about terminating batch job?
One way to do it is to create a SECOND batch file. The second file will call the first batch file and it will append "< nul".
For example, say you had a batch file which contained the following line:
python manage.py runserver
Name this file something like file1.bat
Then create a second batch file which contains the line:
file1.bat < nul
Name the second file something like file2.bat Then, execute the second file from that point on in order to call your program.
This worked for me in a Windows 10 environment. Good luck.

Problems Executing Powershell from .cmd file

I am attempting to run a build task from a .cmd file where Powershell extracts a zip file, which helps bypass a problem with Visual Studio's limit on the number of directory characters. However, I am having problems getting the Powershell command to execute correctly. I've tried a number of variations with the quotations, and I either get a termination error, or the Powershell command outputs as a string with the zip file not extracted. Below is an example of my current .cmd file:
//%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
set Source=%1%directory.zip
set Destination=%1%directory
powershell -Command $pscmd = '{Add-Type -assembly "system.io.compression.filesystem";[io.compression.zipfile]::ExtractToDirectory("%Source%", "%Destination%");}'; Write-Host $pscmd;
I'm very open to a number of variations that can get this to work, provided that this task runs on the command line, uses Powershell, and can be executed from a .cmd file, which is triggered by our app's build process. I'll be happy to provide additional information if needed. Thanks!
This was a strange one. Your code above has some sort of hidden character in it. I took the code and opened it in notepad, saved as ANSI, and when you type it to command line or open it again in a new instance of notepad you can see the error.
Neither add-type nor ExtractToDirectory give output, so I removed your pscmd var.
I would open your existing script, save as ansi as a new file name, delete the original, rename the new one back to the original name.
Here is what I came up with to troubleshoot your script, and it works on my machine.
I named my script L:\util\unzip.cmd
setlocal
//%1% is a passed in command line argument for the absolute path, e.g. C:\path\to\dir
set _Source='%1\directory.zip'
set _Destination='%1\directory'
echo _Source=%_Source%
echo _Destination=%_Destination%
set _c1=Add-Type -assembly system.io.compression.filesystem;
set _c2=[io.compression.zipfile]::ExtractToDirectory(%_Source%, %_Destination%)
echo _c1=%_c1%
echo _c2=%_c2%
set _Command=^& {%_c1% %_c2%};
: to echo, use double quotes or cmd thinks text after & is another command
echo _Command="%_Command%"
pause
powershell -Command "%_Command%"
endlocal
I ran it like this, and it worked: unzip.cmd L:\util
I'll bet this this info, you are good to go.

Make System information Batch file

How to create a batch file about system info and ip address and domain server etc. _ which will generate a output as txt file
You can put whatever Windows commands you want in a batch file and then redirect the output to a text file using >. E.g., you could create a batch file called test.bat and put the following commands in it:
#echo off
systeminfo
ipconfig /all
Then you could run test.bat from a command prompt as follows:
C:>test >outfile.txt
The output of the batch file, which would consist of the output from the commands you placed in it, would be redirected to a file named outfile.txt by the redirect operator, >. Note: it can sometimes take awhile for the output of the systeminfo command to complete, so, depending on your system, you may need to give it a minute to complete.
Alternatively, you could add the >outfile.txt at the end of commands within the batch file itself, though for every instance after the first one you need to use double greater than signs, i.e., >>, to append the output rather than wiping out what was in outfile.txt previously and creating a new version of that text file. E.g., you could have the following in test.bat:
#echo off
systeminfo >outfile.txt
ipconfig /all >>outfile.txt
You would then just use the following at a command prompt:
C:>test

Background processes in batch with redirected output

I'm trying to run several background processes from a batch file and have the output directed to a file. Is it possible to do this in Windows? This is what I've tried but it end up directing the output of the start program rather then background process.
start myapp.exe > myapp.out 2>&1
Actually it is quite easy without using a helper batch file. You just need to run the application via cmd.exe instead, and make sure to escape the special characters so they pass through to cmd.exe.
You probably don't want to see an extra console window, so use the START /B option.
start /b "" cmd /c myapp.exe ^>myapp.out 2^>^&1
Each STARTed process must have its output directed to a unique file. Multiple processes cannot share the same output file.
I think the only chance you have is to create one batch file for each exe that you want to start. Inside the batch file you can redirect the output. The master batch file would then "start" the batch file, not the exe directly.
You just need to include an exit command at the end of each batch file:
start_myapp.cmd contains the following:
myapp.exe > myapp.out 2>&1
exit
then you can run
start start_myapp.cmd
and the output will be redirected

Resources