The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

francis (7) [Avatar] Offline
#1
when i run this script

$Files = get-childitem $TargetFolder -include *.* -recurse |
Where {$_.name -like '~'}

foreach ($File in $Files)
{
$File.Delete()
}
and i get the error "you cannot call a method on a null-valued expression"

please help
dmerida (13) [Avatar] Offline
#2
Re: you cannot call a method on a null-valued expression
Hi Francis

In your script, instead of using Where{$_.name -like '~'}, use Where{$_.name -match "~"}

-match will return true if any part of the string matches the pattern. So in this case, if you're looking for file names that have ~ anywhere in the name, using -match "~" will help you find them.

The rest of your script is fine. After you run it, you should see all files that contain ~ anywhere in the name will be deleted.
francis (7) [Avatar] Offline
#3
Re: you cannot call a method on a null-valued expression
Thanks dmerida, however I tried that change and i still get the same error message. But I think I will keep the double qoutes in there. maybe i should paste the whole script here it is

function getFile
{
$strComputer = "."
$TargetFolder = 'EsmilieowerShell'

if (Test-Path $TargetFolder)
{
#Warn you the targeted folder, so you can double check
Write-host “The Targeted Folder is:” $TargetFolder -foregroundcolor “Red”

Write-Host “If This Is Not The Intended Target, Press ‘Ctrl + C’ To Exit” -foregroundcolor “Yellow”
Start-sleep -s 15

$Files = get-childitem $TargetFolder -include *.* -recurse | Where {$_.name -like "~"}

foreach ($File in $Files)
{
$File.Delete()
}
}
Else
{
Write-Host “The Folder $TargetFolder Does Not Exist!”
}
}
getFile
dmerida (13) [Avatar] Offline
#4
Re: you cannot call a method on a null-valued expression
Hi Francis, give this a try instead:

function getFile
{
$strComputer = "."
$TargetFolder = "EsmilieowerShell"
$Test = test-path $TargetFolder

if($Test -eq "True")
{
#Warn you the targeted folder, so you can double check
Write-host “The Targeted Folder is:” $TargetFolder -foregroundcolor “Red”

Write-Host “If This Is Not The Intended Target, Press ‘Ctrl + C’ To Exit” -foregroundcolor “Yellow”
Start-sleep -s 15

$Files = get-childitem $TargetFolder -include *.* -recurse | Where {$_.name -match "~"}

foreach ($File in $Files)
{
$File.Delete()
}
}
Else
{
Write-Host “The Folder $TargetFolder Does Not Exist!”
}
}

getFile


After you run that script, check that file location and you should see the files with "~" in it's name were deleted.

In your original script, test-path $TargetFolder returns $true if the folder exists but still requires a comparison operator to evaluate the expression. So although true was returned, the if loop had no means to evaluate the expression.

Hope this helps!
francis (7) [Avatar] Offline
#5
Re: you cannot call a method on a null-valued expression
Thanks dmerida I don't get the error anymore, doing some testing now. really really appreciate your help
soostibi (11) [Avatar] Offline
#6
Re: you cannot call a method on a null-valued expression
How do you like that:

function getFile ([string] $TargetFolder = (Get-Location) )
{
if (Test-Path $TargetFolder)
{
#Warn you the targeted folder, so you can double check
Write-Warning "The Targeted Folder is: $TargetFolder"
Write-Warning "If This Is Not The Intended Target, Press 'Ctrl + C' To Exit"
Start-sleep -s 15

get-childitem $TargetFolder -recurse | Where {$_.name -match "~"} | foreach-object {$_.Delete()}
}
else
{
Write-Error "The Folder $TargetFolder Does Not Exist!"
}
}
francis (7) [Avatar] Offline
#7
Re: you cannot call a method on a null-valued expression
Thanks will give it a try
Late4dinner (2) [Avatar] Offline
#8
Re: you cannot call a method on a null-valued expression
Francis,

Is the problem that there are no files to delete?

When you run the

foreach ($File in $Files)

is $Files populated? I used to get this in perl all the time until I figured it out.
francis (7) [Avatar] Offline
#9
Re: you cannot call a method on a null-valued expression
yes the files were there, after i used the suggestions and help i got here it started working, Thanks everybody for the help, scripting is getting much easier and fun now. keep up the good works guys