The general approach is to establish a filename-lister
for /f "delims=" %%b in ('dir /b /s /a-d filemask') do (
)
This executes the dir
command, ignoring directorynames (/a-d
) for files matching the filemask (*.md
in your case), scanning subdirectories (/s
) and listing in basic form (/b
) that is, without headers, footers or details - names only. A list is built in memory of all of the files in the tree and then each filename found is assigned to %%b
in turn.
The delims=
turns off the default for /f
processing into tokens (see for /?
from the prompt for documentation - or hundreds of SO responses) so the entire filename is assigned to %%b
I prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables)
ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs
(See `for/f` from the prompt for documentation)
Since %%b
contains the full absolute path to the file, it may contain spaces or other confounding characters, so best to "quote the filename"
when used. which overcomes most problems.
Next step would be to create a temporary file containing your required prefix data as trying to insert at the start of a file is dicey.
echo --->tempfile.###
echo tags: unpolished>>tempfile.###
echo aliases:>>tempfile.###
echo cssclass:>>tempfile.###
echo --->>tempfile.###
would do that - note that there is only ONE >
in the first of those lines. This starts a new file, so if there's an existing tempfile.###
, it will be overwritten. The >>
appends to the file.
BUT
That's highly inefficient as each echo
statement opens, writes and closes the file.
(
echo ---
echo tags: unpolished
echo aliases:
echo cssclass:
echo ---
)>tempfile.###
opens, writes and closes just once - and is easier to read and maintain as well. Note that the echo
es are contained between parentheses (…)
.
Then append your selected file in %%b
to the tempfile
type "%%b">>tempfile.###
so the tempfile then contains the preamble data and the original file,
and then replace the original using move
tempfile original.
move tempfile.### "%%b"
Since this is a batch file, the /y
switch to bypass confirmation of overwriting a file is not required, but there's no harm in including it.
move /y tempfile.### "%%b"
this will generate messages which can be suppressed by appending >nul
and/or 2>nul
to the move
command line. >nul
suppresses normal messages (1 file(s) moved
) and 2>nul
suppresses error messages.
And there you have it. Within the filename-lister, create the preamble in the tempfile, append the original to the tempfile and move the resultant tempfile over the original.
obligatory warning: Always verify against a test directory before applying to real data.
Useful extension:
If you run this process more than once, each file will acquire another copy of the preamble.
findstr /x /c:"tags: unpolished" "%%b" >nul
if errorlevel 1 (
rem did not fine "tags: unpolished" as a line in the file, so insert it
...
) else (echo skipping "%%b" - already processed)
will attempt to find a line in the file %%b
which exactly (/x
) matches the constant string (/c:string
) and set errorlevel
to 0
if FOUND, hence don't process or non-0 if not found, hence process.