Reputation: 2405
I'm having some trouble with trying to add a text file to each directory in this directory having a certain name. I have a directory called Daily Notes
where I save my notes for the day and I have this further organized by week. So I have a bunch of directories in here such as 6.5.17-6.10.17
and in each of these I need a text file with the name format such as 6.7.17DailyNotes.txt
. I know I can easily do this by hand each day, but I have some free time and am trying to learn how to program with cmd. I tried to just make a test text file with a for loop but it saved it to the directory containing the batch file. Here is my code right now:
@echo off
setlocal disabledelayedexpansion
set "folder=%~1"
if not defined folder set "folder=%cd%"
for /D %%a in ("%folder%\*") do (
echo test > test.txt
)
endlocal
So, I want to go into each directory and make 5 text files, one for each day with the format month.day.yearDailyNotes.txt. I was thinking I could just make a variable from reading the directory name and count up in days from that for the text files. Any advice?
Upvotes: 0
Views: 47
Reputation: 56238
Your comment
I know it would be very difficult to handle month changes in the middle of the week, I was thinking it will be acceptable to just handle that part manually, so have a file named something like 10.32.17DailyNotes.txt which I manually will change after
makes it much easier (Date/Time Math in Batch is possible, but ugly and involves a lot of code)
@echo off
setlocal enabledelayedexpansion
REM next two lines for simulating your environment:
set "folder=%~dp0"
md 6.5.17-6.10.17 6.11.17-6.16.17 2>nul
REM for every folder
for /d %%f in ("%folder%*.*.*-*.*.*") do (
REM extract day, month and year [from first part of foldername]
for /f "tokens=1-3 delims=.-" %%a in ("%%~nxf")do (
REM calculate "end day" [may be greater than days in that month]
set /a end=%%b+5
REM for [start] to [end]
for /l %%i in (%%b,1,!end!) do (
REM create blank file
break > "%%f\%%a.%%i.%%cDailyNotes.txt"
)
)
)
Upvotes: 1
Reputation: 80211
@echo off
setlocal disabledelayedexpansion
set "folder=%~1"
if not defined folder set "folder=%cd%"
for /D %%q in ("%folder%\*") do (
echo test "%%q\%%~nxqDailyNotes.txt"
)
ENDLOCAL
Interesting exercise with a couple of twists.
my posted code will simply show 'test "fullfilename" '. Naturally, you could use copy nul "*fullfilename*"' to create an empty file (with
>nulto suppress the creation-report) or
echo.>"fullfilename"` to create a file containing just an empty line - or whatever.
The main magic is with the gymnastics in creating the filename. Since you are using for/d
, the directoryname will appear in %%q
. so the required filename is the name+extension part of that directoryname (you and I know it's actually not a filename but the outermost leafname of a directory tree - just don't tell cmd
) so we use %%~nxq
meaning the name and extension part of the "filename" in %%q
(see for /?|more
from the prompt for documentation). We then just tack the remainder of the required name on as a literal.
So - why the change to %%q
?
Suppose we leave it as %%a
. The filename would then be "%%~nxaDailyNotes.txt" and be interpreted as name,extension,attribute,drive of %%a
since i
is neither a modifier nor a participating metavariable. Changing the metavariable from %%a
to %%q
removes the misinterpretation.
Upvotes: 0
Reputation: 16266
Iterate over all of the subdirectories and create a test.txt file.
set "folder=%~1"
if not defined folder set "folder=%cd%"
FOR /F "usebackq tokens=*" %%a IN (`DIR /S /B /A:D "%folder%\*"`) DO (
ECHO test > "%%~a\test.txt"
)
Upvotes: 0