# Move a folder to archive along with specific files

**URL:** <https://forums.powershell.org/t/move-a-folder-to-archive-along-with-specific-files/6601>\
**Category:** PowerShell Help\
**Created:** [June 7, 2016, 10:35pm UTC](https://forums.powershell.org/t/move-a-folder-to-archive-along-with-specific-files/6601 "2016-06-07T22:35:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkatesh-sql](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@venkatesh-sql](https://forums.powershell.org/u/venkatesh-sql)\
**Post date:** [June 7, 2016, 10:35pm UTC](https://forums.powershell.org/t/move-a-folder-to-archive-along-with-specific-files/6601/1 "2016-06-07T22:35:13Z")

</div>

How to write powershell script to archive folders only when the folder has files of certain type for instance (.txt, .csv, .xls, .xlsx). My source path is "C:\Temp\Test" and there are 5 sub-folders

foldera  
folderb  
folderc  
folderd  
foldere  
Archive

Only foldera, folderb, folderc have the files to be archived, the archive path is “C:\Temp\Test\Archive” and this has folders foldera, folderb so these folders need not be created but files need to be zipped and copied, folderc needs to be created and the files have to be zipped and copied.

Using the code below I get the files but not the folder that particular file was in. I am able to create the folders but get error if the folder already exists and if I just copy-item I can move just the files but cannot create the folder. Please guide me.

​$source = "C:\Temp"

$archive = “C:\Temp\Archive”

$archive = Get-ChildItem -Recurse -Path $source -include \*.txt, \*.csv, \*.xls| select directory | sort -Unique -Property Directory | Select -expand Directory | Copy-Item -Destination $archive

If (!(Test-Path $archive)) {

Get-ChildItem -Recurse -Path $source -include \*.txt, \*.csv, \*.xls | select directory | sort -Unique -Property Directory | Select -expand Directory | Copy-Item -Destination $archive

}

else {

Write-Host “Directory $archive already exists!”

}​

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [June 8, 2016, 9:50am UTC](https://forums.powershell.org/t/move-a-folder-to-archive-along-with-specific-files/6601/2 "2016-06-08T09:50:30Z")

</div>

This will display files as they are added to zip files. If you want to learn more about adding to zip files using .NET and PowerShell, check the link.

> **[PowerShell File Frontier, Part 6: Adding Files to a Zip Archive -- Microsoft...](https://mcpmag.com/articles/2014/09/29/file-frontier-part-6.aspx)**
>
> Prof. Powershell's rundown of what can be done with zipped files in PowerShell continues.

```
$source = "C:\Temp\"
$dest = "C:\Temp\Archive"
$files = Get-ChildItem -Path $source -Recurse -Exclude Archive -Include '*.txt','*.csv','*.xls'
Add-Type -assembly 'system.io.compression.filesystem'

foreach ($file in $files){
$folder = $file.directoryname | Split-Path -Leaf

# Create zip
If (!(Test-Path $dest$folder.zip)){New-Item $dest$folder.zip -ItemType file}

# Add files to zip
$zip = [io.compression.zipfile]::Open("$dest$folder.zip",'Update')
[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$file.FullName,$file.name)
$zip.dispose()
}

# Results
# Archive\foldera.zip    
# Archive\folderb.zip
```

---

<div class="post-metadata">

**Author:** ![venkatesh-sql](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@venkatesh-sql](https://forums.powershell.org/u/venkatesh-sql)\
**Post date:** [June 9, 2016, 11:26pm UTC](https://forums.powershell.org/t/move-a-folder-to-archive-along-with-specific-files/6601/3 "2016-06-09T23:26:56Z")

</div>

Thanks very much.

---

<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:41pm UTC](https://forums.powershell.org/t/move-a-folder-to-archive-along-with-specific-files/6601/4 "2024-05-16T20:41:04Z")

</div>


