Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9108333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:47:03+00:00 2026-06-17T02:47:03+00:00

I’m getting an error that I can’t call a method on a null valued

  • 0

I’m getting an error that I can’t call a method on a null valued expression. However, I’m not sure WHY the parameters are resulting in a null value. I need a second set of eyes to look at this and give me some guidance.

$docpath = "c:\users\x\desktop\do"
$htmPath = "c:\users\x\desktop\ht"
$txtPath = "c:\users\x\desktop\tx"
$srcPath = "c:\users\x\desktop\ht"
#
$srcfilesTXT = Get-ChildItem $txtPath -filter "*.htm*"
$srcfilesDOC = Get-ChildItem $docPath -filter "*.htm*"
$srcfilesHTM = Get-ChildItem $htmPath -filter "*.htm*"
#
function rename-documents ($docs) {  
    Move-Item -txtPath $_.FullName $_.Name.Replace("\.htm", ".txt") 
    Move-Item -docpath $_.FullName $_.Name.Replace("\.htm", ".doc") 
}
ForEach ($doc in $srcpath) {
    Write-Host "Renaming :" $doc.FullName         
    rename-documents -docs  $doc.FullName   
    $doc = $null   
}

And the error….

You cannot call a method on a null-valued expression.
At C:\users\x\desktop\foo002.ps1:62 char:51
+     Move-Item -txtPath $_.FullName $_.FullName.Replace <<<< ("\.htm", ".txt")
    + CategoryInfo          : InvalidOperation: (Replace:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\users\x46332\desktop\foo002.ps1:63 char:51
+     Move-Item -docpath $_.FullName $_.FullName.Replace <<<< ("\.htm", ".doc")
    + CategoryInfo          : InvalidOperation: (Replace:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

First: it appears that my ("\.htm", ".txt") is what’s showing up as null. I’ve tried it without the \ – (".htm", ".txt") – as well and received the same results.

Second: syntactically, I’m interpreting my line as move-item <path> <source-file-passed-to-function> <replacement=name-for-file> (parameters-for-replacement). Is that an appropriate understanding of what this code is doing?

Third: Do I need to have a -literalpath parameter in there somewhere? MS TechNet and get-help have very little information on the uses of the -literalpath parameter; I was unable to find something relevant to my particular situation.

Help me understand what I’m missing. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:47:04+00:00Added an answer on June 17, 2026 at 2:47 am

    In the context of a simple function $_ is not defined. $_ is only valid in a pipeline. That is, $_ reprensents the current object being passed down the pipeline.

    With your current function definition try it this way:

    function Rename-HtmlDocument([System.IO.FileInfo]$docs, $newExt) {  
        $docs | Move-Item -Dest {$_.FullName -replace '\.htm$', $newExt} 
    }
    

    You can pass this function the $srcfilesDOC and $srcFilesTXT variables directly e.g.:

    Rename-HtmlDocument $srcFilesDOC .doc
    Rename-HtmlDocument $srcFilesTXT .txt
    

    Of course you could make this more generic and get the source extension from the FileInfo object e.g.:

    function Rename-DocumentExtension([System.IO.FileInfo]$docs, $newExt) {  
        $docs | Move-Item -Dest {$_.FullName.Replace($_.Extension, $newExt)} 
    }
    

    BTW PowerShell’s Move-Item command doesn’t have the parameters you’re using -txtPath and -docPath. Is this a function you’ve created?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.