Adding multiple files with one command - bash

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...

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

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.

Related

for loop command with multiple variables

I am very new to command line and script language, and not so familiar with terms. I will try my best to explain my issue.
I am trying to edit/compress my ebooks in bulk process, and here comes the Calibre.
Calibre is the ebook editing software to make some changes to my epub files.
It's quite feature rich and in most cases easy to use, but only and biggest downside is whenever I edit/covert/read books in the GUI interface I first have to add books in the library, which involves making copy of every file into calibre's predefined directory. and on editor's GUI I can only edit one file at a time.
I have thousands of ebooks to edit, it will take days to do so, it's also bad for my storage space. Big NO NO.
Thankfully, it is possible to compress images in mass on command line interface with the help of useful plugin to asssist with handling bulk files.
for /r "C:\Users\foldername" %v in (*.epub) do calibre-debug -r "Editor Chains" "Compress Images" "%v" "%v.epub"
It works just as expected. it scans all epub files in that folder and its subfolders, and call the plugin to do its job - compress images - and save the output files in the same folder, adding the ".epub" extension to the name of original file name to avoid duplicate.(e.g. this is a book.epub --> this is a book.epub.epub)
Only problem is I have yet to find a way to save the output files in a different folder, with same file name. maybe there's something I am missing or it's just not possible.
If anyone knows how, please let me know.
I've tried:
for /r "C:\Users\foldername" %v & "C:\Users\newfoldername" %x in (*.epub) do calibre-debug -r "Editor Chains" "Compress Images" "%v" "%x"
... and it obviously failed, haha.

Is there a way to show the exact place where two files differ using FC command? or any other good way to compare 2 files in windows?

I'm comparing two files with the command:
FC /W file1.txt file2.txt > log.txt
And I'm getting 4000 lines of differences...
But when it shows the difference, it just paste 3~5 lines from each file and I sorta have to compare them myself to find the place where the difference occured.
Is there a way to make it easier to see where the difference is? Maybe something like adding a flag like "HERE" in one of them where it first differ from the other?
I found this online diff very useful and simple to use... very good error displaying showing too

How to browse for file (Win7/64bits)

I need to quickly write a simple GUI over a command line application. Two steps are required:
Specify an input file path,
Specify an output file path (non existing)
I found out a previous post, however trying to get the above (1) to work seems like an insane task.
Indeed BrowseForFolder seems to only work in some weird cases, you can use BIF_BROWSEINCLUDEFILES for only *.pdf and/or *.txt (trial and errors). I can get an error if I select a *.dll and/or a *.jpg (don't ask).
So instead, I gave up this approach and followed another one, in this case (objIE.Document.all.FileSelect), only the name of the selected file is returned the path seems to be always set to "c:/fakepath" for some reason. So again I am missing the full path to be able to pass that to the command line app.
Is there any sane way (<10 lines of codes) to get (1) and (2) working on Win7/64bits (VBS, HTA...)?
Don't know if people are still interested in the BrowseForFolder file selection issue, but here's what I've found.
I had the same issue selecting files with BrowseForFolder using &H4000 / BIF_BROWSEINCLUDEFILES. I could get a return with .docx but not .doc files and as you say .pdf files.
For me .txt wouldn't return anything, as didn't WMI Backup .rec files that I needed for a script I'm writing, resulting in this error information:-
Error: Unspecified error
Code: 80004005
Source: (null)
After looking at other solutions I came back to this one as my preferred choice, plus it was doing my head in that it didn't want to work. So at the bitter end it seems to be this easy.
To get my .rec files recognized I add this to the registry:-
[HKEY_CLASSES_ROOT\.rec]
#="WMI.Backup"
[HKEY_CLASSES_ROOT\WMI.Backup]
#="WMI Backup"
"BrowseInPlace"="1"
To get .txt files recognized I add this to the registry:-
[HKEY_CLASSES_ROOT\txtfile]
"BrowseInPlace"="1"
So "BrowseInPlace"="1" seems to be the nugget.
Seems so unbelievably easy that I'm sure this solution is out there somewhere but I never came across it so thought I'd put it online.
I would be interested to find that it works for others as I fear that this issue may of sent me mad, still can't believe it seems to work.
Hope this helps.
Here are 3 different ways to do what you want:
http://www.robvanderwoude.com/vbstech_ui_fileopen.php

Find completely commented files

In a solution with lots of files and projects - how would you find all completely commented files? I assume that every line of code starts with // (EDIT: or is empty) in such files.
I am using VS 2008, C#, ReSharper is available.
I know, normally such files should not exist - that's what a source safe is for ...
To find all files in and under the current directory in which all lines begin with '//':
find . -type f -exec sh -c 'grep -vq "^//" {} || echo {}' \;
Note that this will report empty files.
The argument to grep can easily be expanded to account for whitespace, or generalized to match an arbitrary regex.
There is no way to achieve this with a simple search style with the components you've mentioned. Doing this would require a bit of interpretation on the file but could be done with a fairly simple script.
It sounds like you're looking for files without code though vs. files with all comments. For example if there are 1000 lines where 900 are commented and 100 are blank, it seems to meet your criteria.
The script should be fairly straight forward to write but you would need to look out for the following weird cases
Block comments
if blocks which are always false. For example #if 0
Empty lines
Well, you could write a program (probably a console app) to recursively walk the directory and file tree. Read in all .cs files and check each line to see if its first non-space and non-tab characters are "//". If you wanted to get really fancy, you could count the total lines and the lines with "//" and display the percentages so you could catch files that didn't have absolutely every line commented out. You'll just need to understand a little bit about System.IO to get the files and string functions to look for the characters you are looking for. That should cover it.
This should be close to what you're looking for: http://www.codeproject.com/KB/cs/csharplinecounter.aspx
Look for the method in the project that determines if a line is commented or not, and you can use that to build a count, etc.

Are there any invalid linux filenames?

If I wanted to create a string which is guaranteed not to represent a filename, I could put one of the following characters in it on Windows:
\ / : * ? | < >
e.g.
this-is-a-filename.png
?this-is-not.png
Is there any way to identify a string as 'not possibly a file' on Linux?
There are almost no restrictions - apart from '/' and '\0', you're allowed to use anything. However, some people think it's not a good idea to allow this much flexibility.
An empty string is the only truly invalid path name on Linux, which may work for you if you need only one invalid name. You could also use a string like "///foo", which would not be a canonical path name, although it could refer to a file ("/foo"). Another possibility would be something like "/dev/null/foo", since /dev/null has a POSIX-defined non-directory meaning. If you only need strings that could not refer to a regular file you could use "/" or ".", since those are always directories.
Technically it's not invalid but files with dash(-) at the beginning of their name will put you in a lot of troubles. It's because it has conflicts with command arguments.
I personally find that a lot of the time the problem is not Linux but the applications one is using on Linux.
Take for example Amarok. Recently I noticed that certain artists I had copied from my Windows machine where not appearing in the library. I check and confirmed that the files were there and then I noticed that certain characters in the folder names (Named for the artist) were represented with a weird-looking square rather than an actual character.
In a shell terminal the filenames look even stranger: /Music/Albums/Einst$'\374'rzende\ Neubauten is an example of how strange.
While these files were definitely there, Amarok could not see them for some reason. I was able to use some shell trickery to rename them to sane versions which I could then re-name with ASCII-only characters using Musicbrainz Picard. Unfortunately, Picard was also unable to open the files until I renamed them, hence the need for a shell script.
Overall this a a tricky area and it seems to get very thorny if you are trying to synchronise a music collection between Windows and Linux wherein certain folder or file names contain funky characters.
The safest thing to do is stick to ASCII-only filenames.

Resources