The basics of batch processing: Where should I start? [closed] - windows

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I would like to get into batch file processing for Windows, but I have zero experience in this area. If you can point me in a general direction, it would be greatly appreciated.
Other potential questions:
What are the risks to be avoided with batch processing?
What is the basic structure of a batch file?
Are there examples of basic types of batch files, where I can put into action?
What are some basic types of batch commands?

Bach is a very methodical and ordered programming language that runs heavily on the Windows internal CMD or runline commands you're most likely familiar with. The main use of Batch from my personal experience is to use scripts to automate mundane tasks that need to be run every time something happens, for example at user startup.
I bet you could quite easily make a basic script now just buy learning what a) every bactch file should ideally have/need and b) by recognising that by default a batch file will read from top to bottom executing one command after the previous one finishes or fails.
In answer to your specific questions:
The risks to batch processing aren't really existent if you script something properly and test it every step of the way. - I would of said that anyone can easily see your code but third party tools will convert batch to .exe files without a problem.
I've listed some example codes with a few explanations at the bottom of this answer.
Again see the bottom of my answer for some basic scripts that will work on your computer (providing it's a version of Windows).
As I mentioned above any command you can type in to a CMD window will work within a batch file, so you can use command /? within a CMD window to check parameters and the way you should write your commands. For example type in to a CMD window ipconfig /?.
Basic Batch File Structure (Save as test.bat)
#echo off
title Test Batch Script
color a
echo.
ipconfig /all
echo.
echo Above is your IP information.
pause>nul
exit
#echo off - This turns off the CMD prompt until turned on again, without this every action is displayed, on the above script remove the line or turn it on to see the effects.
title - This is quite self explanitory, this command titles the CMD window to your choosing.
color xx - color xx changes the colour of the background and text, the first value is the text and the second is the window background. - Use color /? in a CMD window to find all the possible combinations and choose one for yourself, for example color 1f
echo. - This will enter a line break in to the file itself, I use this mainly for spacing text or adding a few lines under commands so my text can be seen clearly after commands have run.
echo - This will print the line of text you're trying to say, for example echo hello will display the text hello in the window.
pause>nul - This will pause the batch script from advancing to the next command in the series. - however just using the command pause will display the text "Press any key to continue..." using pause>nul will remove this message. Not essential but personal preference really.
exit - Exit will of course close your program, however not necessary when batch scripting as the script will close when no more commands are left to run, providing that no user input is required.
I feel that I've given you a basic and common batch file using all the most common commands that you'll use nearly every time you make a batch file.
The other answers have listed a lot of resources you can use to learn more. - Batch is an extremely easy language to learn but can get tricky depending on how much user input is required or how automated you want to make something.
Get to grips with my example and replace ipconfig /all with different/multiple commands to practice using them within your scripts.

In relation to the risks inolvd with batch programming, the worst that can happen is that when working with some files on your computer your batch script may malfunction, causing the files to be either erased or ruined. That is why, it is recommended to back-up all files before testing a batc script.
The basic structure of a batch file varies quite oftenly. When you start of you will mostly rely on goto loop structures. Where your script will start of with a series of commands (normally the first one being #echo off) and then making the script goto diffferent parts of your program.
Later on you will rely on for loops and batch calling.
To find some half decent batch app's I recommend you look online as there are some sights which can contain quite a few.
For basic tutorials, I recommend you start of by making simple batch apps to do very basic things like count.
Hope this helped, Mona.
P.S. Here are some I recently uploaded some of my old batch apps on instructables, here are the links:
Naughts and crosses
Calculator + Tutorial
Encryption with 7zip

go to http://www.dostips.com/
list of basic commands:
echo hello world ::will write hello world to the screen
echo %time% :: will write the value of the variable time to the screen
cd dir ::will go into the directory named dir
type file.txt ::will print the contents of a file to the screen
dir ::will echo the contents of the current directory to the screen
cd .. ::will goto the parent directory
help :: will show a list of commands
set ::will show all current variables
if "%var%" EQU "hello" (echo is) ::will echo is if the varible var is set to hello
set var=hello ::will set var equal to hello
set /p var=how are you: ::will get input from the user and stroe it in var
for /l %%i in (1,1,5) do echo %%i ::will echo 1 to five on the screen
ping host.com ::will check if you can connect to host.con and diplay some results.
copy a.txt dir\dir2\b.txt ::will copy a.txt to the second argument.
move a.txt ..\a.txt ::will move a.txt to the parent directory.
ren a.txt b.pdf ::will rename a.txt to b.pdf
command /? ::will display help on a command
here are some sample files I made:(blank lines matter)
file1:
setlocal enabledelayedexpansion
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
for /f "delims=" %%i in (a.txt) do set a=!a!!nl!%%i
echo %a%
file2:(each for is a single line)
#echo off&setlocal enabledelayedexpansion
if '%1'=='' (echo missing parameter&pause>nul&exit /b)
for /l %%i in (%1,-1,0) do for /l %%j in (%%i,1,%1) do set %%i=!%%i!
for /l %%i in (0,1,%1) do (for /l %%j in (0,1,%%i) do set nums=!nums! %%j)&echo !%%i!!nums!&set nums=
for /l %%i in (%1,-1,0) do (for /l %%j in (0,1,%%i) do set nums=!nums! %%j)&echo !%%i!!nums!&set nums=
file3:
setlocal enabledelayedexpansion
for /f "delims=" %%i in (a.txt) do set a=%%i&set a=!a:""=! &echo !a!>>new.txt
file4:(for is one line)
#echo off
for /d %%i in (*) do for /f %%j in ("%%i") do (dir "%%i" /b|for /f %%k in ('find "%%j" /v') do #dir "%%k" /b /s|find "thumbs.db" /v)
email me if you have questions. I know batch better than any other language and love it. my email is: 12nephi12#gmail.com
have fun!!! ;)

Related

Well-tried and tested Windows batch file stops at for loop since today - what could be the cause? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 18 days ago.
Improve this question
I have written and used a windows batch file that has been working without a problem for quite a while.
This is what it does:
The program loops through the *.pdf files of the current folder, and then performs a few tasks like extracting the text layer of the pdf file, searching for a couple of search terms and in the end it renames the pdf file according to which search term was found.
The batch file worked fine until today.
Now it just stops working at the for loop and I have no idea why.
Stripped down to the essence, the batch looks like this:
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b /a-d /o-d *.pdf') do (
echo %%i
echo %%~ti
echo %%~ni
)
The echos would just be examples, but as the batch files never reaches what's after the for loop, I simplified it.
Since today, the program would just stop when it reaches the for loop.
This is what I tried to solve the problem:
examined the syntax of the for loop, found no mistakes
tried simplified variations of the for /f loop including leaving out the delims option, changing it against a usebackq option, changing the name of the loop variable, stripping down the options of the dir command, changing the file filter to *.txt
turned the for loop into a one-liner with just an echo %%i command etc.
looped through the lines of a given text file
changed the location of the batch file to another folder
tried the batch on a Windows 10 and a Windows 7 system
try again after rebooting the system
checked folder access rights, I had all of them, no restrictions
None of these helped, the batch file would always stop and close its window right at the for loop.
I also inserted the dir command before the for loop just to check the syntax - this worked with no problem, so it can't be the syntax of the dir command either.
I remember that I had this problem a few times years ago and it either disappeared by itself or I had to retype the batch word for word in a new text file copying whatever was in the original file as copy and paste wouldn't work.
Has anyone out there observed this strange behaviour and maybe found the cause and at best a solution for it? I'd be so thrilled.
EDIT: When I run the batch in the cmd window, I get a simple SYNTAX ERROR when it reaches the line with the for loop.
Try this at the command prompt:
reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun
reg query "HKEY_LOCAL_MACHINE\\Software\Microsoft\Command Processor" /v AutoRun
Both results should be empty, but probably in your case they are set.
The problem of the AutoRun file, it's started when a new cmd instance is started, but also for the FOR/F command.
If this is the problem, you should delete these entries by
reg DELETE "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /f
reg DELETE "HKEY_LOCAL_MACHINE\\Software\Microsoft\Command Processor" /v AutoRun /f
Or if you really need an AutoRun batch, then it should start with a guard.
#echo off
setlocal EnableDelayedExpansion
REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the originial!!!
set "_ccl_=!cmdcmdline!"
REM *** The check is necessary to distinguish between a new cmd.exe instance for a user or for a "FOR /F" sub-command
if "!_ccl_:~1,-2!" == "!comspec!" (
REM ***** INTERACTIVE ****
REM *** Do your stuff ***
)
endlocal
exit /b

how to send each iteration of a loop in a batch script to a new cmd window and continue with loop

Disclaimer: I'm an engineer not a programmer, so while I do have technical knowledge, please bear with me if I am using the wrong terminology or asking the wrong questions.
I am trying to write a windows batch script that will allow me to submit multiple finite element simulations as a batch with all of the same settings. I currently have a script that works after a fashion, but it is not as efficient as I would like. My current script steps through the directories and runs the simulation in the command window before moving on to the next directory and repeating the loop. This script can be seen below:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
title BEST FRM Runs batch submission
echo This script will step through the run# directories and run the blast.k file in each run# folder. The .cmd file needs to be placed in the "FRMRuns" directory. You may need to change the reference to the LSDYNA solver if the version has changed.
SET /P ANSWER=Do you want to continue (y/n)?
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={n} (goto :no)
:yes
SET /P i=Enter input file name (e.g blast.k)
SET /P n=Enter number of CPUs (e.g. 2)
SET /P m=Enter memory (e.g 500m)
FOR /D %%D IN (run*) DO (
echo %%D
cd %%D
set lstc_license=network
set lstc_license_server=###.##.##.##
::the solver reference may need to be changed as we move on to new versions
c:\LSDYNA\program\ls971_s_R5.1.1_winx64_p.exe i=%i% ncpu=%n% memory=%m%
cd ..
)
exit /b
:no
exit /b
Our network licensing for LSDYNA allows for queuing of jobs, so ideally I would like to run through the entire loop and have the jobs run simultaneously rather than run one after another. I think this would be possible if I could send each iteration of the loop to a new command window and have it execute independently while the loop in batch script continues.
I'm not sure if I am searching for the wrong things, or if this is a unique request, but I have not been able to find anything that really helps with what I am trying to do. I have tried various things using start /b cmd /k, but I have not been able to pass the loop commands to the new window and have the loop continue in the original window. I have managed to get the new window to open, but not to actually execute any commands, and the code does not continue until the new window is closed.
I would appreciate any suggestions that you might have for how to accomplish my goal.
Thank you!
This starts each command in it's own process, with the start "" at the beginning.
start "" c:\LSDYNA\program\ls971_s_R5.1.1_winx64_p.exe i=%i% ncpu=%n% memory=%m%

pipe multiple files into a single batch file (using explorer highlight)

I can already get a batch file to run when a user right clicks on a file type. How can I make it so that only one instance runs per highlighted group and gets all the files as arguments.
Currently it runs single instance per file when a user "shift clicks"
there is most likely a better way to word this... you can see why I had trouble googling it.
thanks
Normally a file association multi-selection invocation will start several instances of a program and the program itself would have to deal with it on its own (Or with the help of DDE or IDropTarget)
It is going to be very hard to implement this in a batch file, this example should get you started:
#echo off
setlocal ENABLEEXTENSIONS
set guid=e786496d-1b2e-4a49-87b7-eb325c8cc64d
set id=%RANDOM%
FOR /F "tokens=1,2,3 delims=.,:/\ " %%A IN ("%TIME%") DO SET id=%id%%%A%%B%%C
set sizeprev=0
>>"%temp%\%guid%.lock" echo %id%
>>"%temp%\%guid%.list" echo %~1
:waitmore
>nul ping -n 3 localhost
FOR %%A IN (%temp%\%guid%.list) DO set sizenow=%%~zA
if not "%sizeprev%"=="%sizenow%" (
set sizeprev=%sizenow%
goto waitmore
)
FOR /F %%A IN (%temp%\%guid%.lock) DO (
if not "%%A"=="%id%" goto :EOF
FOR /F "tokens=*" %%B IN (%temp%\%guid%.list) DO (
echo.FILE=%%B
)
del "%temp%\%guid%.list"
del "%temp%\%guid%.lock"
pause
)
While this works, it is a horrible horrible hack and will fail badly if you don't wait for the first set of files to be parsed before starting a new operation on another set of files.
If you create a batch file and place it on your desktop, then you can select multiple files and drop them on that batch file. They will get passed as multiple parameters to the file.
For example, assume you put dropped.bat on your desktop, and it looks like this:
#echo off
echo %*
pause
Now assuming you had three files x, y and z, if you multiple-selected them and dropped them on dropped.bat, you'd see a command window come up with this text in it:
C:\Users\alavinio\Desktop\x C:\Users\alavinio\Desktop\y C:\Users\alavinio\Desktop\z
Press any key to continue . . .
That's the closest you can get. The right-click-and-Open semantics expect to start a new executable for each selected item, and typically those executables check for another instance of themselves, and if they see one, send the parameter over there to that existing process, and terminate themselves. You can actually watch that happen with Task Manager or Process Explorer.
Late to the party but here is my 2 cents. I had the same problem when trying to customise the behaviour of a 'Move to Dropbox Folder...' context menu command. I needed every file selected to be piped to a batch file to handle the processing in one instance.
After some digging I found Context Menu Launcher.
Simple enough to use. I popped singleinstance.exe in C:\Windows\system32 and created and ran a .reg file similar to below.
Windows Registry Editor Version 5.00
; Documents
[HKEY_CLASSES_ROOT\SystemFileAssociations\document\Shell\Dropbox]
#="Move to Dropbox Folder"
"Icon"="%SystemRoot%//system32//imageres.dll,-112"
"MultiSelectModel"="Player"
[HKEY_CLASSES_ROOT\SystemFileAssociations\document\Shell\Dropbox\command]
#="singleinstance.exe \"%1\" \"C:\\Move to Dropbox Folder.bat\" $files --si-timeout 400"
The way it works seems to have been imposed on you by the shell, and there doesn't seem to be an easy way to solve this.
Some application would add its own menu item that would allow the app to be invoked differently (i.e. just once for the group) from how it is done generally (repeatedly for every selected item), while another one would employ the API to check its own presence and would redirect the 'open' request to its already running copy.
Batch files aren't meant for either of these. You would probably need a different tool. Even if you would like the main job to be done by the batch file, you'd still need a way to call the batch file for processing of the item list.
I'm guessing you have a group of highlighted files and you want to run some program for each file.
#echo off
for %%A in (%*) do echo %%A
pause

Detecting how a batch file was executed

Assuming Windows, is there a way I can detect from within a batch file if it was launched from an open command prompt or by double-clicking? I'd like to add a pause to the end of the batch process if and only if it was double clicked, so that the window doesn't just disappear along with any useful output it may have produced.
Any clever ways to do this? I'm looking for solutions I could rely on to work on a machine that was configured more or less with default settings.
I just ran a quick test and noticed the following, which may help you:
When run from an open command prompt, the %0 variable does not have double quotes around the path. If the script resides in the current directory, the path isn't even given, just the batch file name.
When run from explorer, the %0 variable is always enclosed in double quotes and includes the full path to the batch file.
This script will not pause if run from the command console, but will if double-clicked in Explorer:
#echo off
setlocal enableextensions
set SCRIPT=%0
set DQUOTE="
#echo do something...
#echo %SCRIPT:~0,1% | findstr /l %DQUOTE% > NUL
if %ERRORLEVEL% EQU 0 set PAUSE_ON_CLOSE=1
:EXIT
if defined PAUSE_ON_CLOSE pause
EDIT:
There was also some weird behavior when running from Explorer that I can't explain. Originally, rather than
#echo %SCRIPT:~0,1% | findstr /l %DQUOTE% > NUL
if %ERRORLEVEL% EQU 0 set PAUSE_ON_CLOSE=1
I tried using just an if:
if %SCRIPT:0,1% == ^" set PAUSE_ON_CLOSE=1
This would work when running from an open command prompt, but when run from Explorer it would complain that the if statement wasn't correct.
Yes. Patrick Cuff's final example almost worked, but you need to add one extra escape, '^', to make it work in all cases. This works great for me:
set zero=%0
if [^%zero:~0,1%] == [^"] pause
However, if the name of the batch file contains a space, it'll be double quoted in either case, so this solution won't work.
Don't overlook the solution of having two batch files:
abatfile.bat and abatfile-with-pause.bat
The second simply calling the first and adding a pause
Here's what I use :
rem if double clicked it will pause
for /f "tokens=2" %%# in ("%cmdcmdline%") do if /i "%%#" equ "/c" pause
I use a parameter "automode" when I run my batch files from scripts.
set automode=%7
(Here automode is the seventh parameter given.)
Some code follows and when the file should pause, I do this:
if #%automode%==# pause
One easy way to do it is described here:
http://steve-jansen.github.io/guides/windows-batch-scripting/part-10-advanced-tricks.html
There is little typo in the code mentioned in the link. Here is correct code:
#ECHO OFF
SET interactive=0
ECHO %CMDCMDLINE% | FINDSTR /L /I %COMSPEC% >NUL 2>&1
IF %ERRORLEVEL%==0 SET interactive=1
ECHO do work
IF "%interactive%"==1 PAUSE
EXIT /B 0
Similar to a second batch file you could also pause if a certain parameter is not given (called via clicking).
This would mean only one batch file but having to specify a -nopause parameter or something like that when calling from the console.
crazy idea: use tasklist and parse it's results.
I've wrote in a test batch file:
tasklist > test.out
and when I double-clicked it, there was an additional "cmd.exe" process just before the tasklist process, that wasn't there when the script was run from command line (but note that might not be enough if someone opens a command line shell and then double-click the batch file)
Just add pause regardless of how it was opened? If it was opened from command prompt no harm done apart from a harmless pause. (Not a solution but just thinking whether a pause would be so harmful / annoying )

A Windows equivalent of the Unix tail command [closed]

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.

Resources