I have a script that duplicates a Word doc template and renames each using a csv file with a list of names. What would be the equivalent code if instead of a csv file I use a text file?

Import-csv ‘.\individuals2.csv’ | foreach-object {
	$newname = ‘2 ‘ + $_.name + '.docx’
	Copy-item '.\template.docx' $newname
}
  • Kissaki@beehaw.org
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    3 months ago

    A csv file is a text file. Where the data in text form is in a specific form.

    So your question doesn’t really make sense. You’re already using a text file, and you don’t disclose what would be different.

    • mikyopii@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      3 months ago

      His example doesn’t seem to perform any comparisons to determine what to name the file. If the text file is just a list then something like this could work:

      $file = Get-Content -Path .\individuals2.txt

      foreach ($line in $file) { Copy-Item -Path .\template.docx -Destination “.$(2 + $line + ‘.docs’)” }

      Edit: Wow, Lemmy destroys formatting.

      • brisk@aussie.zone
        link
        fedilink
        arrow-up
        0
        ·
        3 months ago

        It’s markdown, you should be able to indent your lines by 4 spaces or fence with triple backtics to get code blocks. Your client’s editor may have a button to help

        ```
        $file = Get-Content -Path .\individuals2.txt
        
        foreach ($line in $file)
        {
            Copy-Item -Path .\template.docx -Destination ".\$(2 + $line + '.docs')"
        }
        ```
        

        Becomes

        $file = Get-Content -Path .\individuals2.txt
        
        foreach ($line in $file)
        {
            Copy-Item -Path .\template.docx -Destination ".\$(2 + $line + '.docs')"
        }
        
      • giloronfoo@beehaw.org
        link
        fedilink
        arrow-up
        0
        ·
        edit-2
        3 months ago

        The confusion is that CSV is also just a Notepad text file that uses Commas to Separate Values.

        They are asking about what kind of values are in it.