Analysis of some bugs in for command


1. About colon: when a line begins with, use for / F “tokens = 1 * delims =:”%% I in (‘findstr / n* test.txt ) do echo.%% J will:
Filter out;

2. About semicolon: findstr* test.txt You can display the lines beginning with semicolons completely, but you can’t find them in the for statement. You must add the parameter / n
It seems that the for statement will ignore the lines beginning with semicolons as comments, which is not the fault of findstr, but the type, more and findstr that are not placed in the for statement are OK;

The solutions to problem 1 and problem 2 are as follows:

A little more complicated:

@echo off
:: empty lines are not ignored
for /f “delims=” %%i in (‘findstr /n .* test.txt’) do (
set “str=%%i”
call set “str=%%str:*:=%%”
call echo “%%str%%”

The most concise:

@echo off
:: this code ignores empty lines
for /f “delims= eol=” %%i in (test.txt) do echo %%i

3. In the for extension,%% ~ Si means that the path of the extension only contains short file names. However, in the following test process, it is found that this is not the case

Test environment:
D:\abcdefg hijk\abcd efgh.txt
D:\abcdefg hijk\te st.txt

Test code:
@echo off
for /f “delims=” %%i in (‘dir /a /b *.txt’) do echo %%~si

Test results:

See the last record of the test results? Actually extended to test ~ 1. Txttxt!

To sum up the error situation, it seems like this: when more than one place in the path is expanded to a short file name because the directory name exceeds 11 characters, the file name + suffix name
Files with less than 11 characters and spaces will have extended errors.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]