none
Exisitng zip Files not getting deleted. RRS feed

  • Question

  • I have some folders to zip. When i re-run the script the existing .zip files must be deleted before zipping.

    I tried this code. It is zipping perfectly my folders starting with ABC. But it is not getting deleted as i am appending Timestamp to the filename. So this part of code  If(Test-path $destination) {Remove-item $destination} isn't working. Any clues how to fix it? I need to delete existing .zip files  before zipping new files.

    $TimeStamp=Get-Date -Format 'yyyyMMddhhmmss'
    $source= Get-ChildItem -Path $outputReportFolder "*ABC*?" -Directory
    Add-Type -assembly "system.io.compression.filesystem"
    Foreach ($s in $source)
     {
      $destination = Join-path -path $outputReportFolder -ChildPath "$($s.name)_f1_t1_$TimeStamp.zip"
      If(Test-path $destination) {Remove-item $destination}
      [io.compression.zipfile]::CreateFromDirectory($s.fullname, $destination)}

     
    Wednesday, May 31, 2017 8:56 PM

All replies

  • What have you tried? Is the condition met, i.e. is Test-Path finding your path? Have you tried -Force parameter?

    We can't do the debugging for you, since we would need your computer.

    Thursday, June 1, 2017 5:49 AM
  • It's the logical error:

    When you run the script next time, the value in $TimeStamp changed because of "Get-Date"

    $Destination consists of " $($s.name)_f1_t1_$TimeStamp.zip" and since $TimeStamp has been changed, you can't include the correct file name (the previous file name when you created the files).

    Therefore, Test-Path doesn't work & Remove-Item operation is not successful.

    Thursday, June 1, 2017 1:59 PM