0

I have csv file with list of filepaths:

Filename 
C:\Users\postgres\1.tmp 
C:\Users\postgres222\2.txt
C:\Users\postgres3333\3.jpeg

I would like to loop through that list and to create in the same directory txt file per every file with below information: Today's date Filepath Name of file

so in example for 1st file it should be file C:\Users\postgres\1.tmp.txt with data:

03\11\2021
C:\Users\postgres\1.tmp 
1.tmp

I tried:

$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}
2
  • 3
    Show us what you've tried. Commented Nov 3, 2021 at 13:01
  • updated original Commented Nov 3, 2021 at 13:22

1 Answer 1

2

Just use Import-Csv on the input file and loop through the data with ForEach-Object.
Inside the loop, split the fullname into the path and the filename only:

$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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.