简体   繁体   中英

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.

Eg

  • Directory contains: file1.zip, file2.zip, etc.
  • 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)
  • Create index.csv using full path and parts of file names. File names use underscores as shown:
    x:\\folder\\file1_server_20150830,server,20150830
    x:\\folder\\file1_server2_20150831,server2,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.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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