使用批处理文件计算行数和列数 [英] Count rows and columns using a batch file
问题描述
我需要计算几个文件的行数和列数.到目前为止,我设法获得了行数
I need to count the number of lines and columns that several files have. So far I managed to get the number of rows
@echo off
if exist counts.txt del counts.txt
for /R "path To folder" %%a in (*.txt) do call :sub %%a
goto :eof
:Sub
for /F %%c in ('type "%*" ^| find /c /v ""') do set rowCount=%%c
echo %*;%rowCount% >> counts.txt
此批处理文件保存用;
分隔的文件名和行数.我卡住的是列数.有什么主意吗?
This batch file saves file name and row count separated by ;
. What I got stuck with is the column count. Any idea?
可能输出为
<代码>文件路径;行数;列数
推荐答案
解决方案1:
一个基本的 for
循环将未加引号的逗号,空格,制表符和分号作为定界符.魔术之处在于它将正确地跳过引号中的逗号.要对CSV文件中的列进行计数,您可以(第一行)中的%% I做%count
.这仅在csv标头行中不包含未用引号引起来的空格或其他定界符的情况下有效.如果您希望遵循原始逻辑并计算逗号数,请参见下面的解决方案2.
Solution 1:
A basic for
loop evaluates unquoted commas, spaces, tabs, and semicolons as delimiters. The magic is that it'll correctly skip quoted commas. To count columns in your CSV file, you could just for %%I in (first line) do increment counter
. This will only work if your csv header row does not contain unquoted spaces or other delimiters. If you prefer to follow your original logic and count the number of commas, then see solution #2 below.
@echo off
setlocal
if exist counts.txt del counts.txt
for /R "path To folder" %%a in (*.txt) do call :sub "%%~fa"
goto :eof
:Sub
setlocal
for /F %%c in ('type "%~1" ^| find /c /v ""') do set rowCount=%%c
:: read first line of file.txt into var
set /P "line=" <"%~1"
:: delayed expansion prevents evaluation of special characters
setlocal enabledelayedexpansion
set count=0
for %%I in (!line!) do set /a count += 1
endlocal & set "colCount=%count%"
:: If you want to echo only filename.ext to counts.txt, un-rem this and
:: use %filename% instead of %~1 in your echo.
rem for %%I in ("%~1") do set "filename=%%~nxI"
echo %~1;%rowCount%;%colCount% >> counts.txt
endlocal
解决方案2:
使用:length
函数获取带或不带逗号的标题行的长度,并返回差值+1.(:length
函数逻辑基于 jeb的:StringLength
.)这仅在您的csv文件没有标题的情况下有效行,或者保证标头行不包含引号逗号.
Solution 2:
Uses a :length
function to get the length of the header row with and without commas, and returns the difference + 1. (:length
function logic is based on jeb's :StringLength
.) This will only work if your csv files have no header rows, or if the header rows are guaranteed not to contain quoted commas.
@echo off
setlocal
if exist counts.txt del counts.txt
for /R "path To folder" %%a in (*.txt) do call :sub "%%~fa"
goto :eof
:Sub
setlocal
for /F %%c in ('type "%~1" ^| find /c /v ""') do set rowCount=%%c
:: read first line of file.txt into var
set /P "line=" <"%~1"
set colCount=0
:: delayed expansion prevents evaluation of special characters
setlocal enabledelayedexpansion
set line=!line:"=!
call :length b4 "!line!"
call :length after "!line:,=!"
endlocal & set /a colCount = %b4% - %after% + 1
:: If you want to echo only filename.ext to counts.txt, un-rem this and
:: use %filename% instead of %~1 in your echo.
rem for %%I in ("%~1") do set "filename=%%~nxI"
echo %~1;%rowCount%;%colCount% >> counts.txt
echo %~1;%rowCount%;%colCount%
endlocal
goto :EOF
:length <return_var> <string>
setlocal enabledelayedexpansion
set "tmpstr=%~2"
set ret=0
for %%I in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if not "!tmpstr:~%%I,1!"=="" (
set /a "ret += %%I"
set "tmpstr=!tmpstr:~%%I!"
)
)
endlocal & set "%~1=%ret%"
goto :EOF
这篇关于使用批处理文件计算行数和列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!