从文本文件内列出的多个文件中获取文件信息

时间:2018-07-18 08:36:36

标签: powershell batch-file filelist

我在files.txt中列出了多个文件,这些文件位于不同的文件夹中。我需要获取导出到.txt.csv文件的文件信息,例如修改日期和大小。

Windows中是否有命令?或如何将其放入批处理文件或Powershell脚本中?

files.txt内容:

c:\windows\system32\file1.dll
c:\windows\temp\file2.dll
c:\program files\file3.exe
and so on....

修改

我在批处理文件中尝试了此操作,但是输出看起来很糟糕:

@echo off
dir c:\windows\system32\file1.dll >>filelist.txt
dir c:\windows\temp\file2.dll >>filelist.txt
dir c:\program files\file3.exe >>filelist.txt

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

Get-Content "C:\temp\file.txt" | where {Test-Path $_} | %{Get-Item $_} | select FullName, LastWriteTime

说明:

Test-Path : for test if file exist
Get-Item : for take file information

答案 1 :(得分:0)

批处理解决方案

  • 从环境变量temp和中获取文件位置
  • 将不存在的文件报告给控制台

:: Q:\Test\2018\07\19\SO_51397282.cmd
@Echo off
set "FileIn=%temp%\file.txt"
set "FileOut=%temp%\file.csv"

( Echo "FileName","Size","LastWriteTime"
  For /f "usebackq delims=" %%A in ("%FileIn%") do (
    If Exist "%%A" (
      For %%B in ("%%A") Do Echo "%%~fB","%%~zB","%%~tB"
    ) else (
      >CON: Echo File %%A doesn't exist
    )
  )
) > "%FileOut%"

示例输出:

> SO_51397282.cmd
File blabla blub doesn't exist

> type %temp%\file.csv
"FileName","Size","LastWriteTime"
"Q:\Test\2018\07\19\SO_51397282.cmd","370","2018-07-19 14:26"
"Q:\Test\2018\07\19\SO_51397282.ps1","354","2018-07-19 14:19"
"Q:\Test\2018\07\19\SO_51416162.ps1","473","2018-07-19 14:03"

与PowerShell解决方案非常相似。

## Q:\Test\2018\07\19\SO_51397282.ps1
$FileIn  = "$($Env:temp)\file.txt"
$FileOut = "$($Env:temp)\file.csv"

$Csv = Get-Content $FileIn | ForEach-Object {
    If (Test-Path $_) {
        Get-Item $_ | Select-Object FullName,Length,LastWriteTime
  } Else {
    Write-Host ("File {0} doesn't exist!" -f $_)
  }
}
$Csv
$Csv| Export-Csv $FileOut -NoTypeInformation

示例输出:

> .\SO_51397282.ps1
File blabla blub doesn't exist!

FullName                           Length LastWriteTime
--------                           ------ -------------
Q:\Test\2018\07\19\SO_51397282.cmd    370 2018-07-19 14:26:21
Q:\Test\2018\07\19\SO_51397282.ps1    372 2018-07-19 14:30:02
Q:\Test\2018\07\19\SO_51416162.ps1    473 2018-07-19 14:03:31

> type $env:tmp\file.csv
"FullName","Length","LastWriteTime"
"Q:\Test\2018\07\19\SO_51397282.cmd","370","2018-07-19 14:26:21"
"Q:\Test\2018\07\19\SO_51397282.ps1","372","2018-07-19 14:30:02"
"Q:\Test\2018\07\19\SO_51416162.ps1","473","2018-07-19 14:03:31"