| 1. |
Solve : Outputting the Number of Files Copied and the Total Number of Lines Copied? |
|
Answer» I am attempting to WRITE a batch file that copies the data in all files with the specified criteria into one file (the date is included in the filename for the example below). I would like to output to the Audit.csv file the number of files copied and the total number of lines in all of the files (not including the first line of each file). I am using Windows 7. This is what I have so far. This is what I have so far. And what were the results? I'm not aware of any switches for the copy command that will produce a line count. My best guess would be the bulldozer approach, manually copying each line of each file to the combined file and doing the counting along the way. It's not pretty but the logic is sound. Code: [Select]@echo off ::Get date variable from user :: set /p dte=Enter the file date to copy as mmddyy: :: Start with new files :: if exist c:\mcr\combined_%dte%.csv del c:\mcr\combined_%dte%.csv if exist c:\mcr\audit_%dte%.csv del c:\mcr\audit_%dte%.csv ::Output each line of the files to the combined file :: for /f "tokens=* delims=" %%i in ('dir /b c:\mcr\%dte%*.*') do ( for /f "tokens=* delims=" %%j in (c:\mcr\%dte%%%i) do ( echo %%j >> c:\mcr\Combined_%dte%.csv set /a tLines+=1 ) set /a tFiles+=1 ) ::Write the current date & time to audit file :: echo %date% %time% %tLines% %tFiles% >> c:\mcr\audit_%dte%.csv Quote ... and the total number of lines in all of the files (not including the first line of each file). I wasn't sure why this was needed so I'll let you subtract the tFiles variable from the tLines variable just prior to to outputting the audit record. If you have long file names with embedded spaces or special characters you may need to use quotes in the for statements. Good luck. Count the number of lines in a file; doesn't skip blank lines, or lines starting with a semicolon like FOR does Code: [Select]For /f "tokens=1* delims=:" %%A in ( ' findstr /N /R ".*" "filename.ext" ' ) do set lines=%%A %lines% contains the number of lines in the file Code: [Select]@echo off setlocal enabledelayedexpansion if exist test.txt del test.txt for /l %%N in (1,1,10) do echo Line %%N blablabla>>test.txt echo.>>test.txt echo ; line starting with a semicolon>>test.txt for /l %%N in (13,1,20) do echo Line %%N blablabla>>test.txt echo test.txt begin echo -------------- type test.txt echo -------------- echo test.txt end echo. for /f "tokens=1* delims=:" %%A in ('findstr /N /R ".*" "test.txt"') do set lines=%%A echo findstr counted %lines% lines set flines=0 for /f "delims=" %%L in (test.txt) do ( set /a flines=!flines!+1 ) echo FOR /F counted %flines% lines echo. Code: [Select]test.txt begin -------------- Line 1 blablabla Line 2 blablabla Line 3 blablabla Line 4 blablabla Line 5 blablabla Line 6 blablabla Line 7 blablabla Line 8 blablabla Line 9 blablabla Line 10 blablabla ; line starting with a semicolon Line 13 blablabla Line 14 blablabla Line 15 blablabla Line 16 blablabla Line 17 blablabla Line 18 blablabla Line 19 blablabla Line 20 blablabla -------------- test.txt end findstr counted 20 lines FOR /F counted 18 lines Sidewinder - I tried your code and entered 102510 at the prompt. Filenames are 102510_ABC and 102510_DEF. I'm getting "The system cannot find the file c:\mcr\102510102510_ABC" and "The system cannot find the file c:\mcr\102510102510_DEF" Any ideas as to why this is happening?Sometimes you just have to look at things from a different angle, so I turned my monitor upside down. Actually I went back to your basic structure, shamelessly stole borrowed the counting lines code from Mr. Trout, did a little tweaking and came up with a result not nearly as ugly as the first. Code: [Select]@echo off setlocal set indir=c:\mcr set outdir=c:\mcr :: Get date variable from user :: set /p dte=Enter the file date to copy as mmddyy: :: Delete any PREVIOUS files :: if exist "%outdir%\combined_%dte%.csv" del "%outdir%\combined_%dte%.csv" if exist "%outdir%\audit_%dte%.csv" del "%outdir%\audit_%dte%.csv" :: Copy the files to combined file :: for /f "tokens=* delims=" %%i in ('dir /b "%indir%\%dte%*.*"') do ( type "%indir%\%%i" >> "%outdir%\combined_%dte%.csv" set /a tFiles+=1 ) :: Count the lines :: for /f "tokens=1* delims=:" %%i in ('findstr /N /R ".*" "%outdir%\combined_%dte%.csv"') do set tLines=%%i :: Write the current date & time & totals to audit file :: echo %date%,%time%,%tFiles%,%tLines% >> "%outdir%\audit_%dte%.csv" Some of the code is left over from testing but should not create any problems. Good luck. Sidewinder - Thank you so much. It works perfectly. And thanks to you also Salmon Trout.Sidewinder, well done, but don't forget that starting a COMMENT with the unofficial, undocumented, unsupported, DEPRECATED double colon breaks code if used inside parentheses... |
|