MackMan
MackMan

Reputation: 129

Have a batch create text files based on a list

I've got to create a text file for each PC at my workplace is there a way to have a batch read from a list of PCs in a text file and create a text file for each line of text?

Ex. Text File

    Computer1
    Computer2
    Computer3

I want it to create a computer1.txt, computer2.txt, and computer3.txt, but for a list of about 400 PCs... It seems pretty easy I'm just not sure how to link batch files together. I'm learning! :) Thanks for all your help, as always!

Upvotes: 1

Views: 8050

Answers (3)

Nathan
Nathan

Reputation: 5204

I had a similar but different requirement, which was that my file had two columns of data, comma separated.

First col: the file name I wanted

Second col: the data for the contents of that file (HTML)

i.e.:

asdf.html, <p>Text about this</p>
bcvadf.html, <p>More text</p>

So this was what I put in cmd:

FOR /F "tokens=1,2 delims=," %G in (file.csv) DO echo %H > D:\temp\%G

Explanation:

  • %G relates to 'token' (column) 1, %H column 2
  • 'echo' tell it was to write, in this case column 2 (the content within the file)
  • ">" actually creates the file/s
  • "> D:\temp\%G" creates the file with the name in the 1st column

Further understanding and examples, as in crownedzero's answer, https://ss64.com/nt/for_f.html

Upvotes: 0

Matt Williamson
Matt Williamson

Reputation: 7095

@echo off
setlocal

for /f "tokens=*" %%a in (comps.txt) do (type nul>"%%a.txt")

To add a pre-defined text to the file, do something like this:

for /f "tokens=*" %%a in (comps.txt) do (
  echo This is line 1 of text>"%%a.txt"
  echo This is line 2 of text>>"%%a.txt"
  echo This is line 3 of text>>"%%a.txt"
  echo This is line 4 of text>>"%%a.txt"
)

Upvotes: 5

crownedzero
crownedzero

Reputation: 506

http://ss64.com/nt/for_f.html

FOR /F ["options"] %%parameter IN (filenameset) DO command 

See also: Batch script to read input text file and make text file for each line of input text file

Upvotes: 0

Related Questions