【问题标题】:Get file information from multiple files listed inside a text file从文本文件中列出的多个文件中获取文件信息
【发布时间】:2018-07-18 08:36:36
【问题描述】:

我在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

【问题讨论】:

  • 请查看tourHow to Askminimal reproducible example。你都尝试了些什么?如果你表现出一些努力会很好。
  • 另外,一个快速提示:使用 Powershell,查看Get-ContentGet-Item
  • 我已经尝试过:这在一个批处理文件上,虽然输出看起来很糟糕。 @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
  • 编辑您的问题并发布您在那里尝试过的代码。另外,“看起来很糟糕”是什么意思。你想要什么?
  • 在命令提示符处输入 DIR /? 并阅读使用信息输出。然后在一些单独的文件和目录上使用它的各种选项测试命令,(你学到了什么)。完成此操作并且您对该命令感到满意后,请在命令提示符处输入 FOR /?,然后再次开始阅读。 这不是给我代码的网站,也不是教程网站,请学习如何使用命令,编写代码,如果您有问题,请通过编辑和添加到您的问题中相应地对其进行格式化。

标签: powershell batch-file filelist


【解决方案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

【讨论】:

  • 我不确定是否应该对此投反对票、编辑或仅评论仅代码,请澄清... 虽然这个答案在技术上很好,但至少 2 个错别字,没有描述,并且确实显示出比 OP 问题更多的努力......(哦,可以放在评论中)
【解决方案2】:

批量解决方案,

  • 从环境变量 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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多