[英]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 例如
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.