2

I'm trying to create a CSV with fullpath\filename, file owner and last write access (modification date) of all txt and html files from all hard drives of a data server.

Here's what I got so far:

set pgm=%~n0
set log=%~dpn0.log
set host=%COMPUTERNAME%
set csv=%host%.csv
set dir=D:\BME

if not exist "%csv%" type nul>"%csv%"
    for /f "delims=;" %%a in ('dir /b/s %dir%\*.txt, %dir%\*.html') do (
    >>%csv% echo "%%a"
)

That outputs the path + filename of all found txt and html files of a certain folder in a CSV. I tried this command to get the hard drives:

wmic logicaldisk where drivetype=3 get caption

But I can't get my head around how to store that in a variable or file and loop through it and also retrieve the owner and last modification date and put it into a new column of the csv file.

Clam
  • 75
  • 2
  • 11
  • How did you capture the output of the `dir` command line? perhaps the same method works for `wmic` either... by the way, `wmic` can also be used to retrieve file information -- see `wmic DataFile /?`... – aschipfl Aug 19 '16 at 08:42
  • see [here](http://stackoverflow.com/a/18024049/2152082) for an example – Stephan Aug 19 '16 at 09:29

2 Answers2

1

I can't get my head around how to store that in a variable

Use the following batch file.

GetDrives.cmd:

@echo off
setlocal enabledelayedexpansion
rem skip=1 to remove the header
rem findstr to remove blank lines
for /f "skip=1" %%d in ('wmic logicaldisk where drivetype^=3 get caption ^| findstr /r /v "^$"') do (
  set _drive=%%d
  echo !_drive!
  )
endlocal

Notes:

  • Be careful when using drivetype=3 as I have a removable drive of type 3. In the below output C: is a fixed hard disk and F: is a removable external USB drive.
  • Replace echo !_drive! as appropriate with a modified version of your existing code.

Example Output:

F:\test>GetDrives
C:
F:

F:\test>

Further Reading

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
1

DavidPostill answered how-to store wmic logicaldisk … output in a variable;

The script:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
set "pgm=%~n0"
set "log=%~dpn0.log"
set "host=%COMPUTERNAME%"
set "csv=%host%.csv"
set "dir=D:\BME"
set "dirmask=%dir%\*.txt, %dir%\*.html"

rem if not exist "%csv%" type nul>"%csv%"
>"%csv%" (
    for /f "delims=;" %%a in ('dir /b/s %dirmask% 2^>NUL') do (
          set "_fFullPath=%%~a"
          set "_fLastWrite=%%~ta"
          set "_fOwner="
          call :getRealOwner
          SETLOCAL EnableDelayedExpansion
            echo "!_fFullPath!","!_fOwner!","!_fLastWrite!"
          ENDLOCAL
      ) 
    )
)
type "%csv%"
goto :continue

:getRealOwner
SET "ESCAPED=%_fFullPath:\=\\%"

SET "UNDELIMITED="
for /F "skip=2 delims=" %%g in ('
  wmic path Win32_LogicalFileSecuritySetting where Path^="%ESCAPED%" ^
    ASSOC /RESULTROLE:Owner /ASSOCCLASS:Win32_LogicalFileOwner ^
          /RESULTCLASS:Win32_SID 2^>NUL
  ') do (
      SET "UNDELIMITED=%%g"
      call :process_wmioutput
)
if NOT defined UNDELIMITED set "_fOwner=???"
exit /B

:process_wmioutput
SET "DELIMITED=%UNDELIMITED:  =•%"
FOR /F "delims=• tokens=10,12" %%G in ("%DELIMITED%") DO set "_fOwner=%%H\%%G"
exit /B

:continue

I used next settings to demonstrate various output:

set "dir=D:"      
set "dirmask=%dir%\loc*.vbs %dir%\bcd*.log %dir%\act*.xsl %dir%\diag*.xml %dir%\chec*.csv"

Output - non-privileged cmd window:

==> D:\bat\SO\39034430.bat
"D:\odds and ends\tempx\links\testDJ\LocUsers.vbs","mypc\user","25.12.2014 00:13"
"D:\tempWin\ActivityLog.xsl","NT AUTHORITY\SYSTEM","24.02.2016 13:12"
"D:\tempWin\CompatTelemetryLogs\diagerr.xml","???","12.08.2015 03:17"
"D:\tempWin\CompatTelemetryLogs\diagwrn.xml","???","12.08.2015 03:17"
"D:\test\check_acl.csv","BUILTIN\Administrators","06.03.2016 14:28"

Output - privileged (run as administrator) cmd window:

=ADMIN=> D:\bat\SO\39034430.bat
"D:\odds and ends\tempx\links\testDJ\LocUsers.vbs","mypc\user","25.12.2014 00:13"
"D:\tempWin\ActivityLog.xsl","NT AUTHORITY\SYSTEM","24.02.2016 13:12"
"D:\tempWin\CompatTelemetryLogs\diagerr.xml","NT AUTHORITY\SYSTEM","12.08.2015 03:17"
"D:\tempWin\CompatTelemetryLogs\diagwrn.xml","NT AUTHORITY\SYSTEM","12.08.2015 03:17"
"D:\test\check_acl.csv","BUILTIN\Administrators","06.03.2016 14:28"
Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83