using object.name.substring

S

sporticia

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 :o(

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 Computer

S

sporticia

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 :o(
>
> 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 Computer

J

Justin Rich

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" <[email protected]> wrote in message
news:%[email protected]

> 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 :o(
>>
>> 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 Computer

Top