Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

ps script to archive folders only when the folder has certain file types

$
0
0

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.

$source = "C:\Temp\Test\"
$archive = "C:\Temp\Test\Archive"

if (!(Test-Path -path $archive)) {New-Item $archive -Type Directory}
Copy-Item $source -Destination $archive -recurse -include *.txt,*.csv,*.xls,*.xlsx


SQLEnthusiast



Viewing all articles
Browse latest Browse all 21975

Trending Articles