Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 从文本文件中列出的多个文件中获取文件信息_Powershell_Batch File_Filelist - Fatal编程技术网

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

Powershell 从文本文件中列出的多个文件中获取文件信息,powershell,batch-file,filelist,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.... 编辑 我在批处理文件中尝试了这一点,但输出看起来很糟糕: @

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

试着这样做:

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

  • 从环境变量temp和
  • 向控制台报告不存在的文件

样本输出:

> 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"
> .\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"
一个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.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"
> .\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"

请看一下,还有。到目前为止你试过什么?如果你表现出一些努力,那就太好了。另外,一个快速提示:使用Powershell,看看
Get Content
Get 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.tx编辑您的问题并发布您在那里尝试过的代码。你说的“看起来糟透了”是什么意思。需要什么?在命令提示下输入
DIR/?
,然后读取使用信息输出。然后在一些单独的文件和目录上,使用它的各种选项(您所学到的)测试该命令。完成此操作并熟悉该命令后,在命令提示下输入
以获取/?
,然后再次开始阅读。这不是一个“给我代码”网站,也不是一个教程网站,请学习如何使用命令,编写代码,如果您有问题,请通过相应的编辑和格式化将其添加到您的问题中。我不确定我是否应该对此投反对票、编辑此代码或仅评论代码,请澄清。。。虽然这个答案在技术上很好,但它至少有两个拼写错误,没有描述,而且实际上没有比OPs问题更费劲。。。(哦,可以加入评论)