1

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!

MackMan
  • 129
  • 3
  • 15

3 Answers3

5
@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"
)
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
  • Thanks again! That does it! Only thing I had issues with was if I was to type different text/numbers I would have to do Number0 >>%%a.txt" putting a space between the number 0 and >>. Other than that it works like a charm. Thank you! – MackMan Nov 01 '13 at 18:36
  • `>>"%%a.txt" echo(This is line 1 of text` <-- change the order of redirection to eliminate the need for a space in every line. The `(` also protects against issues with certain characters at the start of the line – foxidrive Nov 02 '13 at 01:17
0

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

Community
  • 1
  • 1
crownedzero
  • 496
  • 1
  • 7
  • 18
0

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

Nathan
  • 4,358
  • 2
  • 10
  • 26