Batch Script to overwrite a file with first 5 lines - batch-file

I have a file with x lines and I would like to overwrite that file with only first 5 lines deleting the rest. I need to do this with a batch script. Any help on this would be greatly appreciated.
Thanks
Kumar

As long as each of first 5 lines is <= 1021 bytes long, and there are no control characters at the end of any of the 5 lines:
#echo off
setlocal enableDelayedExpansion
<"test.txt" >"test.txt.new" (
for /l %%N in (1 1 5) do (
set "ln="
set /p "ln="
echo(!ln!
)
)
move /y "test.txt.new" "test.txt" >nul
Or you could use my JREPL.BAT utility - a hybrid JScript/batch script that runs on any Windows machine from XP onward. This solution works well with any size file as long as no line exceeds 2 gigabytes - effectively no limitation.
JREPL.BAT is really designed to do regular expression search and replace on text files, but it is convenient to use it to get the head or tail of a file.
jrepl "^" "" /jbegln "quit=(ln>=5)" /f test.txt /o -

#echo off
set "file_to_process=E:\somefile.txt"
set "first_n_lines=5"
break>"%temp%\empty"&&fc "%temp%\empty" "%file_to_process%" /lb %first_n_lines% /t |more +4 | findstr /B /E /V "*****" >5.txt
rem move /y 5.txt "%file_to_process%"
remove the rem on the last line if 5.txt file is what you want.

Related

Batch unzip files in child folders, then copy specific files to network folder based on number in file name

I am a serious newbie at creating batch files and am hoping someone can help me. One of our staff receives zipped pdf docs by email, which she copies to a folder on her desktop. Within that folder, I would like for her to run a batch script that will
A. Unzip the zipped contents into a network directory, i.e. \server\contracts
Under this directory, the process will create folders for each group of contracts, i.e. \server\contracts\Masterson (The name of this will be same as zipped file name).
B. Then the batch process should copy a select few of the pdf documents into a network directory based on the filename. Each file contains a number, which will go in the following manner: Masterson + 1.pdf >> \server\contracts\Item1 and \server\contracts\Item2, etc. Masterson + 1.pdf will go into \server\contracts\Item1 without a folder name, as will Paisley + 1 certificate.pdf and Johnsonville + 1 document.pdf.
The problem is that the companies do not follow instructions and the number can be at the beginning, middle, or end of the file name. Also, unfortunately, there are spaces in the name of the zipped file and the pdf documents. Currently, we are only copying 4 filenames into separate directories for other people to review and validate.
Below is what I did so far looking around this site:
#Echo off
SETLOCAL
for /R "delims=\\server\contracts\RECEIVED 2017-18 APPLICATION" %%I in
("*.zip") do (
"%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI"
)
rem setlocal enableextensions disabledelayedexpansion
CLS
::The Input Folder
set "InputFolder=C:\Users\eartha.kitt\Desktop\Test"
::The Output Folders
set "Output1=\\server\contracts\ITEM 1 17-18 CERTS"
set "Output6=\\server\contracts\ITEM 6 SIGNATURES"
set "Output8A=\\server\contracts\ITEM 8A 17-18 CALENDARS"
set "Output8B=\\server\contracts\ITEM 8B 16-17 REVISED CALENDARS"
set "Output8a=\\server\contracts\ITEM 8A 17-18 CALENDARS"
set "Output8b=\\server\contracts\ITEM 8B 16-17 REVISED CALENDARS"
::The extensions to wait
set "extensions=*.pdf"
setlocal EnableDelayedExpansion
:Loop
cls
echo Waiting for file ...
for /f "usebackq delims=|" %%a in ('dir /b /s %InputFolder%\%extensions%
2^>nul') do (
rem for /r %%a in in (%InputFolder%\%extensions% 2^>nul') do (
set "Fichier=%%a"
echo Treating _^> %%a
if "!Fichier:~0,-2!"==" 1" COPY "%%~a" %Output1%
if "!Fichier:~0,-2!"==" 6" COPY "%InputFolder%\~%%a" %Output6%
if "!Fichier:~0,-3!"=="8A" COPY "%InputFolder%\%%a" %Output8A%
if "!Fichier:~0,-3!"=="8B" COPY "%InputFolder%\%%a" %Output8B%
if "!Fichier:~0,-3!"=="8a" COPY "%InputFolder%\%%a" %Output8a%
if "!Fichier:~0,-3!"=="8b" COPY "%InputFolder%\%%a" %Output8b%
::Waiting ~5 seconds
ping localhost -n 6 >nul
)
::Return to the loop
goto:Loop
Of course this doesn't work. Please help!
Well - bravo for the attempt! And so close...
Let's take the first part
for /R "delims=\\server\contracts\RECEIVED 2017-18 APPLICATION" %%I in ("*.zip") do (
"%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI"
)
What's wrong here is that the delims clause is only usable in for /f. for /r always delivers the entire filename to the metavariable %%I.
On my system I use %server%\u for testing - u is a shared resource on the server assigned to U:\ on server.
for /R "\\%server%\u\contracts\RECEIVED 2017-18 APPLICATION" %%I IN ("*.zip") do (
"%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI"
worked happily for me - delivering the extracted files to "u:\contracts\RECEIVED 2017-18 APPLICATION"
The second part of your code is examining "C:\Users\eartha.kitt\Desktop\Test" not "\%server%\u\contracts\RECEIVED 2017-18 APPLICATION" - very sensibly assigned to a variablename for easy adjustment.
Here's my modified code:
SET "terminatefilename=stop.txt"
DEL "%terminatefilename%" 2>nul
rem setlocal enableextensions disabledelayedexpansion
CLS
::The Input Folder
set "InputFolder=C:\Users\eartha.kitt\Desktop\Test"
set "InputFolder=\\%server%\u\contracts\RECEIVED 2017-18 APPLICATION"
::The Output Folders
set "Output1=\\%server%\u\contracts\ITEM 1 17-18 CERTS"
set "Output6=\\%server%\u\contracts\ITEM 6 SIGNATURES"
set "Output8A=\\%server%\u\contracts\ITEM 8A 17-18 CALENDARS"
set "Output8B=\\%server%\u\contracts\ITEM 8B 16-17 REVISED CALENDARS"
FOR /f "tokens=1*delims==" %%b IN ('set output') DO MD "%%c" 2>nul
::The extensions to wait
set "extensions=*.pdf"
setlocal EnableDelayedExpansion
:Loop
cls
echo Waiting for file ...
for /f "delims=|" %%a in ('dir /b /s "%InputFolder%\%extensions%" 2^>nul') do (
rem for /r %%a in in (%InputFolder%\%extensions% 2^>nul') do (
SET "copied="
echo Treating _^> %%a
REM OPTION 1 - Key string must be at end-of name part
set "Fichier=%%~Na"
if /i "!Fichier:~0,-2!"==" 1" COPY "%%a" "%Output1%"&SET "copied=Y"
if /i "!Fichier:~0,-2!"==" 6" COPY "%%a" "%Output6%"&SET "copied=Y"
if /i "!Fichier:~0,-3!"==" 8A" COPY "%%a" "%Output8A%"&SET "copied=Y"
if /i "!Fichier:~0,-3!"==" 8B" COPY "%%a" "%Output8B%"&SET "copied=Y"
REM OPTION 2 - Key string may be anywhere in filename
IF NOT DEFINED copied (
echo "%%~na"|FINDSTR /i /L /C:" 8B" >NUL
IF NOT ERRORLEVEL 1 COPY "%%a" "%Output8B%"&SET "copied=Y"
)
IF NOT DEFINED copied (
echo "%%~na"|FINDSTR /i /L /C:" 8A" >NUL
IF NOT ERRORLEVEL 1 COPY "%%a" "%Output8A%"&SET "copied=Y"
)
IF NOT DEFINED copied (
echo "%%~na"|FINDSTR /i /L /C:" 6" >NUL
IF NOT ERRORLEVEL 1 COPY "%%a" "%Output6%"&SET "copied=Y"
)
IF NOT DEFINED copied (
echo "%%~na"|FINDSTR /i /L /C:" 1" >NUL
IF NOT ERRORLEVEL 1 COPY "%%a" "%Output1%"&SET "copied=Y"
)
)
::Waiting ~5 seconds
timeout 6 >NUL
:: Test for exit
IF EXIST "%terminatefilename%" DEL "%terminatefilename%"&GOTO :EOF
::Return to the loop
goto:Loop
First, I set up terminatefilename so that creating this file will terminate the batch (it's an infinite loop by design in your code)
Next, I overrode your inputfolder name to suit my system.
Then the output directories. I adjusted their names to suit my system. Note that batch is largely case-insensitive, so setting Output8A and Output8a is setting the same variable. The only time that batch commands are case-sensitive is the metavariable (loop-control variable) in a for statement.
Then I inserted a line to create the destination directories. This uses a set command to list the variables starting output in the format Output1=\\%server%\u\contracts\ITEM 1 17-18 CERTS (where server will have been resolved). The command reads the output of the set command, uses = as the delimiter and assigns output1 to %%b and \\%server%\u\contracts\ITEM 1 17-18 CERTS to %%c. We want to make sure the directory %%c exists, so we make it with an md command and ignore complaints that it already exists with 2>nul.
Next the for/f. for /f reads each line of (thisfilename) or ("this literal value") or ('the output of this command') but when you need to read a from a file whose name must be double-quoted beacuse it contains spaces, then the syntax is for /f "usebackq"... (idontknow) or ("this filename containing spaces") or ('ive no idea whatever') or (`the output of this command`)
So there's no need to use usebackq -- in fact, it's counterproductive.
The delims=| is optional and could be replaced by delims= as the output of a dir command will never contain | (illegal in a file or directoryname). You do need the delims clause however, as the default delimiters include Space and the default tokens is 1 so only the first string of each line output up to the first space will be assigned to %%a.
Hmm - you've remmed-out a for/r. Sadly, the target directory in the for /r can't be a metavariable.
Next, I've cleared a copied flag saying "so far, this file has not been copied"
Next, set fichier to the name-part only of the filename. Since you are using .pdf as a filter, each name output by the for/f will be a full-filename, ending in .pdf
Next, almost-correct with the battery of if statements. The /i makes the comparison case-insensitive so that it will cope with both 8a and 8A. The strings on both sides of the == must be identical to pass the == test, so you need a 3-character string in the 8 tests.
You've evidently been experimenting with the copy command and trying to feed it with appropriate strings. %%~a strips %%a of any enclosing quotes. %%a won't have enclosing quotes - just the fullfilename, so this does nothing in this instance. %InputFolder%\~%%a concatenates the values from InputFolder,"\~" and %%a - which means "%inputfolder%\~%inputfolder%\filenameandextension of %%a". The last two would be resolved to the same, bar the ~.
Since the entire filename is contained in %%a, all that's needed for the sourcefile is "%%a" - quoted as it will probably contain spaces.
Well - the destination directory (we've already established it with the for /f...%%b) can also contain spaces, so it needs to be quoted, too.
I've then set the flag copied to Y if the line took effect. Actually, it can be set to any non-empty value as the whole object is to interpret whether or not it exists with an if defined statement to bypass any later attempts to copy the same file (remember - copied is cleared for each file)
Now - the second option. This is actually a more powerful detector of the required target strings than is the substring version, and will detect the target string anywhere in the filename.
If you echo the name-part only of the file %%~na into a findstr, then set to findstr to look for the /L literal string /i case-insensitive c:"some literal string" and output ny matches found to nowhere (>nul) then findstr will set errorlevel to 0 if found and 1 otherwise. (the /L is superfluous here, I use it habitually to remind me I'm working with literal strings, not regular expressions)
If errorlevel n will be true if errorlevel is currently n or greater than n, so if the string is found, we do the copy and set the copied flag as before.
I reversed the order of tests because it was easier for me to do using the editor I use.
Now - there's a downside to this approach. It's a double-edged sword. Since the target string is detected wherever it appears in the filename, whatever whatever 10 something something.pdf will pass the test for 1 because Space1 appears in its name.
When the loop has finished, use timeout to wait, redirecting the output to nul to make it pipe down. Note that your delay was inside the for loop - so it would have waited 6 seconds after processing each file, not 6 seconds after processing an entire batch.
Finally, if you create terminatefilename from anothe cmd instance, the batch will exit cleanly and kill terminatefilename for you. Much cleaner than control-c.
A last note: Since you are copying the file, not MOVEing it, it will still exist in the same place presumably after the 6 seconds has elapsed and will be endlessly copied and recopied. You'd probably need to make adjustments to achieve the desired result.

Editing text file to add "-" 8 characters in

I deal with a large text files daily, I need to add a single "-" to each line 8 characters in for instance :
ABCDEF DC01 B738
ABCDEF B787
would become
ABCDEF -DC01 B738
ABCDEF - B787
How easy is this to do with a batch file?
regards
David
#echo off
setlocal enabledelayedexpansion
(for /f "delims=" %%a in (input.txt) do (
set line=%%a
set line=!line:~0,7!-!line:~7!
echo !line!
))>output.txt
Note: this will remove empty lines and has problems with exclamation marks. (may or may not be a problem; depends on your file content)
It also will eliminate lines that begin with ; due to default EOL character.
Also, lines are limited to ~8190 bytes max length (Thanks dbenham)
Here is a "robust" pure batch solution that preserves ! and empty lines. But it is still limited to ~8190 max line length, and it is painfully slow for large files. This will not modify lines that have less than 7 characters.
#echo off
setlocal disableDelayedExpansion
set "file=file.txt"
>"%file%.new" (
for /f "delims=" %%A in ('findstr /n "^"` "file.txt"') do (
set "s=%%A"
setlocal enableDelayedExpansion
set "s=!s:*:=!"
if not defined s (
echo(
) else if "!s:~7!" equ "" (
echo(!s!
else (
echo(!s:~0,7!-!s:~7!
)
endlocal
)
)
move /y "%file%.new" "%file%" >nul
And here is a truly robust and fast solution that uses JREPL.BAT - a regular text processing utility that is pure script (batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party executable required. This solution also does not modify lines that have fewer than 7 characters.
From the command line:
jrepl "^.{7}" "$&-" /f file.txt /o -
From within a batch script
call jrepl "^.{7}" "$&-" /f file.txt /o -
This is a simple two-lines Batch-JScript hybrid script solution that have all the advantages of JScript language: is fast, robust, manage all special characters and preserve empty lines. Besides, it is simple enough to be understood and then modified for other similar tasks after read the documentation. Copy it to a file with .BAT extension.
#set #a=0 // & cscript //nologo //E:JScript "%~F0" < input.txt > output.txt & goto :EOF
WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/^.{7}/gm,"$&-"));

Batch For loop.. wildcards to determine length of searched files?

I'm stuck on a piece of code that I mainly didn't write myself. I'm looking for a code that runs through all files with extension .dwg, and start with K_E , and have length 9. instinctively I used wildcards, but that doesn't work..
K_E??????.dwg would do the trick in my head..but doesn't.
the reason I need this length, is that in deeper folders there are
K_E??????xx.dwg and other files.
The other files in subfolders can have a range of other names, the general idea is that I only want files that are exactly named K_E[insert 6numbershere].dwg Either that, or the limit of folders depth = 2. I also tried the wildcard in the folder names to allow the code to look in only 2 deep folders for K_E*.dwg files, but that also didn't work. something like C:\Users\b00m49\Desktop\LSPTEST**\K_E*.dwg could also work..
the code is supposed to open the drawings, apply a script, and move on to the next file.
this is what I'm working with so far.
for /r "C:\Users\b00m49\Desktop\LSPTEST" %%a in (K_E*.dwg) do ( start /wait "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")
So you just want to exclude files containing backup in their names? This should work:
#echo off
setlocal enabledelayedexpansion
for /r "C:\Users\b00m49\Desktop\LSPTEST" %%a in (K_E*.dwg) do (
set filename=%%a
if "!filename:backup=!"=="!filename!" (
rem start /wait "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr"
)
)
pause
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\b00m49\Desktop\LSPTEST"
SET "sourcedir=U:\sourcedir"
for /r "%sourcedir%" %%a in (K_E*.dwg) do (
FOR /f "tokens=1,6delims=\" %%m IN ("%%a") DO (
REM %%n contain dirname or filename or empty
REM %%n is only empty for 0, 1 or 2 levels down.
IF "%%n"=="" (
SET "name=%%~na"
IF "!name:~9!"=="" IF "!name:~8!" neq "" (
REM the name is not longer than 9 characters, but IS longer than 8
ECHO(start /wait "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")
)
)
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances. I used a test directory.
The required START commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(START to START to actually start the processes.
I'm not sure what you mean by "two levels down" - I've assumed the target directory +two levels. Since your actual dirctory has 3 more levels than my test directory, you'd probably need to change 1,6 to 1,9
Simply use the tokenising feature to ensure that there are no more than ? levels of directory, where the drivename and filename count as levels. Ignore the file if there are.
Then get the name - we know the first 3 characters are K_E but we want exactly 6 characters after, so see whether there are characters 10+ and if there are not, make sure character 9 exists (characters count from 0) and if both of these conditions are valid then it must be K_E*6characters*
Could ensure that all 6 characters are numeric if required, but not actually specified.
BTW - to add information, please edit it into your question so people don't need to chase all over the thread.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\Users\b00m49\Desktop\LSPTEST"
for /r "%sourcedir%" %%a in (K_E*.dwg) do (
FOR /f "tokens=1,9delims=\" %%m IN ("%%a") DO (
REM %%n contain dirname or filename or empty
REM %%n is only empty for 0, 1 or 2 levels down.
IF "%%n"=="" (
SET "name=%%~na"
IF "!name:~9!"=="" IF "!name:~8!" neq "" (
REM the name is not longer than 9 characters, but IS longer than 8
ECHO(start /wait "" "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")
)
)
)
)
pause
GOTO :EOF
This worked for me.
Notes:
The pause line should stop the procedure after it displays the command to be executed.
You would need to change U: to C: in the setting of sourcedir to have it process your c: drive.
You would need to change ECHO(start to start in order to actually execute the autocad processing. This technique (echoing the command) is used to show the command in order to have it verified. Note I've also deliberately inserted a pair of rabbit's-ears ("") because the first quoted argument to start is taken as the window-title.

merge lines from several files

I have 3 text files like this
file1.txt
AAA1
AAA2
AAA3
...
...
file2.txt
BBB1
BBB2
BBB3
..
..
file3.txt
CCC1
CCC2
CCC3
..
..
I want to have output.txt like this
AAA1:BBB1:CCC1
AAA2:BBB2:CCC2
..
...
now I can do this by making a loop reading a line from each file but I want to do this using sed , grep or any similar tools but I dont know how.
thanks
Use paste:
paste -d ':' file1.txt file2.txt file3.txt
paste is well suited to this task. Try:
paste -d ":" file1.txt file2.txt file3.txt > output.txt
The paste answer sounds like a perfect solution.
But someone (not the OP) might want a native solution that does not require downloading an executable. Something like this really should be done with a more powerful scripting language like VBScript or JScrpt.
But, since I like the challenge of solving things with batch, I thought I'd take a stab at a native batch solution, just for fun.
The OP said "now I can do this by making a loop reading a line from each file". That is easier said than done using batch!
Normally files are read using FOR /F, but there is no good way to interleave reads from multiple files using FOR /F. The only other native alternative is to use SET /P with redirected input. This technique imposes the following limitations:
The input files must use Windows style line terminators: <carriage return><line feed>
No input line can exceed 1021 bytes in length (disregarding the line terminators)
Trailing control characters are stripped from each input line
In addition, each final merged line must not exceed the batch variable length limit of ~8k bytes.
One last restriction - the script can only handle up to 7 input files. The script will fail if more than 7 files are specified - I did not include any error checking.
So here is a working batch script - call it "merge.bat". Note - this batch solution is MUCH slower than other solutions like paste or scripts written in VBScript or JScript. But it does work :-)
#echo off
setlocal disableDelayedExpansion
::Initialization
set "inputRedirection="
set "files=2"
set "lines=0"
for %%F in (%*) do call :setInputRedirection
setlocal enableDelayedExpansion
set "inputRedirection=!inputRedirection:::=<!"
::Merge the files
%inputRedirection% (
for /l %%N in (1 1 %lines%) do (
set "ln="
for /l %%I in (3 1 %files%) do (
call :readLine %%I
if %%I neq %files% (
set "ln=!ln!!input!:"
) else (
echo(!ln!!input!
)
)
)
)
exit /b
:setInputRedirection
set /a "files+=1"
for %%A in (1) do (
set inputRedirection=%files%::"%%~F" %inputRedirection%
for /f %%N in ('find /c /v "" ^<"%%~F"') do if %%N gtr %lines% set "lines=%%N"
)
exit /b
:readLine fileHandle
set "input="
<&%1 set /p "input="
exit /b
To merge your files you would do:
merge.bat file1.txt file2.txt file3.txt >output.txt

CMD.EXE batch script to display last 10 lines from a txt file

Any ideas how to echo or type the last 10 lines of a txt file?
I'm running a server change log script to prompt admins to state what they're doing, so we can track changes. I'm trying to get the script to show the last 10 entries or so to give an idea of what's been happening recently. I've found a script that deals with the last line, as shown below, but can't figure out what to change in it to display the last 10 lines.
Script:
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (c:\log09.txt) do (
set var=%%a
)
echo !var!
Example of log file:
06/02/2009, 12:22,Remote=Workstation-9,Local=,
mdb,bouncing box after updates,CAS-08754,
=================
07/02/2009, 2:38,Remote=,Local=SERVER1,
mdb,just finished ghosting c drive,CAS-08776,
=================
07/02/2009, 3:09,Remote=,Local=SERVER1,
mdb,audit of server,CAS-08776,
Any thoughts?
The script works great, just need it to pipe more lines to the screen.
Hopefully this will save Joel's eyes :)
#echo OFF
:: Get the number of lines in the file
set LINES=0
for /f "delims==" %%I in (data.txt) do (
set /a LINES=LINES+1
)
:: Print the last 10 lines (suggestion to use more courtsey of dmityugov)
set /a LINES=LINES-10
more +%LINES% < data.txt
This answer combines the best features of already existing answers, and adds a few twists.
The solution is a simple batch implementation of the tail command.
The first argument is the file name (possibly with path information - be sure to enclose in quotes if any portion of path contains spaces or other problematic characters).
The second argument is the number of lines to print.
Finally any of the standard MORE options can be appended: /E /C /P /S /Tn. (See MORE /? for more information).
Additionally the /N (no pause) option can be specified to cause the output to be printed continuosly without pausing.
The solution first uses FIND to quickly count the number of lines. The file is passed in via redirected input instead of using a filename argument in order to eliminate the printout of the filename in the FIND output.
The number of lines to skip is computed with SET /A, but then it resets the number to 0 if it is less than 0.
Finally uses MORE to print out the desired lines after skipping the unwanted lines. MORE will pause after each screen's worth of lines unless the output is redirected to a file or piped to another command. The /N option avoids the pauses by piping the MORE output to FINDSTR with a regex that matches all lines. It is important to use FINDSTR instead of FIND because FIND can truncate long lines.
:: tail.bat File Num [/N|/E|/C|/P|/S|/Tn]...
::
:: Prints the last Num lines of text file File.
::
:: The output will pause after filling the screen unless the /N option
:: is specified
::
:: The standard MORE options /E /C /P /S /Tn can be specified.
:: See MORE /? for more information
::
#echo OFF
setlocal
set file=%1
set "cnt=%~2"
shift /1
shift /1
set "options="
set "noPause="
:parseOptions
if "%~1" neq "" (
if /i "%~1" equ "/N" (set noPause=^| findstr "^") else set options=%options% %~1
shift /1
goto :parseOptions
)
for /f %%N in ('find /c /v "" ^<%file%') do set skip=%%N
set /a "skip-=%cnt%"
if %skip% lss 0 set skip=0
more +%skip% %options% %file% %noPause%
You should probably just find a good implementation of tail. But if you really really insist on using CMD batch files and want to run on any NT machine unmolested, this will work:
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (c:\tmp\foo.txt) do (
set var9=!var8!
set var8=!var7!
set var7=!var6!
set var6=!var5!
set var5=!var4!
set var4=!var3!
set var3=!var2!
set var2=!var1!
set var1=!var!
set var=%%a
)
echo !var9!
echo !var8!
echo !var7!
echo !var6!
echo !var5!
echo !var4!
echo !var3!
echo !var2!
echo !var1!
echo !var!
There are several windows implementations of the tail command. It should be exactly what you want.
This one sounds particularly good:
http://malektips.com/xp_dos_0001.html
They range from real-time monitoring to the last x lines of the file.
Edit: I noticed that the included link is to a package It should work, but here are some more versions:
http://www.lostinthebox.com/viewtopic.php?f=5&t=3801
http://sourceforge.net/projects/tailforwin32
If file is too large it can take too long to get count of lines
another way is to use find and pass it a nowhere string
$find /v /c "%%$%!" yourtextfile.txt
this would result an output like this
$---------- yourtextfile.txt: 140
then you can parse output using for like this
$for /f "tokens=3" %i in ('find /v /c "%%$%!" tt.txt') do set countoflines=%i
then you can substract ten lines from the total lines
After trying all of the answers I found on this page none of them worked on my file with 15539 lines.
However I found the answer here to work great. Copied into this post for convenience.
#echo off
for /f %%i in ('find /v /c "" ^< C:\path\to\textfile.txt') do set /a lines=%%i
set /a startLine=%lines% - 10
more /e +%startLine% C:\path\to\textfile.txt
This code will print the last 10 lines in the "C:\path\to\textfile.txt" file.
Credit goes to OP #Peter Mortensen
using a single powershell command:
powershell -nologo "& "Get-Content -Path c:\logFile.log -Tail 10"
applies to powershell 3.0 and newer
I agree with "You should use TAIL" answer. But it does not come by default on Windows. I suggest you download the "Windows 2003 Resource Kit" It works on XP/W2003 and more.
If you don't want to install on your server, you can install the resource kit on another machine and copy only TAIL.EXE to your server. Usage is sooooo much easier.
C:\> TAIL -10 myfile.txt
Here's a utility written in pure batch that can show a lines of file within a given range.To show the last lines use (here the script is named tailhead.bat):
call tailhead.bat -file "file.txt" -begin -10
Any ideas how to echo or type the last
10 lines of a txt file?
The following 3-liner script will list the last n lines from input file. n and file name/path are passed as input arguments.
# Script last.txt
var str file, content ; var int n, count
cat $file > $content ; set count = { len -e $content } - $n
stex -e ("["+makestr(int($count))) $content
The script is in biterscripting. To use, download biterscripting from http://www.biterscripting.com , save this script as C:\Scripts\last.txt, start biterscripting, enter the following command.
script last.txt file("c:\log09.txt") n(10)
The above will list last 10 lines from file c:\log09.txt. To list last 20 lines from the same file, use the following command.
script last.txt file("c:\log09.txt") n(20)
To list last 30 lines from a different file C:\folder1\somefile.log, use the following command.
script last.txt file("C:\folder1\somefile.log") n(30)
I wrote the script in a fairly generic way, so it can be used in various ways. Feel free to translate into another scripting/batch language.

Resources