简体   繁体   English

如何使用Powershell将Set-Content写入文件?

[英]How do I write Set-Content to a file using Powershell?

I'm doing a number of string replacements in a PowerShell script. 我正在PowerShell脚本中进行许多字符串替换。

foreach ($file in $foo) {
    $outfile = $outputpath + $file
    $content = Get-Content ($file.Fullname) -replace 'foo','bar'
    Set-Content -path $outfile -Force -Value $content 
}

I've validated (through console logging of $outfile and $content , which I don't show in the above code) that the proper files are being selected, the -replace is accuratly updating the content, and the $outfile s are being created. 我已经验证(通过控制台记录$outfile$content ,我没有在上面的代码中显示),已经选择了正确的文件, -replace正在正确更新内容,并且$outfile正在创建。 However, each of the output files a 0 byte file. 但是,每个输出文件均为0字节文件。 The Set-Content line does not appear to be writing the data to the files. Set-Content行似乎没有将数据写入文件中。 I've tried piping Set-Content to Out-File , but that just gives me an error. 我试过Set-Content传递到Out-File ,但这给我一个错误。

When I replace Set-Content with Out-File , I get a runtime error Out-File : A parameter cannot be found that matches parameter name 'path'. Set-Content替换为Out-File ,出现运行时错误Out-File : A parameter cannot be found that matches parameter name 'path'. even though I can output $outfile to the console and see that it's a valid path. 即使我可以将$outfile输出到控制台,并看到它是有效路径。

Is there an additional step (like a close-File or save-file command) I need to take or a different order in which I need to pipe something to get the $content to write to my $outfile ? 是否需要执行其他步骤(如close-File或save-file命令),或者是否需要以其他顺序传递$content以使$content写入$outfile What component am I missing? 我缺少什么组件?

The Out-File cmdlet does not have a -Path parameter, however it does have a -FilePath parameter. Out-File cmdlet没有-Path参数,但是有-FilePath参数。 Here is an example of how to use it: 这是一个如何使用它的示例:

Out-File -FilePath test.txt -InputObject 'Hello' -Encoding ascii -Append;

You will also need to wrap the Get-Content command in parentheses, as it does not have a parameter called -replace . 您还需要将Get-Content命令包装在括号中,因为它没有名为-replace的参数。

(Get-Content -Path $file.Fullname) -replace 'foo','bar';

I'd also recommend adding the -Raw parameter to Get-Content , so that you ensure that you're only dealing with a single line of text, rather than an array of strings (one [String] per line in the text file). 我还建议将-Raw参数添加到Get-Content ,以确保仅处理一行文本,而不是字符串数组(文本文件中每行一个[String]个) 。

(Get-Content -Path $file.Fullname -Raw) -replace 'foo','bar';

There isn't enough information to completely understand what's going on, but here is a filled out example of what I think you're trying to do: 没有足够的信息来完全了解正在发生的事情,但是下面是我认为您正在尝试做的一个完整示例:

# Create some dummy content (source files)
mkdir $env:SystemDrive\test;
1..5 | % { Set-Content -Path $env:SystemDrive\test\test0$_.txt -Value 'foo'; };

# Create the output directory
$OutputPath = mkdir $env:SystemDrive\test02;

# Get a list of the source files
$FileList = Get-ChildItem -Path $env:SystemDrive\test -Filter *.txt;

# For each file, get the content, replace the content, and 
# write to new output location
foreach ($File in $FileList) {
    $OutputFile = '{0}\{1}' -f $OutputPath.FullName, $File.Name;
    $Content = (Get-Content -Path $File.FullName -Raw) -replace 'foo', 'bar';
    Set-Content -Path $OutputFile -Value $Content;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM