I'm trying to zip every file in a directory but I can't seem to get it to use the directory or the old file name. It needs to delete the old file so that it's protected in the zip file.
This is what I came up with so far:
$files = Get-ChildItem "D:\Backuptest\input\*" |
ForEach ($filename in $files) { $content = Get-Content $_.FullName & "C:\Program Files\WinRAR\Rar.exe" a -m0 -hps8Cs7y7RfLjYgYb -k -df $filename.rar } 2 1 Answer
PowerShell Rar Archive, Encrypt, and Delete Multiple Files in a Folder
With Powershell and the below syntax I....
- set the variables for the source directory up top to tell it thepath to get the files to compress, and
- set the variable for the destination directory telling it the path for the encrypted archive rar files are to be output to,
so this makes it a little more explicit than it was before telling it where the files will go before deleting them (no more /System32, etc,).
This way uses the ForEach (method) to get the file object filename per file in the source directory and then one-by-one it will create a new rar encrypted file with the same file name as the non-encrypted [source] file and append the .rar to the end of it. (E.g. test123.txt will become test123.txt.rar encrypted and then test123.txt will be deleted.)
I think your issue was either (1.)the syntax of the rar commands not being correct, or (2.)not having the destination in the rar commands for the output location and that running implicitly from that directory and just outputting there.
Powershell Script
$Source = Get-ChildItem "D:\Backuptest\input"
$Destination = "D:\Backuptest\input"
$RarApp = "C:\Program Files\WinRAR\Rar.exe"
ForEach ($files in $Source) { & $RarApp a $($Destination + $files.Name + ".rar") $($files.FullName) -m0 -hps8Cs7y7RfLjYgYb -k -df }