问题
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!
回答1:
@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"
)
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/19732198/have-a-batch-create-text-files-based-on-a-list