我有帶有檔案路徑串列的 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}
uj5u.com熱心網友回復:
只需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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347913.html
上一篇:從范圍中間開始并回圈的For回圈
