Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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脚本,用于解压缩多个文件并基于文件名和部分文件名创建.CSV索引_Powershell_Csv - Fatal编程技术网

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

Powershell脚本,用于解压缩多个文件并基于文件名和部分文件名创建.CSV索引,powershell,csv,Powershell,Csv,我正在使用PS脚本解压目录中的所有文件,然后根据每个文件的完整文件路径和从文件名部分提取的关键字创建一个.CSV索引文件 例如 目录包含:file1.zip、file2.zip等 将所有内容提取到一个目录:file1\u server\u 20150830, 文件1\u服务器2\u 20150831、文件2\u服务器\u 20150829等。 文件为二进制文件(文件类型在Windows中显示“文件”,无扩展名) 使用完整路径和部分文件名创建index.csv。文件名使用下划线,如图所示: x:

我正在使用PS脚本解压目录中的所有文件,然后根据每个文件的完整文件路径和从文件名部分提取的关键字创建一个.CSV索引文件

例如

  • 目录包含:file1.zip、file2.zip等
  • 将所有内容提取到一个目录:file1\u server\u 20150830, 文件1\u服务器2\u 20150831、文件2\u服务器\u 20150829等。 文件为二进制文件(文件类型在Windows中显示“文件”,无扩展名)
  • 使用完整路径和部分文件名创建index.csv。文件名使用下划线,如图所示:
    x:\folder\file1\u server\u 20150830,server,20150830
    x:\folder\file1\u server2\u 20150831,server220150831
    x:\folder\file2\u server\u 20150829,server,20150831
我是一个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' 

来自另一个论坛的伟大建议,似乎正在为我所需要的工作:

$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

来自另一个论坛的伟大建议,似乎正在为我所需要的工作:

$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