0

I googled for days and could not find much help.

I am trying to make a powershell .PS1 file to run it every 30 minutes to check if there is a new folder, It will RAR contents inside the folder and keep the original folder the same. Rar the files within those folders and delete the source.

For example:

I will set the Destination to:

Data = "C:\test"

NewDestination = "C:\backup"

And I will have folders:

"C:\test\folder 1"

  • somedata.txt
  • files.exe
  • my video.mp4

"C:\test\folder 2"

  • somedata.txt
  • somedata2.txt
  • somedata3.doc

It does not matter if those folder has content inside or not, I want to make rar file for those folders in their original name and delete the original folder.

I will have this:

"C:\test\folder 1\folder 1.rar"

"C:\test\folder 2\folder 2.rar"

When files in subfolders are rared and deleted, move the main folder to another destination.

NewDestination = "C:\backup"

"C:\backup\folder 1\folder 1.rar"

"C:\backup\folder 2\folder 2.rar"

foreach ($i in Get-ChildItem -dir C:\test)

I tried with some of these lines but could not make it happen.

If you have something which might help me, plz do share.

Thank you and sorry if it's a repeated question.

1

Cole Try something like this

$Source = "C:\temp"
$Destination = "D:\Backup"
# Gather Subfolders only (-directory parameter). If there are files on the root source remove this parameter to collect all files and folders
$Tree = Get-ChildItem -Path $Source -Directory | Select-Object -Property Name, FullName 

# Treatment
foreach ($dir in $tree)
    {
    # to avoid to overwrite an existing archive file, use the parameter -update
    # the name of the destination archive file will be the same of the source dir
    Compress-Archive -Path $dir.fullName -DestinationPath $Destination\$($dir.name) -CompressionLevel Optimal -Update
    Write-Verbose "$($dir.FullName) has been archived to $Destination\$($dir.name)"
    # At this step, a new .zip archive has been created, but the source folder is already present, then go to delete it
    Remove-Item -Path $dir.FullName -Force
    Write-Verbose "$($dir.FullName) has been removed"
    }

I use the native cmdlet Compress-archive (Powershell>2) with optimal compression

Olivier

  • Thank you very very much. I modified a little bit and all working perfectly. Thanks once again ^__^ – M. Cole Oct 6 '19 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.