Windows Vista Forums

Noob Question
  1. #1


    Peter Brown Guest

    Noob Question

    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).
    Thanks,

    Peter

      My System SpecsSystem Spec

  2. #2


    Rob Campbell Guest

    RE: Noob Question

    if ($_.name -match "^[a-h]")

    "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).
    > Thanks,
    >
    > Peter

      My System SpecsSystem Spec

  3. #3


    Matthias Tacke Guest

    Re: Noob Question

    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

      My System SpecsSystem Spec

  4. #4


    Peter Brown Guest

    Re: Noob Question

    Thank you for your assistance... now for the harder part.... I need to
    actually understand the code.. ;-)

    It's really appreciated...

    "Matthias Tacke" wrote:

    > Matthias Tacke wrote:
    >

    > > 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
    > > }
    > >
    >
    > This is now tested
    >
    > $sourcedir = "\\SourceServer\usersdir\"
    > $dirlist = get-childitem -path $sourcedir\* -Name
    > $dirlist |foreach-Object {
    > switch -regex ($_.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\"}
    > }
    > echo $sourcedir$_ $destdir$_
    > }
    >

      My System SpecsSystem Spec

  5. #5


    Peter Brown Guest

    RE: Noob Question

    thanks for the help... I'll work on the code translation....


    "Rob Campbell" wrote:

    > if ($_.name -match "^[a-h]")
    >
    > "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).
    > > Thanks,
    > >
    > > Peter

      My System SpecsSystem Spec

  6. #6


    Peter Brown Guest

    Re: Noob Question

    So is this actually valid $.beginswith ????

    "Matthias Tacke" wrote:

    > Matthias Tacke wrote:
    >

    > > 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
    > > }
    > >
    >
    > This is now tested
    >
    > $sourcedir = "\\SourceServer\usersdir\"
    > $dirlist = get-childitem -path $sourcedir\* -Name
    > $dirlist |foreach-Object {
    > switch -regex ($_.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\"}
    > }
    > echo $sourcedir$_ $destdir$_
    > }
    >

      My System SpecsSystem Spec

  7. #7


    Matthias Tacke Guest

    Re: Noob Question

    Peter Brown wrote:

    > Thank you for your assistance... now for the harder part.... I need to
    > actually understand the code.. ;-)
    >
    There's plenty of help available, for the switch statement see:

    <http://www.microsoft.com/technet/scriptcenter/resources/pstips/jan08/pstip0111.mspx>

    --
    HTH
    Matthias

      My System SpecsSystem Spec

  8. #8


    Matthias Tacke Guest

    Re: Noob Question

    Peter Brown wrote:

    > So is this actually valid $.beginswith ????
    >
    Sorry, your top posting makes it difficult to decide
    what you are referring to?

    If you have scripting experience with vbscript this url might
    help with the transition to powershell:
    <http://www.microsoft.com/technet/scriptcenter/topics/winpsh/convert/default.mspx>

    --
    Regards
    Matthias

      My System SpecsSystem Spec

Noob Question problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Noob question....sorry JaggedOne General Discussion 11 24 Aug 2009
Noob question about colours Rodux1 General Discussion 3 02 Jan 2009
noob to graphics card question dustinfrank Graphic cards 3 21 Mar 2008
Noob question! dustinfrank Graphic cards 7 07 Mar 2008
Vista Noob Question pvdg42 Vista General 7 27 May 2006