How to avoid “You cannot call a method on a null-valued expression” error when file might be empty?
I want to open a file, replace some content, and append to another file. I wrote a Powershell script:
(Get-Content file.txt).replace("abc", "def") | Add-Content other.txt
This works fine, unless file.txt is empty. Then it gives an error
You cannot call a method on a null-valued expression.
Why does it give that error? And how can I avoid it?
I expected opening an empty file to return empty rather than null, as it would in unix
cat file.txt | sed -r s/abc/def/g >> other.txt
.net powershell
add a comment |
I want to open a file, replace some content, and append to another file. I wrote a Powershell script:
(Get-Content file.txt).replace("abc", "def") | Add-Content other.txt
This works fine, unless file.txt is empty. Then it gives an error
You cannot call a method on a null-valued expression.
Why does it give that error? And how can I avoid it?
I expected opening an empty file to return empty rather than null, as it would in unix
cat file.txt | sed -r s/abc/def/g >> other.txt
.net powershell
add a comment |
I want to open a file, replace some content, and append to another file. I wrote a Powershell script:
(Get-Content file.txt).replace("abc", "def") | Add-Content other.txt
This works fine, unless file.txt is empty. Then it gives an error
You cannot call a method on a null-valued expression.
Why does it give that error? And how can I avoid it?
I expected opening an empty file to return empty rather than null, as it would in unix
cat file.txt | sed -r s/abc/def/g >> other.txt
.net powershell
I want to open a file, replace some content, and append to another file. I wrote a Powershell script:
(Get-Content file.txt).replace("abc", "def") | Add-Content other.txt
This works fine, unless file.txt is empty. Then it gives an error
You cannot call a method on a null-valued expression.
Why does it give that error? And how can I avoid it?
I expected opening an empty file to return empty rather than null, as it would in unix
cat file.txt | sed -r s/abc/def/g >> other.txt
.net powershell
.net powershell
edited Nov 21 '18 at 13:54
Colonel Panic
asked Nov 21 '18 at 12:20
Colonel PanicColonel Panic
81k61301393
81k61301393
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Use PowerShell's -Replace
operator instead of the Replace
method of the returned String
(being null for an empty file).
(Get-Content file.txt) -Replace "abc", "def" | Add-Content other.txt
add a comment |
$path = "test.txt"
$path2 = "test2.txt"
try{
$content = (Get-Content $path).replace("abc", "def") | Add-Content $path2
Write-Host "Successfully added content" -ForegroundColor Green
}catch{
if(!$content){
Write-Host "$path is empty" -ForegroundColor Yellow
}else{
throw $_
}
}
I think you should always work with try/catch when running cmdlets..
Hope this helps
add a comment |
Give this a shot:
(Get-Content file.txt).replace("abc", "def") | Add-Content other.txt -erroraction SilentlyContinue
There are other options for the type of erroraction
, so just use tab completion to view them and test them if SilentlyContinue does not do what you're looking for!
EDIT: If you need to know that something has failed because it is empty, please view Bernard's answer. Otherwise this one liner should work as well, just depends on what your needs are.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411902%2fhow-to-avoid-you-cannot-call-a-method-on-a-null-valued-expression-error-when-f%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use PowerShell's -Replace
operator instead of the Replace
method of the returned String
(being null for an empty file).
(Get-Content file.txt) -Replace "abc", "def" | Add-Content other.txt
add a comment |
Use PowerShell's -Replace
operator instead of the Replace
method of the returned String
(being null for an empty file).
(Get-Content file.txt) -Replace "abc", "def" | Add-Content other.txt
add a comment |
Use PowerShell's -Replace
operator instead of the Replace
method of the returned String
(being null for an empty file).
(Get-Content file.txt) -Replace "abc", "def" | Add-Content other.txt
Use PowerShell's -Replace
operator instead of the Replace
method of the returned String
(being null for an empty file).
(Get-Content file.txt) -Replace "abc", "def" | Add-Content other.txt
edited Nov 21 '18 at 17:48
answered Nov 21 '18 at 17:29


pfxpfx
5,270121934
5,270121934
add a comment |
add a comment |
$path = "test.txt"
$path2 = "test2.txt"
try{
$content = (Get-Content $path).replace("abc", "def") | Add-Content $path2
Write-Host "Successfully added content" -ForegroundColor Green
}catch{
if(!$content){
Write-Host "$path is empty" -ForegroundColor Yellow
}else{
throw $_
}
}
I think you should always work with try/catch when running cmdlets..
Hope this helps
add a comment |
$path = "test.txt"
$path2 = "test2.txt"
try{
$content = (Get-Content $path).replace("abc", "def") | Add-Content $path2
Write-Host "Successfully added content" -ForegroundColor Green
}catch{
if(!$content){
Write-Host "$path is empty" -ForegroundColor Yellow
}else{
throw $_
}
}
I think you should always work with try/catch when running cmdlets..
Hope this helps
add a comment |
$path = "test.txt"
$path2 = "test2.txt"
try{
$content = (Get-Content $path).replace("abc", "def") | Add-Content $path2
Write-Host "Successfully added content" -ForegroundColor Green
}catch{
if(!$content){
Write-Host "$path is empty" -ForegroundColor Yellow
}else{
throw $_
}
}
I think you should always work with try/catch when running cmdlets..
Hope this helps
$path = "test.txt"
$path2 = "test2.txt"
try{
$content = (Get-Content $path).replace("abc", "def") | Add-Content $path2
Write-Host "Successfully added content" -ForegroundColor Green
}catch{
if(!$content){
Write-Host "$path is empty" -ForegroundColor Yellow
}else{
throw $_
}
}
I think you should always work with try/catch when running cmdlets..
Hope this helps
answered Nov 21 '18 at 12:37


Bernard MoeskopsBernard Moeskops
27826
27826
add a comment |
add a comment |
Give this a shot:
(Get-Content file.txt).replace("abc", "def") | Add-Content other.txt -erroraction SilentlyContinue
There are other options for the type of erroraction
, so just use tab completion to view them and test them if SilentlyContinue does not do what you're looking for!
EDIT: If you need to know that something has failed because it is empty, please view Bernard's answer. Otherwise this one liner should work as well, just depends on what your needs are.
add a comment |
Give this a shot:
(Get-Content file.txt).replace("abc", "def") | Add-Content other.txt -erroraction SilentlyContinue
There are other options for the type of erroraction
, so just use tab completion to view them and test them if SilentlyContinue does not do what you're looking for!
EDIT: If you need to know that something has failed because it is empty, please view Bernard's answer. Otherwise this one liner should work as well, just depends on what your needs are.
add a comment |
Give this a shot:
(Get-Content file.txt).replace("abc", "def") | Add-Content other.txt -erroraction SilentlyContinue
There are other options for the type of erroraction
, so just use tab completion to view them and test them if SilentlyContinue does not do what you're looking for!
EDIT: If you need to know that something has failed because it is empty, please view Bernard's answer. Otherwise this one liner should work as well, just depends on what your needs are.
Give this a shot:
(Get-Content file.txt).replace("abc", "def") | Add-Content other.txt -erroraction SilentlyContinue
There are other options for the type of erroraction
, so just use tab completion to view them and test them if SilentlyContinue does not do what you're looking for!
EDIT: If you need to know that something has failed because it is empty, please view Bernard's answer. Otherwise this one liner should work as well, just depends on what your needs are.
edited Nov 21 '18 at 12:39
answered Nov 21 '18 at 12:23
cet51cet51
668421
668421
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411902%2fhow-to-avoid-you-cannot-call-a-method-on-a-null-valued-expression-error-when-f%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown