0

I have a directory with several csv files. I would like to return the line count for each file, the modified date of each file and the string in the first column after header row using batch scripts on window 10. How would I output to text file?

Example

File 1: String 100 lines 1/12/2019
File 2: String 100 lines 1/10/2019
File 3: String 200 lines 1/12/2019
4
  • 3
    what have you tried so far? May 24, 2019 at 2:59
  • Not much, as I am not skilled in batch coding. I found this code that said it should read the line of a file in a directory but no luck. for %G in (*.csv) do find /c /v "+" %G
    – Quest
    May 24, 2019 at 11:59
  • I am not sure where to start
    – Quest
    May 24, 2019 at 11:59
  • @Quest Read my answer on why that didn't work. There you have the pieces you need to make a script.
    – dmb
    May 24, 2019 at 13:24

1 Answer 1

1

I'm not going to make the script for you but I'm give you the methods for what you want. First let's split the question in two parts:

1.- Get line count in Dir

for /f "usebackq tokens=1 delims=," %%a in ("you_file_path") do set /a count+=1

and to get firt row after header you would do that but with a loop break

for /f "usebackq tokens=1 delims=," %%a in ("you_file_path") do (
    set /a count+=1 && if count==2 (first_row=%%a && goto next)
)
:next

2.- Modified Date

for /f "tokens=1,2 delims= " %%a in (
    'dir "FullDirPath" ^| find /i "File_name"'
) do set ArchDate=%%a

Now is up to you to glue this together and make a script, you would need to loop every file using the above code and at the end of each loop echo "%first_row%" "%archdate%" "%count%" or echo "%first_row%" "%archdate%" "%count%" > file.txt

You can test this methods in cmd but to make use of variables on the terminal you need to replace %%a for %a. On a batch script is %%a is the right syntax, but on the terminal %a is the right sintax.

Cheers.

2
  • Thank you, I am trying to solve the first requirement by using the code you provide. One step at a time as I figure this out. I am getting a on "" in the file.txt. for /f "usebackq tokens=1 delims=," %%a in ("C:\Users\kbearam\Desktop\New folder\New folder") do set /a count+=1 ) echo "%count%"
    – Quest
    May 24, 2019 at 14:05
  • @Quest Yes, you need to provide a file path, not a dir path.
    – dmb
    May 24, 2019 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .