powershell - 文件名到 CSV
问题描述
#get filenames from txtfile
foreach ($file in Get-Content -Path "C:\Filelist.txt")
{
#Remove : and replace with Underscores
$name = ( $file) -replace '\\', '_' -replace ':'
#Create new folder at destination location with new filename
New-Item -Path "\\destinationpc\$name" -ItemType Directory -ErrorAction SilentlyContinue
###Next step How would one create a csv with 2 columns, the original name and new name?
}
下一步我遇到了困难,如何创建一个包含 2 列、原始名称和新名称的 csv?
解决方案
Get-Content C:\Filelist.txt |
ForEach-Object {
$name = $_ -replace '\\', '_' -replace ':'
# Create new folder at destination location with new filename
New-Item -Path "\\destinationpc\$name" -ItemType Directory -ErrorAction SilentlyContinue
# Output an object with properties reflecting the original and the new name
[pscustomobject] @{
OriginalName = $_
NewName = $name
}
} |
Export-Csv out.csv -NoTypeInformation -Encoding utf8
注意:在 PowerShell [Core] v6+中,-NoTypeInformation
和-Encoding utf8
都是隐含的;在Windows PowerShell中,默认字符编码是 ASCII(!),并且utf8
总是创建一个带有 BOM的 UTF-8 文件。
推荐阅读
- python - 有没有办法检查你正在运行什么 python 程序?
- javascript - 元素在面板中,但无法从 js 或控制台访问
- java - 通过 YAML 文件加载基于抽象类的对象
- xamarin - 无法在 Monodevelop 中编译程序
- python - 如何计算坐标之间的成对harsine距离
- php - 在 Wordpress 中记录 PHP 的 mail() 函数
- javascript - 为什么three.js投射阴影不适用于3D模型
- javascript - 如何检查元素是否出现在while循环中
- android - 在 ImageView 中使用 TextView 创建 ScrollView 时遇到问题
- htmx - 如何拥有嵌套的选择下拉菜单,为主列表和 htmx 的依赖列表获取不同的内容?