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
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.