3

This is my first post so be gentle.

I'm starting to use Powershell and found it very intuitive and would like some advice/examples on how to properly script something specific instead of just copying files from one place to another.

Files structure:

c:\backup\archive\

test_20130320_000711_backup.rar
test1_20130320_000711_backup.rar
test_20130320_001215_backup.rar
test1_20130320_001215_backup.rar
test_20130321_000811_backup.rar
test1_20130321_000811_backup.rar
test_20130321_001015_backup.rar
test1_20130321_001015_backup.rar

Unpacking directory:

c:\unpack_file\[contents of each rar goes here, one at a time]

destination:

E:\backup\archive\[date of file above e.g. 20130320 created]\[unpacked rar file contents here]

What I need to do is copy each file by date and unpack each, one at a time. Then copy the contents to the destination folder and remove whats in the unpacking folder. phew!

I've done some basic scripting but this is a real challenge so any pointers or examples would be amazing!

  • 2
    Why don't you just un-RAR the files to their final destination? What good would unpack-copy-delete do? – vonPryz Apr 09 '13 at 18:08
  • Hey vonPryz, I'm trying to set limitations for myself to learn real word scenario's. The reason why I wanted to unpack them one at a time is to minimize disk memory on the c:. This way it unpacks each file, copies the contents to a different drive and then removes the unpacked files and starts again on the next file. This way you dont have gigs of data on the C:. – user1683032 Apr 10 '13 at 09:55

1 Answers1

3

I am not sure why you need the intermediate directory. Here is some code to get you started.

$rarExe = "C:\Program Files (x86)\7-Zip\7z.exe"
$srcDir = "C:\backup\archive"
$dstDir = "E:\backup\archive"

$rarFiles = gci $srcDir

foreach ($file in $rarFiles){
    $arcDate = $file.basename.Split("_")[1]
    New-Item "$dstDir\$arcDate" -type directory

    Write-Output "EXECUTING: $($startinfo.FileName) $($startinfo.Arguments)"
    $startinfo = new-object System.Diagnostics.ProcessStartInfo
    $startinfo.FileName = $rarExe
    $startinfo.Arguments = "e $($file.fullname) -o$dstDir\$arcDate"
    $process = [System.Diagnostics.Process]::Start($startinfo)

}
Bin
  • 211
  • 1
  • 7
  • Thanks for this! if I wanted to then create another directory after {New-Item "$dstDir\$arcDate" -type directory} inside it I would just do the same right? {New-Item "$dstDir\$arcDate\archive" -type directory} and place the files in this new directory – user1683032 Apr 10 '13 at 09:58