Peter Brown wrote:
> I'm new to Powershell as well as scripting (to a degree), and I'm in need of
> some advice:
>
> Here's my code:
>
> $sourcedir = "\\SourceServer\usersdir\"
> $destdir1 = "\\TargetNode1\usersa-h\"
> $destdir2 = "\\TargetNode1\usersi-l\"
> $destdir3 = "\\TargetNode2\usersm-p\"
> $destdir4 = "\\TargetNode2\usersq-v\"
> $destdir5 = "\\TargetNode3\usersw-x\"
> $dirlist = get-childitem -path $sourcedir\* -Name
> $dirlist |foreach-Object { robocopy.exe $sourcedir$_ '
> $destdir$_ /E /XO /R:3 /W:3 /NP /TEE /Log+:d:\RoboLogs\Userdata\$_.txt }
>
> I want to validate that the directory name starts with the letters (in the
> groups) a-h or i-l or m-p or q-v or w-x and then write to the appropriate
> destdir....
>
> How do I validate the 1st letter of $_ ??
>
> I know about [a-h] grouping, but I'm not sure of the validation of the 1st
> letter of a string (i.e. $_ in this case). Maybe something like this [untested].
$sourcedir = "\\SourceServer\usersdir\"
$dirlist = get-childitem -path $sourcedir\* -Name
$dirlist |foreach-Object { switch ($_.substring(0,1))
{
[a-h] {$destdir = "\\TargetNode1\usersa-h\"}
[i-l] {$destdir = "\\TargetNode1\usersi-l\"}
[m-p] {$destdir = "\\TargetNode2\usersm-p\"}
[q-v] {$destdir = "\\TargetNode2\usersq-v\"}
default {$destdir = "\\TargetNode3\usersw-x\"}
}
robocopy.exe $sourcedir$_ '
$destdir$_ /E /XO /R:3 /W:3 /NP /TEE /Log+:d:\RoboLogs\Userdata\$_.txt
}
--
HTH
Matthias