user1683032
user1683032

Reputation: 41

Powershell - Copying Files to a share

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!

Upvotes: 3

Views: 488

Answers (1)

Bin
Bin

Reputation: 211

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)

}

Upvotes: 3

Related Questions