this post was submitted on 28 Mar 2024
4 points (100.0% liked)

Programming

13362 readers
1 users here now

All things programming and coding related. Subcommunity of Technology.


This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.

founded 1 year ago
MODERATORS
 

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
}
top 10 comments
sorted by: hot top controversial new old
[–] [email protected] 5 points 7 months ago* (last edited 7 months ago) (1 children)

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.

[–] [email protected] 1 points 7 months ago

Oh I see. I thought only .txt files are text files.

I was trying to adjust the code so that it uses a .txt file instead of a .csv file.

[–] [email protected] 2 points 7 months ago (2 children)

What's the format of the text file?

[–] mikyopii 3 points 7 months ago* (last edited 7 months ago) (2 children)

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.

[–] [email protected] 4 points 7 months ago (1 children)

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')"
}
[–] mikyopii 1 points 7 months ago

That's good to know. Thanks!

[–] [email protected] 1 points 7 months ago

Thanks for your help, but I had a couple errors pop up when I tried this out. I don't need to input anything in the code you provided, do I?

Cannot convert value "VALUE" to type "System.Int32". Error: "Input string was not in a correct format."
At line:4 char:55
+ ... py-Item -Path .\template.docx -Destination ".\$(2 + $line + '.docs')"
+                                                     ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

Copy-Item : Cannot overwrite the item FILENAME with itself.
At line:4 char:5
+     Copy-Item -Path .\template.docx -Destination ".\$(2 + $line + '.d ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (FILENAME) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand
[–] [email protected] 1 points 7 months ago (1 children)

It's just a Notepad text file.

[–] [email protected] 1 points 7 months ago* (last edited 7 months ago) (1 children)

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.

[–] [email protected] 1 points 7 months ago

Just words? It's a list of names.