简体   繁体   English

Powershell脚本解压缩多个文件并基于文件名和部分文件名创建.CSV索引

[英]Powershell script to unzip multiple files and create a .CSV index based on file name, and partial file name

I am working on a PS script to unzip all files in a directory, then create a .CSV index file based on the full file path of each file, and keywords pulled from the parts of the file name. 我正在使用PS脚本来解压缩目录中的所有文件,然后根据每个文件的完整文件路径以及从文件名部分提取的关键字来创建.CSV索引文件。

Eg 例如

  • Directory contains: file1.zip, file2.zip, etc. 目录包含:file1.zip,file2.zip等。
  • Extract all to a single directory: file1_server_20150830, file1_server2_20150831, file2_server_20150829 etc. Files are binary (file type shows "File" in Windows, no extension) 将所有内容提取到一个目录:file1_server_20150830,file1_server2_20150831,file2_server_20150829等。文件为二进制文件(Windows中文件类型显示为“ File”,无扩展名)
  • Create index.csv using full path and parts of file names. 使用完整路径和部分文件名创建index.csv。 File names use underscores as shown: 文件名使用下划线,如下所示:
    x:\\folder\\file1_server_20150830,server,20150830 X:\\文件夹\\ file1_server_20150830,服务器,20150830
    x:\\folder\\file1_server2_20150831,server2,20150831 X:\\文件夹\\ file1_server2_20150831,server2,20150831
    x:\\folder\\file2_server_20150829,server,20150831 ... x:\\ folder \\ file2_server_20150829,server,20150831 ...

I am a novice PS scripter, so what I have so far is below, commented lines are what I have been swapping in and out and tweaking to try and build my index file. 我是PS脚本的新手,所以到目前为止,这里是我所要发表的内容,下面是注​​释行,这些内容是我一直在换入和调整并尝试构建索引文件的内容。

$path = "X:\backup\test\source"
$dest = “X:\backup\test\import”
$shell_app= New-Object -com shell.application
$files = Get-ChildItem -Path $path -filter *.zip -recurse

foreach($file in $files) {

  $zip_file = $shell_app.namespace($file.FullName)

  $copyHere = $shell_app.namespace($dest)

  $copyHere.Copyhere($zip_file.items())

}

Get-ChildItem -Path $dest | Get-ChildItem -Path $dest | ForEach-Object { $_.BaseName -Replace '[_]', ',' -Replace ' ', '' -Replace 'index', '' } |
Out-File 'X:\backup\test\import\index.txt'



#Get-childItem 'X:\backup\test\import\index.txt' | ForEach { 
#(Get-Content $_ | ForEach {$_ -replace '21148965A', 'X:\backup\test\import\21148965A'}) | Set-Content $_}


#$index2 = {Get-ChildItem -Path $dest | ForEach-Object { $_.BaseName -Replace '[_]', ',' -Replace ' ', '' -Replace 'import', '' }}

#Get-ChildItem -Path $dest | Select-Object DirectoryName, $index2 | Export-Csv -Path 'X:\backup\test\import\index.txt' -NoTypeInformation



#Out-File 'X:\backup\test\import\index.txt'

#Get-ChildItem -Path $dest | ForEach-Object { $_.BaseName -Replace '[_]', ',' -Replace ' ', '' -Replace 'import', '' } |  Out-File 'X:\backup\test\import\index.txt' 

Great suggestion from another forum, seems to be working for what I need: 来自另一个论坛的好建议似乎正在满足我的需求:

$path = 'X:\backup\test\source'
$dest = 'X:\backup\test\import'
$shell_app= New-Object -com shell.application
$files = Get-ChildItem -Path $path -filter *.zip -recurse

foreach($file in $files) {

  $zip_file = $shell_app.namespace($file.FullName)

  $copyHere = $shell_app.namespace($dest)

  $copyHere.Copyhere($zip_file.items())

}

$tabName = "Results"
$table = New-Object system.Data.DataTable “$tabName”
$col1 = New-Object system.Data.DataColumn Folder,([string])
$col2 = New-Object system.Data.DataColumn FileName,    ([string])

$table.columns.add($col1)
$table.columns.add($col2)

gci $dest | % {
$base=$_.BaseName -replace '[_]', ',' -Replace ' ', '' -Replace     'index', ''
$folder = $_.DirectoryName

$row = $table.NewRow()
$row.Folder = $folder 
$row.FileName = $base

$table.Rows.Add($row)

}

$table | export-csv $dest\index.csv -noType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM