# How to compare Folder Name to a zip file name

**URL:** <https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152>\
**Category:** PowerShell Help\
**Created:** [March 7, 2019, 12:27pm UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152 "2019-03-07T12:27:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![timaty](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@timaty](https://forums.powershell.org/u/timaty)\
**Post date:** [March 7, 2019, 12:27pm UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152/1 "2019-03-07T12:27:39Z")

</div>

I have created a script that looks at “older than two years” files in source location, and create a zip for files from each year. So files from 2015 would be zipped into 2015.zip, files from 2016 into 2016.zip so on and so forth. Issue I am running into is that I need to check if the file already exists, if it does than pass over that year and move onto the next year - which I am unable to figure out yet, since I have to compare the name of the folder vs name of zip file (test-path won’t work), like folder named “2015” vs zip named 2015.zip. Furthermore, I create a temporary folder for each year move the files to it then zip them back in the source location. I can provide my code hear if needed, but thanks in advance for your help.

> # Exit script in case of any error $ErrorActionPreference = 'Stop' Add-Type -assembly "system.io.compression.filesystem"
> 
> $SourcePath = “C:\Users\archive”  
> $destination = “C:\Users\archive{0}.zip”  
> $logfile=“C:\Users\archive\zipfiles\_folders.log”
> 
> $groups = Get-ChildItem $SourcePath -exclude \*.zip |  
> Where-Object{ ($_.LastWriteTime.Year -le [datetime]::Now.year - 2) -and ($_.psIsContainer -eq $false) } |  
> Group-Object { “{0:yyyy}” -f $\_.LastWriteTime }
> 
> if($groups -eq $null){  
> write-output “NO FILES TO ARCHIVE.” \>\> $logfile  
> }
> 
> # Create a temporary working dir
> 
> $TmpDirPath = "C:\Test"  
> $TmpDirectory = New-Item -Path $TmpDirPath -ItemType Directory
> 
> ForEach ($group in $groups){  
> $FileNames = ($group.Name)
> 
> # delete temp directorty to save space on C:\
> 
> if (test-path -path $GroupDirectory){  
> del $GroupDirectory -recurse -force  
> }
> 
> # Create a new directory for the group
> 
> $GroupDirectory = New-Item -Path (Join-Path $TmpDirectory.FullName -ChildPath $group.Name) -ItemType Directory
> 
> if (test-path “$destination$FileNames.zip”){  
> write-output " already exists!" \>\> $logfile  
> }  
> else{
> 
> # Move files into the new directory
> 
> $group.Group | Move-Item -Destination $GroupDirectory.FullName  
> }
> 
> # Create the year-specific zip file
> 
> [System.IO.Compression.ZipFile]::CreateFromDirectory($GroupDirectory.FullName, ($destination -f $group.Name))  
> write-output “$FileNames.zip CREATED.” \>\> $logfile  
> }

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [March 7, 2019, 6:11pm UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152/2 "2019-03-07T18:11:23Z")

</div>

Since, you have not shown the code you are trying, thus forcing us to try and read your use case and write code for you, which would be based on assumptions and thus wrong.

You can use Test-Path or Get-ChildItem to check for a folder name or part of a folder name, or just match on a substring or RegEx match.

```
Get-ChildItem -Path E:\Zipped | Format-Table -AutoSize

# Results

<#
    Directory: E:\Zipped

Mode LastWriteTime Length Name   
---- ------------- ------ ----   
d----- 3/7/2019 3:36 PM Zip2016
d----- 3/7/2019 3:32 PM Zip2017
d----- 3/7/2019 3:32 PM Zip2018

    Directory: E:\Zipped\Zip2016

Mode LastWriteTime Length Name                      
---- ------------- ------ ----                      
-a---- 3/7/2019 3:31 PM 0 New Text Document_2016.txt

    Directory: E:\Zipped\Zip2017

Mode LastWriteTime Length Name                      
---- ------------- ------ ----                      
-a---- 3/7/2019 3:31 PM 0 New Text Document_2017.txt

    Directory: E:\Zipped\Zip2018

Mode LastWriteTime Length Name                      
---- ------------- ------ ----                      
-a---- 3/7/2019 3:31 PM 0 New Text Document_2018.txt
#>

((Get-ChildItem -Path 'E:\Zipped' -Directory).FullName) -Match '2017|2018'

# Results

<#
E:\Zipped\Zip2017
E:\Zipped\Zip2018
#>

# Using Test-Path
'2017','2018' | %{Test-Path -Path "E:\Zipped\*$PSItem"}

# Results

<#
True
True
#>
```

For the zip file, you just need to add in the extension.

---

<div class="post-metadata">

**Author:** ![timaty](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@timaty](https://forums.powershell.org/u/timaty)\
**Post date:** [March 8, 2019, 10:08am UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152/3 "2019-03-08T10:08:29Z")

</div>

Thanks.

I have added script now.

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [March 9, 2019, 4:10am UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152/4 "2019-03-09T04:10:18Z")

</div>

I am going to assume you are using the .Net name space because you are on an OS/PS version that does have the Compress-\* cmdlets as well as using .Net for datetime vs the Built-In Get-Date that’s been in PS since day 1.

```
Get-Command -Name '*archive*' | Format-Table -AutoSize

CommandType Name Version Source                      
----------- ---- ------- ------                      
Function Compress-Archive 1.0.1.0 Microsoft.PowerShell.Archive
Function Expand-Archive 1.0.1.0 Microsoft.PowerShell.Archive
```

For the date, using Get-Date.

```
(Get-Date).Date.AddYears(-2).Year
```

Yeppers, it’s a bit longer in the tooth, but, you know, it’s PS. **;-}**

 

_Yet, you have a lot going on in the script, and outside of the Test-Path / compare thing, which I’ve already addressed, you should be fine. I would have approached this a bit differently for simplification._

 

_For example changing this …_

 
_```
$groups = Get-ChildItem $SourcePath -exclude *.zip |
Where-Object{ 
    ($_.LastWriteTime.Year -le [datetime]::Now.year – 2) -and
    ($_.psIsContainer -eq $false) 
 }
```_
 

_… to this._

 
_```
$groups = Get-ChildItem -File -Path $SourcePath\* -Exclude '*.zip' | 
Where-Object -Property LastWriteTime -le $((Get-Date).Date.AddYears(-2))
```_
 

_Yet, it’s all about choice._

---

<div class="post-metadata">

**Author:** ![timaty](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@timaty](https://forums.powershell.org/u/timaty)\
**Post date:** [March 12, 2019, 12:04pm UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152/5 "2019-03-12T12:04:23Z")

</div>

I keep getting this error:

Test-Path : Cannot bind argument to parameter ‘Path’ because it is null.  
At line:29 char:25

- if (test-path -path $GroupDirectory){
- 

```auto

```

- CategoryInfo : InvalidData: (🙂 [Test-Path], ParentContainsErrorRecordException
- FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCommand

And it’s at this line here

**if (test-path -path $GroupDirectory**){  
del $GroupDirectory -recurse -force  
}

Can you please guide me on this as I have looked at other resources but don’t see anything useful. Note that my script is actually doing join-path to create $GroupDirctory.

Thanks!

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [March 12, 2019, 3:19pm UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152/6 "2019-03-12T15:19:29Z")

</div>

As for …

```
if (test-path -path $GroupDirectory){
 del $GroupDirectory -recurse -force
 }
```

… You don’t have that variable populated from anything / anywhere, thus it’s null.

Just put your cursor at the top of your script and do a search for $GroupDirectory and you’ll see that the first place it lands is that if statement. What is supposed to be in that?

---

<div class="post-metadata">

**Author:** ![timaty](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@timaty](https://forums.powershell.org/u/timaty)\
**Post date:** [March 13, 2019, 9:42am UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152/7 "2019-03-13T09:42:35Z")

</div>

Thanks postanote!

It was written on my forehead but couldn’t see it until you asked this question.  
Now, $GroupDirectory is temporary directory to hold data-set per year before it is zipped up to the $destination location. However, I am working with large data and want to avoid blowing up C:\ drive by deleting $GroupDirectory before moving on to the next year’s data-set for each $group(this holds all data for one year).

This is what I needed to do:

> if (test-path -path $TmpDirectory){ del $TmpDirectory -recurse -force }

---

<div class="post-metadata">

**Author:** ![timaty](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@timaty](https://forums.powershell.org/u/timaty)\
**Post date:** [March 13, 2019, 2:23pm UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152/8 "2019-03-13T14:23:20Z")

</div>

It turns out that PS Version2 on our client’s servers doesn’t have Compress-Archive, so I regressed and now using an in-house zipping utility which keeps throwing following error. This utility returns retval = 0 as successful completion but my powershell goes nuts and:

dtflate : retval = 0 At line:41 char:5 + smflate -z $GroupDirectory ($destination -f $group.Name) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (retval = 0:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError

Once again, what do you suggest?  
Thanks!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/how-to-compare-folder-name-to-a-zip-file-name/12152/9 "2024-05-16T20:33:08Z")

</div>


