0

I've already found most of the answers, but I still can't use the batch file to create what I need.

  1. I need to create folders according to the list in folders.txt.
  2. Create an item.cs.md file in the folders.
  3. Write the content from the content.txt file to the file one by one.

Folders are already creating me files as well, but the content is always filled with just the one from the underline content.txt line.

@echo off
SETLOCAL

for /f "delims=" %%l in (content.txt) do (
for /f "tokens=1" %%a in (folders.txt) do (
if not exist "%%a" mkdir "%%a"

ECHO %%l>%%a\item.cs.md

))

Thanks for your time and tips.

Documentation: How to create multiple text files each with different names using a batch file? create folders from each line of a textfile in bat How to create folders and files from text file with same names to insert with corresponding name? Batch File To Copy Every Line In .txt File And Then Create New .txt File For Each Line

  • To separate commands on a line, use `&`. How does `cmd` know that `SET LINENO=1 for /f "delims=" %%l in (content.txt) do (` means `SET LINENO=1` then `for /f "delims=" %%l in (content.txt) do (` You *may* mean what you have written. Use `set "var1=data"` for setting **STRING** values - this avoids problems caused by trailing spaces. Quotes are not needed for setting arithmetic values (`set /a`) – Magoo Apr 18 '22 at 21:15
  • Thanks a lot, I understand that until the last command set "var1=data" where then does the entry of this fragment belong? – Daniel Deight Apr 18 '22 at 21:35
  • Whenever you `set` a variable to a string value, use the syntax `set "var1=data"`. That syntax ensures that any trailing spaces on the `set` line are not included in the value assigned to the variable `var`. Such trailing spaces make a script hard to debug. Once bitten, twice shy. If you are using a numeric `set`, then the quotes are not required.; eg. on your `SET /A LINENO=LINENO+1` line (probably better written `SET /A LINENO+=1`). Since the value assigned to `lineno` is numeric, you could use `SET /A LINENO=1 *BUT* you are not actually using `lineno` in your post. see `set /?` for more – Magoo Apr 18 '22 at 22:07
  • I would anticipate that you propose to use `lineno` somewhere - so perhaps we can pre-empt problems by referring you to Stephan's DELAYEDEXPANSION link: https://stackoverflow.com/a/30284028/2128947. And note that `>` creates a new file every time it is executed - so only the last value written will remain in the file if you are executing it in a loop as you indicate. Use `>>` to create or append to a file. Note that if the file already exists, `>>` will append to it, so you may need to delete it before your loop. – Magoo Apr 18 '22 at 22:08
  • My mistake I understand LINEO should have been used in the filename to create files 1, 2, 3, but there are none so there is no need anymore. But what still doesn't work for me is the content, so if content.txt encloses line 1 = a, line 2 = b, all files always contain the character "b", always the last one, I try to fix the script so that 1 = 1 2 = 2 , I still can't. I'm sorry, I'm a beginner. – Daniel Deight Apr 18 '22 at 22:16
  • Your code reads the first file and assigns `a` to `%%l`.It then reads each line of the second file, assigning that line's contents to `%%a`, creating the directory and then writes the contents of `%%l` to the file within the directory. Then it reads the next line (`b`) to `%%l` and repeats the process. Because you are using `>`, a new file is created which replaces the file created in the first loop. If you want to **APPEND** to the file, then use `>>` in place of `>` – Magoo Apr 18 '22 at 22:49
  • I am close and i understand exactly what you think, but I still can't get the right solution : ">>" I just filled in the content of everything, ie all the lines from the first to the first file, the second line to the second file. I come very stupid and thank you very much for your help. – Daniel Deight Apr 20 '22 at 16:56
  • If I understand your requirement correctly, just `for /f "tokens=1" %%a in (folders.txt) do (md "%%a" 2>nul & copy "content.txt" "%%a\item.cs.md")` should do what you want. – Stephan Apr 21 '22 at 10:41
  • Everything as before. There is still either the entire content or the last character in the content, it does not select lines as contents into individual files. – Daniel Deight Apr 24 '22 at 11:31
  • It seems that only delims= is missing in the notation, but I can't write it correctly in the command. – Daniel Deight Apr 24 '22 at 18:54

0 Answers0