Roy Ho
Roy Ho

Reputation: 11

For & Find Command

My script:

cd\
set Directory=Directory
set Filename=Filename
set Date Created=Date Created
set Date Modified=Date Modified
set Date Accessed=Date Accessed
set Size=Size

Echo %Directory%,%Filename%,%Date Created%,%Date Modified%,%Date    Accessed%,%Size% >>C:\MyText.csv

For /r C: %%a in ("*.exe" "*.msi") Do (
@echo %%a

For /f "tokens=1* delims=]" %%b in ('dir /tc "%%a" ^| find /n " " ^| find "[6]"') Do echo %%c >>C:\MyText.csv

For /f "tokens=1* delims=]" %%b in ('dir /tw "%%a" ^| find /n " " ^| find "[6]"') Do echo %%c >>C:\MyText.csv

For /f "tokens=1* delims=]" %%b in ('dir /ta "%%a" ^| find /n " " ^| find "[6]"') Do echo %%c >>C:\MyText.csv
)
@pause

Upvotes: 0

Views: 1049

Answers (3)

Stephan
Stephan

Reputation: 56238

You have to split the output of your command. At best, use another for /f loop for that:

For /r C: %%a in ("*.exe" "*.msi") do (
  @echo %%a
  for /f "tokens=1* delims=]" %%b in (' Dir /ta "%%a" ^| find /n " " ^| find "[6]"') do echo %%c
)

It splits the dir output into %%b=[6 and %%c=10/09/2018 02:22 PM xxxxxxx.exe

EDIT based on new information in the question:

first get all desired data, then echo the complete line.

@echo off
setlocal enabledelayedexpansion
(
  Echo Directory,Filename,Date Created,Date Modified,Date Accessed,Size
  For /r C: %%a in ("*.exe" "*.msi") Do (
    For /f "tokens=1,2" %%b in ('dir /tc "%%a" ^| findstr /b "[0-9]"') Do set "tc=%%b %%c"
    For /f "tokens=1,2" %%b in ('dir /tw "%%a" ^| findstr /b "[0-9]"') Do set "tw=%%b %%c"
    For /f "tokens=1,2" %%b in ('dir /ta "%%a" ^| findstr /b "[0-9]"') Do set "ta=%%b %%c"
    echo %%~dpa,%%~nxa,!tc!,!tw!,!ta!,%%~za
  )
)>C:\MyText.csv

Redirecting is done just once to avoid to open/write/close the file for every line.

Upvotes: 1

DodgyCodeException
DodgyCodeException

Reputation: 6143

You seem to be using find /n " " | find "[6]" just to get the 6th line of dir output (which happens to be the line that lists the file details, while other lines are header and summary lines).

A simpler way to do this is as follows:

For /r C: %%a in ("*.exe" "*.msi") do (
  echo %%a
  Dir /ta %%a | find "/"
)

This searches for the "/" in the date that is shown on the same line as the file. This character does not appear in the header or the summary.

One caveat is that it assumes your locale uses "/" as date separator (which is the case in most countries).

By the way, last access time may not always be recorded. See this question: batch script that tells me when was the last time a file has been accessed

Upvotes: 0

user7818749
user7818749

Reputation:

Seems you wimply want date and time or filename with it's drive and path:

simply do:

For /r D: %a in ("*.exe" "*.msi") do echo %~dpnta

see more on variable substitution when doing for /? from cmdline.

Upvotes: 0

Related Questions