Windows Vista Forums

using object.name.substring

  1. #1


    sporticia Guest

    using object.name.substring

    hi

    I'm trying to loop through files in a directory and then perform an
    action based on a part of the name of the file. the first if is to
    exclude .zip files

    the second if loop is supposed to check the 6th and 7th characters and
    compare them to $prevmonthint, which is a 2 character string
    representing a month (so 01=Jan, 02=Feb etc.)

    right now all I want it to do is if the value f $prevmonth int is say
    "03", print all files where the 6th and 7th characters are also "03"

    can anyone see an error in the syntax of the code below ? powershell
    aint having it (

    foreach ($file in Get-ChildItem $thislogdir)
    {
    if (! $file.name.endswith(".zip"))
    {
    if ($file.name.substring(6,2) = $prevmonthint)
    {
    Write-Host $file
    }
    }
    }

    powershell errors with

    Assignment failed because [System.String] doesn't contain a settable
    property 'substring()'.
    At :line:46 char:26
    + if ($file.name.substring <<<< (6,2) = $prevmonthint)

    thanks for any help

    _scott

      My System SpecsSystem Spec

  2.   


  3. #2


    sporticia Guest

    Re: using object.name.substring

    ignore !

    replaced

    if ($file.name.substring(6,2) = $prevmonthint)

    with

    if ($file.name.substring(6,2) -match $prevmonthint)

    and works fine

    need to get used to powershell comparison operators :-/

    thanks

    _scott

    sporticia wrote:

    > hi
    >
    > I'm trying to loop through files in a directory and then perform an
    > action based on a part of the name of the file. the first if is to
    > exclude .zip files
    >
    > the second if loop is supposed to check the 6th and 7th characters and
    > compare them to $prevmonthint, which is a 2 character string
    > representing a month (so 01=Jan, 02=Feb etc.)
    >
    > right now all I want it to do is if the value f $prevmonth int is say
    > "03", print all files where the 6th and 7th characters are also "03"
    >
    > can anyone see an error in the syntax of the code below ? powershell
    > aint having it (
    >
    > foreach ($file in Get-ChildItem $thislogdir)
    > {
    > if (! $file.name.endswith(".zip"))
    > {
    > if ($file.name.substring(6,2) = $prevmonthint)
    > {
    > Write-Host $file
    > }
    > }
    > }
    >
    > powershell errors with
    >
    > Assignment failed because [System.String] doesn't contain a settable
    > property 'substring()'.
    > At :line:46 char:26
    > + if ($file.name.substring <<<< (6,2) = $prevmonthint)
    >
    > thanks for any help
    >
    > _scott

      My System SpecsSystem Spec

  4. #3


    Justin Rich Guest

    Re: using object.name.substring

    the other thing to keep in mind is this isnt like other languages where you
    can use a = in an if statement.. you need to use the -eq which might be a
    LITTLE better here simply because -match is a regex and might be slightly
    more taxing...

    also, dont be scared to use -and


    if(!($file.name.endswith(".zip") -and ($file.name.substring(6,2) -eq
    $prevmonthint))
    {
    ...stuff
    }

    i think that will do what you want..

    my PS skills are fairly meh, but i think you get the idea..


    "sporticia" <scott@newsgroup> wrote in message
    news:%23vzC6Hb9KHA.3592@newsgroup

    > ignore !
    >
    > replaced
    >
    > if ($file.name.substring(6,2) = $prevmonthint)
    >
    > with
    >
    > if ($file.name.substring(6,2) -match $prevmonthint)
    >
    > and works fine
    >
    > need to get used to powershell comparison operators :-/
    >
    > thanks
    >
    > _scott
    >
    > sporticia wrote:

    >> hi
    >>
    >> I'm trying to loop through files in a directory and then perform an
    >> action based on a part of the name of the file. the first if is to
    >> exclude .zip files
    >>
    >> the second if loop is supposed to check the 6th and 7th characters and
    >> compare them to $prevmonthint, which is a 2 character string representing
    >> a month (so 01=Jan, 02=Feb etc.)
    >>
    >> right now all I want it to do is if the value f $prevmonth int is say
    >> "03", print all files where the 6th and 7th characters are also "03"
    >>
    >> can anyone see an error in the syntax of the code below ? powershell aint
    >> having it (
    >>
    >> foreach ($file in Get-ChildItem $thislogdir)
    >> {
    >> if (! $file.name.endswith(".zip"))
    >> {
    >> if ($file.name.substring(6,2) = $prevmonthint)
    >> {
    >> Write-Host $file
    >> }
    >> }
    >> }
    >>
    >> powershell errors with
    >>
    >> Assignment failed because [System.String] doesn't contain a settable
    >> property 'substring()'.
    >> At :line:46 char:26
    >> + if ($file.name.substring <<<< (6,2) = $prevmonthint)
    >>
    >> thanks for any help
    >>
    >> _scott

      My System SpecsSystem Spec


using object.name.substring
Similar Threads
Thread Forum
substring and error - why? VB Script
Binding TextBox.Text to property in nested (multi levels) object withbindingsource, Error if grandchild object is NULL .NET General
Getting substring to react sanely to end of index? PowerShell
Select-string excluding a substring PowerShell
Adding canonical aliases for Compare-Object, Measure-Object, New-Object PowerShell