0

I'm a newbie with some basic skills.

I get a .zip file which contains multiple directories with .csv files within. The program compiling those files does not distinguish between upper and lower case, so you can get files FILE.csv and file.csv in the same directory as separate files.

When I unzip, they are obviously recognized as duplicates.

My PowerShell script as follows works fine if there are no duplicates.

Add-Type -AssemblyName "System.IO.Compression.FileSystem"
function Unzip {
    Param(
        [string]$zipfile,
        [string]$outpath
    )

    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
$sourcefile3 = Get-ChildItem 'root\Drop\Archive*.zip'
$destination3 = 'root\Drop\All_Files'
Unzip $sourcefile3 $destination3

However, because my .zip contains multiple directories, I cannot specify a file name in the $destination3. I tried this, but it created a new folder instead of renaming that duplicate file within a folder.

if (Test-Path $destination3) {
    $i = 0
    while (Test-Path $destination3) {
        $i += 1
        $destination3 = ''root\Drop\All_Files$i'
    }
}

I read here and here, but this talks about files, not files within directories/folders.

halfer
  • 19,824
  • 17
  • 99
  • 186
byuli
  • 55
  • 2
  • 12
  • You cannot use `ExtractToDirectory()` on the archive file here. You must enumerate the content of the archive and then extract the files individually. – Ansgar Wiechers Feb 22 '19 at 13:34
  • Are you trying to extract the archive all in the same directory and not preserve the directory structure that's in the archive? – Ben Richards Feb 22 '19 at 14:50
  • I just need the csv files from all the subfolders. The unzipping extracts the main folder > 1K+ subfolders > csv files in those subfolders. There's a second part that then recurses through all folders and moves files into the main folder, leaving all the subfolders blank. The script stops when a duplicate file is found in one of those subfolders and I just want the script to save both files. Thank you. – byuli Feb 22 '19 at 15:29
  • I assume you run on Windows? Can you run your script on Linux? You could have both files with different case and then handle your situation as you'd like. .NET core support the System.IO.Compression.FileSystem namespace. (I have not tried .net core, but I assume your code would work) – P-L Feb 22 '19 at 17:28
  • Can only run on Windows, sorry – byuli Feb 25 '19 at 09:55

0 Answers0