简体   繁体   中英

Zip a folder and archive it using powershell

Im new to powershell and I would like to archive the folders using it

following are my folders in C:\Bridge\Build\destination

Vista

Tamil

CSK

I want to archive these folders in C:\Bridge\Build\Build_Achive\2020-05-15 with the file name as 2020-05-1.zip

The folder destination and file name will be generated based on current date and time stamp so I would like to use the variable in destination and file name

I tried the following code its giving me error can anyone please help

$path = "C:\Bridge\Build\Build_Achive\$((Get-Date).ToString('yyyy-MM-dd'))"
    If(!(test-path $path))
        {
              New-Item -ItemType Directory -Path "C:\Bridge\Build\Build_Achive\$((Get-Date).ToString('yyyy-MM-dd'))"
              ECHO 'Created folder '  $path
        }
    ELSE 
        {
              Write-Host 'Folder ' ' - ' $((Get-Date).ToString('yyyy-MM-dd'))   ' - ' 'already exists'
        }
$FileLogdate = (Get-Date -f yyyy-MM-dd-HHmmss)



echo $path
echo $FileLogdate
$filename =$path\$FileLogdate
Compress-Archive -Path C:\Bridge\Build\destination -DestinationPath filename.zip

You have problems in the last two lines:

  1. Variable file name - you need to use quotes, as you are trying to create a string: $filename ="$path\$FileLogdate "
  2. I believe you would like to compress the files not with name filename.zip , but with the content of the variable $filename: Compress-Archive -Path C:\TEMP\AdComputers_WindowsServer_2008.csv -DestinationPath $filename

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM