I'm LEARNING!
The only problem is, I can't seem to get the pipeline redirection operator working. May seem a dumb question.
As all of you know, the pipeline operator retrieves output of first command as input of other command. How can I do what the pipeline operator is supposed to do?
Full code (Don't worry, It's very tiny):
#echo off
title malhunt e-alpha
echo starting...
if exist dlls.txt del dlls.txt
echo done verifying.
echo grabbing dlls...
for /f %%a in ('tasklist /m') do echo %%a >>dlls.txt
type dlls.txt
echo dll grabbing done
pause
for /f %%b in (dlls.txt) do findstr wow.dll, dlls.txt
pause
Simple enough, the line is
findstr wow.dll, dlls.txt|echo *
so now, I can tell that the problem occurs on echo * How exactly do you make echo create output of findstr input?
NOTES
The comma on the end of wow.dll is intentional, because tasklist /m likes to leave commas before the dynamic link libraries. I know that probably there's no wow.dll on the tasklist, but for future reference, how can I create pipeline redirection?
Including the comma to search the dll will fail if the dll is the last one in the list of modules loaded into a process.
You can not pipe into echo command. Echo does not consume data from a pipe or a redirection.
You don't need to pipe into echo command, findstr /c:" wow.dll" dlls.txt will show the result on console
As title,
I need to process data in my program,
which those data are needed by an existed executable.
And I want to pass data through pipe instead of writing data as a file.
My platform is Windows 7.
Any one can help me?
Thanks!
You can't use a pipe for passing output as an argument to another program. Pipes connect the STDOUT of one process with STDIN of another process.
If you want the output of the first process to be used as an argument for the second process you could do something like this:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=*" %%a in ('p1.exe') do set output=!output! %%a
p2.exe "%output%" /foo /bar ...
endlocal
I've been having some troubles redirecting a batch file to a log file as well as having it display in the command console.
Is this even possible with windows batch, or do I have to resort to a simple program which intercepts stdout and writes the stream to a file and to stdout?
I don't think you can do this (properly) with just the built-in tools, you probably need to use a tee utility like the Win32 GNU ports (this or this) or mtee
Edit:
You can of course use the FOR batch command, but the output is not live, you have to wait for the command to finish:
#echo off
setlocal ENABLEEXTENSIONS
goto main
:TEE
FOR /F "tokens=*" %%A IN ('%~2') DO (
>>"%~1" echo.%%A
echo.%%A
)
goto :EOF
:main
call :TEE "%temp%\log.txt" "ping -n 2 localhost"
When working with Bash, I can put the output of one command into another command like so:
my_command `echo Test`
would be the same thing as
my_command Test
(Obviously, this is just a non-practical example.)
I'm just wondering if you can do the same thing in Batch.
You can get a similar functionality using cmd.exe scripts with the for /f command:
for /f "usebackq tokens=*" %%a in (`echo Test`) do my_command %%a
Yeah, it's kinda non-obvious (to say the least), but it's what's there.
See for /? for the gory details.
Sidenote: I thought that to use "echo" inside the backticks in a "for /f" command would need to be done using "cmd.exe /c echo Test" since echo is an internal command to cmd.exe, but it works in the more natural way. Windows batch scripts always surprise me somehow (but not usually in a good way).
You can do it by redirecting the output to a file first. For example:
echo zz > bla.txt
set /p VV=<bla.txt
echo %VV%
Read the documentation for the "for" command: for /?
Sadly I'm not logged in to Windows to check it myself, but I think something like this can approximate what you want:
for /F %i in ('echo Test') do my_command %i
Maybe I'm screwing up the syntax of the standard for /f method, but when I put a very complex command involving && and | within the backticks in the limit of the for /f, it causes problems. A slight modification from the usual is possible to handle an arbitrary complexity command:
SET VV=some_command -many -arguments && another_command -requiring -the-other -command | handling_of_output | more_handling
for /f "usebackq tokens=*" %%a in (`%VV%`) do mycommand %%a
By putting your full and complex command in a variable first, then putting a reference to the variable in the limit rather than putting the complex command directly into the limit of the for loop, you can avoid syntax interpretation issues. Currently if I copy the exact command I have set to the VV variable in the example above into where it's used, %VV%, it causes syntax errors.
You could always run Bash inside Windows. I do it all the time with MSYS (much more efficient than Cygwin).
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm looking for the equivalent of the Unix 'tail' command that will allow me to watch the output of a log file while it is being written to.
If you use PowerShell then this works:
Get-Content filenamehere -Wait -Tail 30
Posting Stefan's comment from below, so people don't miss it
PowerShell 3 introduces a -Tail parameter to include only the last x lines
I'd suggest installing something like GNU Utilities for Win32. It has most favourites, including tail.
I've always used Baretail for tailing in Windows. It's free and pretty nice.
You can get tail as part of Cygwin.
Anybody interested in a DOS CMD tail using batch commands (see below).
It's not prefect, and lines sometime repeat.
Usage: tail.bat -d
tail.bat -f -f
#echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
rem tail.bat -d <lines> <file>
rem tail.bat -f <file>
rem ****** MAIN ******
IF "%1"=="-d" GOTO displayfile
IF "%1"=="-f" GOTO followfile
GOTO end
rem ************
rem Show Last n lines of file
rem ************
:displayfile
SET skiplines=%2
SET sourcefile=%3
rem *** Get the current line count of file ***
FOR /F "usebackq tokens=3,3 delims= " %%l IN (`find /c /v "" %sourcefile%`) DO (call SET find_lc=%%l)
rem *** Calculate the lines to skip
SET /A skiplines=%find_lc%-!skiplines!
rem *** Display to screen line needed
more +%skiplines% %sourcefile%
GOTO end
rem ************
rem Show Last n lines of file & follow output
rem ************
:followfile
SET skiplines=0
SET findend_lc=0
SET sourcefile=%2
:followloop
rem *** Get the current line count of file ***
FOR /F "usebackq tokens=3,3 delims= " %%l IN (`find /c /v "" %sourcefile%`) DO (call SET find_lc=%%l)
FOR /F "usebackq tokens=3,3 delims= " %%l IN (`find /c /v "" %sourcefile%`) DO (call SET findend_lc=%%l)
rem *** Calculate the lines to skip
SET /A skiplines=%findend_lc%-%find_lc%
SET /A skiplines=%find_lc%-%skiplines%
rem *** Display to screen line when file updated
more +%skiplines% %sourcefile%
goto followloop
:end
There are quite a number of options, however all of them have flaws with more advanced features.
GnuWin32 tail is buggy (α β γ) - things like -f just plain don't work.
UnxUtils tail seems better (-f works, but --pid seems not to, -n but not --lines=n fails with -f), but appears to be a dead project.
Cygwin is a big ugly mush, could perhaps just use the DLL and coreutils package - but still has problems like --pid not working with native win32 processes.
If you do not want to install anything at all you can "build your own" batch file that does the job from standard Windows commands. Here are some pointers as to how to do it.
1) Using find /c /v "" yourinput.file, get the number of lines in your input file. The output is something like:
---------- T.TXT: 15
2) Using for /f, parse this output to get the number 15.
3) Using set /a, calculate the number of head lines that needs to be skipped
4) Using for /f "skip=n" skip the head lines and echo/process the tail lines.
If I find the time, I will build such a batch file and post it back here.
EDIT: tail.bat
REM tail.bat
REM
REM Usage: tail.bat <file> <number-of-lines>
REM
REM Examples: tail.bat myfile.txt 10
REM tail.bat "C:\My File\With\Spaces.txt" 10
#ECHO OFF
for /f "tokens=2-3 delims=:" %%f in ('find /c /v "" %1') do (
for %%F in (%%f %%g) do set nbLines=%%F )
set /a nbSkippedLines=%nbLines%-%2
for /f "usebackq skip=%nbSkippedLines% delims=" %%d in (%1) do echo %%d
I've used Tail For Windows. Certainly not as elegant as using tail but then, you're using Windows. ;)
With Windows PowerShell you can use:
Get-Content <file> -Wait
I haven't seen Log Expert anywhere among answers here.
It's customizable and is quite good for going around log files. So far it's the best Windows graphical log viewer for me.
Unfortunately, this software is no longer available. You can read about it on archive.org.
I've used Mtail recently and it seems to work well. This is the GUI type like baretail mentioned above.
Download the tail command, part of Windows Server 2003 Resource Kit Tools from Microsoft itself.
Try Windows Services for UNIX. Provides shells, awk, sed, etc. as well as tail.
Update -: Unfortunately, as of 2019 this system is no longer available on the Microsoft Download Center.
I prefer TailMe because of the possibility to watch several log files simultaneously in one window: http://www.dschensky.de/Software/Staff/tailme_en.htm
DOS has no tail command; you can download a Windows binary for GNU tail and other GNU tools here.
Another option would be to install MSYS (which is more leightweight than Cygwin).
DOS's type works like *nux's cat, though just like cat, it does dump the whole file, so it's not really a true tail, but it's going to be available in a pinch without downloading/installing a true tail substitute.
I just wrote this little batch script. It isn't as sophisticated as the Unix "tail", but hopefully someone can add on to it to improve it, like limiting the output to the last 10 lines of the file, etc. If you do improve this script, please send it to me at robbing ~[at]~ gmail.com.
#echo off
:: This is a batch script I wrote to mimic the 'tail' UNIX command.
:: It is far from perfect, but I am posting it in the hopes that it will
:: be improved by other people. This was designed to work on Windows 7.
:: I have not tested it on any other versions of Windows
if "%1" == "" goto noarg
if "%1" == "/?" goto help
if "%1" == "-?" goto help
if NOT EXIST %1 goto notfound
set taildelay=%2
if "%taildelay%"=="" set taildelay=1
:loop
cls
type %1
:: I use the CHOICE command to create a delay in batch.
CHOICE /C YN /D Y /N /T %taildelay%
goto loop
:: Error handlers
:noarg
echo No arguments given. Try /? for help.
goto die
:notfound
echo The file '%1' could not be found.
goto die
:: Help text
:help
echo TAIL filename [seconds]
:: I use the call more pipe as a way to insert blank lines since echo. doesnt
:: seem to work on Windows 7
call | more
echo Description:
echo This is a Windows version of the UNIX 'tail' command.
echo Written completely from scratch by Andrey G.
call | more
echo Parameters:
echo filename The name of the file to display
call | more
echo [seconds] The number of seconds to delay before reloading the
echo file and displaying it again. Default is set to 1
call | more
echo ú /? Displays this help message
call | more
echo NOTE:
echo To exit while TAIL is running, press CTRL+C.
call | more
echo Example:
echo TAIL foo 5
call | more
echo Will display the contents of the file 'foo',
echo refreshing every 5 seconds.
call | more
:: This is the end
:die
The tail command and many others are available in the Windows Resource Kit Tools package.
If you want to use Win32 ports of some Unix utilities (rather than installing Cygwin), I recommend GNU utilities for Win32.
Lighter weight than Cygwin and more portable.
Install MKS Toolkit... So that you can run all Unix commands on Windows.
The command is:
tail -f <file-name>
In Far Manager, press F3 on a file to enter the standard viewer, then the End key to navigate to the end of file.
If the file is updated, Far Manager will scroll it automatically.
Graphical log viewers, while they might be very good for viewing log files, don't meet the need for a command line utility that can be incorporated into scripts (or batch files). Often such a simple and general-purpose command can be used as part of a specialized solution for a particular environment. Graphical methods don't lend themselves readily to such use.
You can try WinTail as well.
ََ
I think I have found a utility that meets the need for the tail function in batch files. It's called "mtee", and it's free. I've incorporated it into a batch file I'm working on and it does the job very nicely. Just make sure to put the executable into a directory in the PATH statement, and away you go.
Here's the link:
mtee
I'm using Kiwi Log Viewer. It's free.