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.
Related
I am writing a script that will loop through a folder I have, and display the name of the files. The only problem I am facing, is there are many folders, inside of folders. Example(Inside Test Upload folder, are three more files and two more folders. Inside those two folders are 3 files, and another folder that contains a single file) I am unsure how to continue the loop, until all files and every folder has been read.
$Files = Get-ChildItem "C:\Users\HelloWorld\Documents\Test Upload"
#write-host $Files
GetFolderContents($Files)
function GetFolderContents($Test){
#$Sub = Get-ChildItem "C:\Users\HelloWorld\Documents\Test Upload\$Test"
foreach($files in $Test){
#check if folder or file
if (! $files.PSIsContainer)
{
write-host "File"
}
else{
write-host "Folder"
#need to now loop through this folder and get its contents
}
}
}
Use the -Recurse switch parameter with Get-ChildItem to have it recursively enumerate the whole directory structure:
$Files = Get-ChildItem "C:\Users\HelloWorld\Documents\Test Upload" -Recurse
I want to compare files with same name but with different extensions present in the same directory.
Example: There are 6 files at a location.
1234.pdf
1234.xml
abcd.pdf
abcd.xml
5678.pdf
efgh.xml
I want to compare all the .pdf/.xml files with the files with .xml/.pdf extension which have a same name and find out if any of the .pdf or .xml file is missing like here in the above example the 5678.xml file is missing and efgh.pdf file is missing.
I tried to copy all the .pdf files in a text file and all the .xml files in another text file and tried comparing the strings within them but it isn't working.
Can anyone please let me know how can i compare the file names with different extensions?
Push-Location "\\Cifintgfsfs001\gfs\MXPDFXML\Data\Test"
Get-childitem *.xml | ForEach {
if (!(test-path "$($_.BaseName).pdf")){
"$($_.BaseName).pdf missing"
}
}
Get-childitem *.pdf | ForEach {
if (!(test-path "$($_.BaseName).xml")){
"$($_.BaseName).xml missing"
}
}
Pop-Location
I'm trying to feed the results of a Get-ChildItem call through io.compression.zipfile to create a zip file of "E:\Applications_Server_Test", excluding two folders "BACKUP" and "BACKUP2".
However, Powershell seems to be interpreting this as "$items = a string of directories and file names" instead of a recursive collection of directories and files I want to zip. I can find tutorials on using Get-ChildItem to exclude directories and I can find tutorials on how to zip a full directory or zip multiple directories but I can't find anything on zipping directories with exclusions. Can somebody tell me where I'm going wrong?
$source = "E:\Applications_Server_Test"
$destination = "E:\AST_Dump.zip"
$items = Get-ChildItem $source -Recurse | ?{ $_.fullname -notmatch "\\backup\\?" }
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($items, $destination)
Thanks!
At the moment you are trying to archive the object $items, not the folder. Unfortunately this is not gonna work.
Try to move the backup folders at the same drive (this will just change records at the drive and not move any data), then to archive the whole "E:\Applications_Server_Test" folder and to move back the backup folders.
Other option is to use ZipArchive.CreateEntry method and to add file by file in to the archive. But this is not so elegant ;)
how to add many folders from differents places to one file zip in PowerShell script using [io.compression.zipfile]
One way to do this is to use the Extensions
https://msdn.microsoft.com/en-us/library/system.io.compression.zipfileextensions(v=vs.110).aspx
A simple example for adding a single file would be as follows
Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"
$NewFilePath = "c:\pat\to\file.txt"
$EntryName = "file.txt"
$zip = [io.compression.zipfile]::Open("test.zip",[io.compression.ziparchivemode]::Create)
[io.compression.ZipFileExtensions]::CreateEntryFromFile($zip, $NewFilePath, $EntryName)
$zip.Dispose()
You can just iterate over your list of files adding them to your new zip file.
Note that you can reference the type without the full path if you wish.
I can compare files in two different folder with the commands:
$test = get-childitem -recurse -path C:\test
$test1 = get-childitem -recurse -path C:\test1
$counter = (diff $test $test1).count
I would like to know how many differences between those two folders. This works.
However, now I would like to compare the filenames in two .zip files. Is it possible to compare the files inside two .zip files and I get a return value for the counter of different files?
Thank you so much.
I have tested .NET ZIP functionality on PowerShell V4. I suspect it would work on V3 but not on V2 (or V1).
Add-Type -AN System.IO.Compression.FileSystem
$zip1 = [IO.Compression.ZipFile]::OpenRead("c:\test\test1.zip")
$zip2 = [IO.Compression.ZipFile]::OpenRead("c:\test\test2.zip")
$names1 = $zip1.Entries.FullName
$names2 = $zip2.Entries.FullName
$counter = (diff $names1 $names2).count
$zip1.Dispose()
$zip2.Dispose()