How to remove words from all text files in a folder?

不想你离开。 提交于 2020-01-15 04:59:10

问题


I have a code like this:

$txt = get-content c:\work\test\01.i

$txt[0] = $txt[0] -replace '-'

$txt[$txt.length - 1 ] = $txt[$txt.length - 1 ] -replace '-'

$txt | set-content c:\work\test\01.i

It just removes a - from first line and last line in a text file, but I need to do this for all text files in the directory tree which contains over 5k text files. how should I modify this code?

All name of text files are random.

I need to do this in powershell.

Its a directory tree, under c:\work\test there will be more lvl of sub-folders then contains all text files.

Please help . thanks


回答1:


Get all files by file extension or something a pattern and loop through them. Lets say it's all the files with .i extension. Try:

Get-ChildItem -Path "c:\work\test" -Filter *.i -Recurse | where { !$_.PSIsContainer } | % { 
    $txt = Get-Content $_.FullName; 
    $txt[0] = $txt[0] -replace '-'; 
    $txt[$txt.length - 1 ] = $txt[$txt.length - 1 ] -replace '-';
    $txt | Set-Content $_.FullName
    }


来源:https://stackoverflow.com/questions/14442229/how-to-remove-words-from-all-text-files-in-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!