将所有 .txt 文件内容转换为小写的脚本

script to convert all .txt file content to lowercase

我有大约 700 个 .txt 文件分散在 300 个目录和子目录中。

我想分别打开,把里面的文字全部转成小写,包括Unicode字符(比如Éé),然后保存关闭。

你能告诉我如何通过 PowerShell 完成吗?这是我自己的电脑,我有管理员权限。

我从以下开始:

Get-ChildItem C:\tmp -Recurse -File | ForEach-Object {}

但我不确定 ForEach-Object {}.

的括号内应该放什么

您需要使用:

# Reading the file content and converting it to lowercase and finally putting the content back to the file with the same filename.
(Get-Content C:\path\file.txt -Raw).ToLower() | Out-File C:\path\file.txt -Force

在 foreach 中,然后将大小写更改为小写。

如果你想迭代相应文件夹中的所有文件,那么你可以使用另一个 foreach 来完成这项工作。

希望对您有所帮助。

简单的脚本,符合您的要求:

$path=".\test\*.txt"
#With Default system encoding
Get-ChildItem $path -Recurse | foreach{    
    (Get-Content $_.FullName).ToLower() | Out-File $_.FullName
}

#Or with specified encoding    
Get-ChildItem $path -Recurse | foreach{    
(Get-Content $_.FullName -Encoding Unicode).ToLower() | 
    Out-File $_.FullName -Encoding Unicode
}
#Test
Get-ChildItem $path -Recurse | foreach{
    Write-Host "`n File ($_.FullName): `n" -ForegroundColor DarkGreen
    Get-Content $_.FullName    
}