user263885
user263885

Reputation: 13

Creating multiple files using the same script

I have a powershell script that archives all files that aren't ps1 and csv files, and it works, but if the zip archive already exists, I have to copy the archive or delete it before running the command again, otherwise the Error appears indicating that the file already exists.

Get-ChildItem -Path .\* -Exclude *.ps1, *.csv | Compress-Archive -DestinationPath .\NonEssArch.zip -verbose

Add-Type -AssemblyName PresentationFramework

$msgBoxInput =  [System.Windows.MessageBox]::Show('Do you also want to delete these non-essential files?','Attention','YesNo','Info')

switch  ($msgBoxInput) {
  'Yes' {
  del -Path *.* -Exclude *.csv, *.ps1, *.zip
  }
  'No' {
  Exit
  }
}

I want to be able to run the script after the archive has already been created, without having it replacing the existing archive. The problem lies in the "-DestinationPath .\NonEssArch.zip" part, but I am having trouble implementing a numbering system of sort.

Any help will be appreciated, thanks.

Upvotes: 1

Views: 72

Answers (1)

Lee_Dailey
Lee_Dailey

Reputation: 7489

the simplest way to "unique-ify" a file name is to add a timestamp to it. you can adjust the granularity to suit your situation. right down to milliseconds if needed. [grin]

something like this ...

$NewFileName = 'NonEssArch_{0}.zip' -f (Get-Date).ToString('yyyy-MM-dd_HH-mm-ss')

right now, that would give you ...

NonEssArch_2019-05-04_17-12-27.zip

Upvotes: 2

Related Questions