-2

I tried to give a sample of a list of my titles. Here it is:

'197. Module Introduction.txt' '198. Our Starting Setup.txt' "199. What's So Complex About Forms.txt" '200. Dealing With Form Submission & Getting User Input Values.txt' '201. Adding Basic Validation.txt' '202. Providing Validation Feedback.txt' '203. Handling the "was touched" State.txt' '204. React To Lost Focus.txt' '205. Refactoring & Deriving States.txt' '206. Managing The Overall Form Validity.txt' 'Assignment 5: Time to Practice: Forms.txt' '207. Adding A Custom Input Hook.txt' '208. Re-Using The Custom Hook.txt' '209. A Challenge For You.txt' '210. Applying Our Hook & Knowledge To A New Form.txt' '211. Summary.txt' '212. Bonus: Using useReducer.txt' '213. Module Resources'.txt

All those titles were in a text file named lectures.txt.

I wanted to create a series of titled blank text files in a folder I had already created to receive them. Each text file should have the specific titles as in that example list. Ultimately I would be creating nearly 500 files, but all of them would have very specific meaningful titles.

I tried to follow all the instructions I found on this forum and elsewhere, on a very long search for help. None of them directly addressed my problem.

They all gave answers for creating multiple files with meaningless names. I don't see the utility in creating a bunch of files named 1.txt, etc, or a.txt, or some combination with a leading standard meaningless lead like filename, or sample...

I may be wrong about that lack of usefulness, but at the very least, it's not what I tried to ask and get an answer for.

I already have the meaningful filenames I want, a lot of them. I want to create many empty text files with names that I already have.

The only remotely useful suggestion (for me) that I got was using touch from a linux or bash prompt.

I found that if I wrapped my titles in quotations and separated the titles by a single space I could get the touch command to kind of work. I also discovered that my file titles could not contain any forward slashes ("/"). Nobody explained to me that the titles had to be wrapped in quotes. I figured that out quickly. Nobody told me how to separate the filenames. I experimented until I found a separation that worked.

I tried some experimenting with the {} bracing and the touch command but wound up not being able to figure out how that could help me in my particular case.

Also, I don't know if it has to do with a buffer on the bash or in PowerShell (I tried using both the Git installation in Windows 10 and bash from PowerShell 7.2.1, but I found that only about seven titles in the list would be touched. The rest resulted in a command not found error.

I broke up the list into about seven titles each and ran touch on each of those filenames (my titles) list segments. That way, on that very list sample I display above from my lectures.txt file, I got my empty text files created successfully, even though I had to do it in three touch commands.

I may have confused some of the people who tried to help me by putting all the titles in a text file that I named lectures.txt. The contents of that file were the titles I wanted for my text files. Thank you so much those who did reach out to me.

I had already found something that might work in bash but I couldn't get it to work. It was roughly on the line of

`touch prefix{1..3}.txt`

This had the same problem that all the other command suggestions I found had as well, like

echo > filename.extension

The same for the apparently most popular

for /l %a in (1 1 10) do type nul > "%a.txt"

which created ten files named 1.txt through 10.txt. That was not the least bit useful to me.

I know my question got voted down as being exceptionally bad, but I'm editing and adding what I found out, so maybe sort of answering my own question.

If you are looking to do what I'm trying to do, and not just create meaningless filenames, I hope you will find some of what I did helpful. Here's a list of suggested solutions of which NONE did I find useful.

https://techpp.com/2021/08/22/create-file-using...

https://www.quora.com/Can-you-create-multiple-f...

https://www.howtogeek.com/725207/how-to-create-...

How to create multiple empty files on cmd(Windows).

Create multiple files with Powershell?.

https://www.laptopmag.com/how-to/create-multipl...

user258081
  • 301
  • 2
  • 8
  • please update the question with a sample of your inputs (`cat lectures.txt`), the code you've tried, the (wrong) output generated by your code and the (correct) expected output (corresponding to the sample input) – markp-fuso Feb 09 '22 at 18:31
  • The `touch` command is used for changing the file systems date and time of a file. I am not understanding how you think that applies to your problem. – Squashman Feb 09 '22 at 18:46
  • Please go back and take the [tour] and read [ask] a good question. You should always provide a [mcve] of the code you are using and any input used for your code. – Squashman Feb 09 '22 at 18:48
  • I got the information from here: https://askubuntu.com/questions/605558/how-to-create-multiple-files-with-the-terminal – user258081 Feb 09 '22 at 19:19
  • it is basically stated as touch bspl{0001..0003}.c where the bspl was a prefix to the filenames, the {} brace expansion let you sequence through a set of numbers (here three), and the .c was the file extension (I would use .txt presumably). – user258081 Feb 09 '22 at 19:26

2 Answers2

0

Remember that using space in bash is not recommended, but if you need to...

You can try something like that:

counter=1
for title in $(cat lectures.txt)
do
  touch "$counter - $title.txt"
  counter=$((counter+1))
done

EDIT

I only added the title on lectures.txt

NEW EDIT

If lectures.txt has 1 title per line you can do this:

while IFS= read -r line || [ -n "$line" ]
do
  touch "$line"
done < lectures.txt
Juranir Santos
  • 370
  • 2
  • 6
  • So, how should I add all the other file titles? do I add them to cat? Since they have spaces do I wrap the titles in quotes? How do I separate the title names in cat, with commas? spaces? Thanks for your suggestion. – user258081 Feb 09 '22 at 21:02
  • Maybe I don't understand well. What exact exist on lectures.txt? Exist only 1 file? multiples? – Juranir Santos Feb 09 '22 at 21:15
  • Sorry, yes, I put all the names of the files in that lectures.txt file. It is what you see in the code block that I provided. So, inside the lectures.txt file is where I put the touch command and the full list of title names each wrapped in single quotes and separated from each other by a space between them. I guess what I need to understand is the format needed of the lectures.txt file in the cat statement. And if I am giving the code you suggested from a bash prompt. I do need a lot more experience using bash I know. I used to write batch files and run them from the old cmd prompt. – user258081 Feb 10 '22 at 13:00
  • maybe we can change your logic: Remove touch command and the extension from the file lectures.txt. (keep only file name) If you file already have the number, remove count variable. The rest is the same – Juranir Santos Feb 10 '22 at 14:12
  • I'm not sure I understand you. For example, I just put one title in it. '214. Module Introduction'. I don't know exactly what the format has to be for the titles, if to wrap them in quotes and how to separate each title. Running for title in $(cat lectures); do touch "title.txt"; done returns a bunch of files with the .txt extension, each of them named for each word in the title. So, 215.txt Data.txt Meals.txt Moving.txt To.txt 214.txt Backend.txt Introduction.txt Module.txt The.txt (and of course the original lectures file that was in the directory). – user258081 Feb 16 '22 at 16:11
  • You, Juanir Santos, were the most helpful responder. My problem may have been poorly stated. I had no code that was wrong because I had no idea how to do what I wanted. I had a list of filename titles already. I did not want or need to create multiple files with meaningless names, but the touch command seemed the most promising. I tried removing the touch command and the .txt extension from my lectures.txt file and then ran for title in $(cat lectures) do touch "$title.txt" done but that returned text files for every single word in the entire list of titles in the lectures file. – user258081 Mar 02 '22 at 04:40
  • Please check my last update. – Juranir Santos Mar 02 '22 at 12:24
  • Juranir, I tried your edited code: while IFS= read -r line || [ -n "$line" ] do touch "$line" done < lectures.txt – user258081 Mar 05 '22 at 16:26
  • Juranir, I tried your edited code: while IFS= read -r line || [ -n "$line" ] do touch "$line" done < lectures.txt from the Git bash in the folder where I first put the lectures.txt file All the created files have a strange looking bullet at the end of the name and have no txt extension, so Notepad won't open them. The last file name in the list got no bullet added to its end. – user258081 Mar 05 '22 at 16:38
0

Get-Content -Path '.\lectures.txt' | ForEach-Object { New-Item -Path ".\$($_)" }

or using aliases it's: gc '.\titles.txt' | % { ni ".\$($_)" }

from a powershell console at the required folder and enter the command.

user258081
  • 301
  • 2
  • 8
  • Juranir Santos' last edited answer using touch from a Git bash also worked but I'm still trying to figure out why I'm getting a bullet after the titles and no .txt extension. – user258081 Mar 06 '22 at 14:35