繁体   English   中英

使用批处理文件将文件行数与所有子目录中的文件内容进行比较

[英]Compare file line count with a file content in all sub directories using batch file

我可以将一个文件中的行数与另一个文件的内容进行比较,例如:file1行数为10 file2包含12,则不等于

使用以下脚本

@ECHO off
rem using linecount and putfileinvar to compare  count and datafile
Set _File=testfile.txt
Set /a _Lines=0
For /f %%j in ('Type %_File%^|Find "" /v /c') Do Set /a _Lines=%%j
set /p myvar= < filecount.tmp
rem echo %myvar%%_lines%
IF %myvar% == %_lines% (ECHO eq )  ELSE (ECHO neq )

现在,我想对整个目录及其子目录执行此操作,其中每个子目录包含两个文件数据文件,其计数在一个单独的文件中

我想将数据文件的行数与计数文件进行比较

:: list foleders in the root directory and store them in list file
dir /b /o:n /ad > dirlist.txt
:: for each directory in list
     FOR /F "tokens=*" %%v in (dirlist.txt) do (

     ::store dat file names in each directory in a temp file
     ::Log inside each folder
     cd  %%v
     ::list dat files in file.txt
     dir  *.dat /b /o:n  > file.txt
     ::for each file stored in file.txt
     FOR /F "tokens=*" %%m in (file.txt) do (
     ::count lines inside dat file
     findstr /R /N "^" %%m | find /C ":"  > save_temp_count.txt 
     ::Get the contents of the temp file and save it to the %mycount% variable
     set /a mycount<=save_temp_count.txt
     )
     ::delete temp files
     del file.txt
     ::Get all files tmp files
     dir *.tmp /b /o:n  > file.txt
     ::get file names in file.txt
     for /F "tokens=*" %%j in (file.txt) do ( 
         set /a fcount=<%%j
               )

     if  %fcount% == %mycount% (echo 1eq )  else (echo neq & echo %%v >> ../result.txt)
     ::delete temp files
     del file.txt
     del save_temp_count.txt
     ::go up back to parent folder
     cd ..

        )

复制此文件并将其粘贴到您的文件夹所在的根目录中,请注意,此脚本不会处理意外情况,例如:

在一个文件夹中找到2个以上的文件。 扩展名与预期的不同(dat,tmp)。 tmp文件不包含整数。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM