Tips & Tricks

@@Dosbatch_logo@@

DOS batch scripts

Findstr w. OR

findstr /I /R "@fn @brief" handleStrings.php

Get first/last line from file

Source: Fetch only first line from text file using Windows Batch File @@Stackoverflow_icon@@


(ECHO:A a&ECHO:B b&ECHO:C c)>file.txt

:: Fetch only first line from text file using Windows Batch File - https://stackoverflow.com/a/46134683
ECHO Get first line from file:
SET first_line=
SET /P first_line=<file.txt&CALL ECHO %first_line%

ECHO Get last line from file:
SET last_line=
FOR /F "UseBackQ Delims==" %%A In ("file.txt") DO SET "last_line=%%A" 
ECHO %last_line%

ZIP

On Windows 10 build 17063 or later you can use tar.exe (Source: Superuser @@Superuser_icon@@ )

C:\> tar -xf archive.zip

ls -l

@echo off&SETLOCAL enabledelayedexpansion
# ls -l
for /f "tokens=*" %%i in ('dir /a /b /-p /o:gen') do ( 
    SET "_FILE=%%i                                       ."
    SET "_ATTRIB=%%~ai ."
    SET "_SIZE=           %%~zi"
    ECHO !_FILE:~0,40!	%%~ti !_SIZE:~-11!  !_ATTRIB:~0,11!.
)

Get last token

:: https://stackoverflow.com/a/26493855
:getLastToken
SETLOCAL
    for %%a in ("%~2\.") do set "lastPart=%%~nxa"
    ENDLOCAL&SET "%~1=%lastPart%"
GOTO :EOF

Loops

[!WARNING] You cannot break out of an infinite loop

for /L %%L in ( ) do  (

Will hang!

Instead use huge values in loop - and bail out:

set cnt=0

for /L %%L in (0 1 9999) do  (
    set /a cnt+=1
    echo Loop !cnt!
    if /I !cnt! GTR  10 goto :break
)
:break

Works nicely