[英]Extract Data from .txt files and export it to a .csv file #powershell
[英]powershell create txt files based on csv file
我有帶有文件路徑列表的 csv 文件:
Filename
C:\Users\postgres\1.tmp
C:\Users\postgres222\2.txt
C:\Users\postgres3333\3.jpeg
我想遍歷該列表,並在每個文件的同一目錄中創建 txt 文件,其中包含以下信息:今天的日期 Filepath 文件名
所以在第一個文件的例子中,它應該是帶有數據的文件C:\\Users\\postgres\\1.tmp.txt
:
03\11\2021
C:\Users\postgres\1.tmp
1.tmp
我試過:
$Time=Get-date
$data = "\mydir"
foreach($file in Get-ChildItem $data){New-Item -ItemType file -Path $Get-Item $file.Fullpath + ".txt" -Value $Time Add-Content $Get-Item $file.Fullpath}
只需在輸入文件上使用Import-Csv
並使用ForEach-Object
循環遍歷數據。
在循環內,將全名拆分為路徑和文件名:
$today = '{0:dd\\MM\\yyyy}' -f (Get-Date)
(Import-Csv -Path 'X:\Path\Folder\Input.csv').Filename | ForEach-Object {
$path = [System.IO.Path]::GetDirectoryName($_) # or use: Split-Path $_ -Parent
$file = [System.IO.Path]::GetFileName($_) # or use: Split-Path $_ -Leaf
# create the full path and filename for the output
$out = Join-Path -Path $path -ChildPath ('{0}.txt' -f $file)
# construct the three lines and write the file
"$today`r`n$_`r`n$file" | Set-Content -Path $out
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.