|
Replies:
8
-
Pages:
1
-
Last Post:
May 1, 2008 6:27 PM
by: francis
|
|
|
Posts:
7
From:
canda
Registered:
4/24/08
|
|
|
|
you cannot call a method on a null-valued expression
Posted:
Apr 25, 2008 12:03 AM
|
|
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
|
|
Posts:
13
From:
Pennsylvania
Registered:
3/21/08
|
|
|
|
Re: you cannot call a method on a null-valued expression
Posted:
Apr 25, 2008 9:52 AM
in response to:
francis
|
|
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.
|
|
Posts:
7
From:
canda
Registered:
4/24/08
|
|
|
|
Re: you cannot call a method on a null-valued expression
Posted:
Apr 25, 2008 10:43 AM
in response to:
dmerida
|
|
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 = 'E:\PowerShell' 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
|
|
Posts:
13
From:
Pennsylvania
Registered:
3/21/08
|
|
|
|
Re: you cannot call a method on a null-valued expression
Posted:
Apr 25, 2008 1:41 PM
in response to:
francis
|
|
Hi Francis, give this a try instead:
function getFile { $strComputer = "." $TargetFolder = "E:\PowerShell" $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!
|
|
Posts:
7
From:
canda
Registered:
4/24/08
|
|
|
|
Re: you cannot call a method on a null-valued expression
Posted:
Apr 25, 2008 2:18 PM
in response to:
dmerida
|
|
Thanks dmerida I don't get the error anymore, doing some testing now. really really appreciate your help
|
|
Posts:
11
From:
Budapest, Hungary
Registered:
3/12/08
|
|
|
|
Re: you cannot call a method on a null-valued expression
Posted:
Apr 29, 2008 4:29 AM
in response to:
francis
|
|
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!" } }
|
|
Posts:
7
From:
canda
Registered:
4/24/08
|
|
|
|
Re: you cannot call a method on a null-valued expression
Posted:
Apr 29, 2008 10:11 PM
in response to:
soostibi
|
|
Thanks will give it a try
|
|
Posts:
2
From:
Baltimore, Maryland
Registered:
3/7/08
|
|
|
|
Re: you cannot call a method on a null-valued expression
Posted:
May 1, 2008 2:55 PM
in response to:
francis
|
|
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.
|
|
Posts:
7
From:
canda
Registered:
4/24/08
|
|
|
|
Re: you cannot call a method on a null-valued expression
Posted:
May 1, 2008 6:27 PM
in response to:
Late4dinner
|
|
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
|
|
|
Legend
|
|
Gold: 300
+
pts
|
|
Silver: 100
- 299
pts
|
|
Bronze: 25
- 99
pts
|
|
Manning Author
|
|
Manning Staff
|
|