Tech Support > Operating Systems > MS-DOS > .bat to make folders
.bat to make folders
Posted by benpak@gmail.com on February 7th, 2006


how would you create a .bat file, that creates folders according to the
number of days in the month. so that when you run it, it will create
say 30 folders with the labels march-01-2006 through march-30-2006. is
it possible?

Posted by foxidrive on February 7th, 2006


On 7 Feb 2006 07:07:07 -0800, benpak@gmail.com wrote:

Try this snippet - it will need to be expanded.
if you have further questions just ask.


@echo off
set mon=march
set yr=2006
for %%a in (0 1 2 3 4 5 6 7 8 9) do MD %mon%-1%%a-%yr%

Posted by thomasanderson on February 7th, 2006


That really helped me out. But its still not working quite right.
i get this error repeated:
A subdirectory or file c:\jmpak\march already exists.
Error occurred while processing: c:\jmpak\march.
A subdirectory or file _2006 already exists.
Error occurred while processing: _2006.

this is the code i put in the .bat file

@echo off
set mo=march
set yr=2006

MD c:\jmpak
MD C:\jmpak\%mo%

for %%a in (1 2 3 4 5 6 7 8 9) do MD c:\jmpak\%mo%\0%%a_%mo%_%yr%
::for %%b in (0 1 2 3 4 5 6 7 8 9) do MD %mo%-1%%b-%yr%
::for %%c in (0 1 2 3 4 5 6 7 8 9) do MD %mo%-2%%c-%yr%
::for %%d in (0 1) do MD %mo%-1%%d-%yr%

pause

------end------


what am i doing wrong?


Im new to .bat files so i dont really understand what the capabilities
are. but i was wondering if it is terribly hard to prompt for a user
input (i.e. enter month: ) then the variable is input into %mo%

i hope this made some sense.

Thank you so much for all your help.

Posted by Dr John Stockton on February 7th, 2006


JRS: In article <1qcpw0sxu0iq1$.7m91tpdivdb2.dlg@40tude.net>, dated
Wed, 8 Feb 2006 03:00:31 remote, seen in news:alt.msdos.batch, foxidrive
<woohoo@gotcha.invalid> posted :
I guess you're American. Over here in Europe we're expecting to have 31
days in March; the difference should confuse travellers.

You should name the folders ...YYYY-MM-DD... for ease of subsequent
use.

Not clear how you expect it to determine, or use, the number of days in
the month.

For on-topic and compatible systems,

NOWMINUS z4 n-1 d1 b9 l2 vEOM

will set environment EOM to the number of days in the current civil
month, allowing for Leap Years; it can be adjusted if necessary to do
the next month, or the month containing the day a fortnight ahead of
now. Add b6 l2 vMON b1 l4 vYEAR to set month and year variables.

NOWMINUS via sig line 3.

Write a file something like a combination of what's above to write for
days 01-09 10-19 20-28 and conditionally 29 30 and 31 depending on
length.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Posted by Todd Vargo on February 7th, 2006



"thomasanderson" <benpak@gmail.com> wrote in message
news:1139342638.006313.153230@g47g2000cwa.googlegr oups.com...
You are running the batch multiple times (once for each FOR command).
Provided the batch is not halting on the error message, there is nothing
wrong with presenting the error message. You could remove the :: and run the
batch only once or include IF EXIST commands to each MD command.

Note, the three :: lines do not have the MD command formed as in the first
FOR command and your fourth :: line creates 10 and 11 not 30 and 31.

This may seem like a dumb question, but why create all these folders? How
many files will actually reside in these folders? What OS is this for?

--
Todd Vargo (double "L" to reply by email)


Posted by thomasanderson on February 8th, 2006


theres like 3 files in each of the folders.
the OS is winXP pro
and i just need to do it for some documentation i do everyday.

Posted by Harlan Grove on February 9th, 2006


Dr John Stockton wrote...

So the bit "snippet - it will need to be expanded." is unclear? OK,
maybe that means adding the additional lines

for %%a in (0 1 2 3 4 5 6 7 8 9) do MD %mon%-2%%a-%yr%
for %%a in (0 1 2 3 4 5 6 7 8 9) do MD %mon%-%%a-%yr%
MD %mon%-30-%yr%

or if the OP just made a typo and wants 31 folders, replace that last
line with

for %%a in (0 1) do MD %mon%-3%%a-%yr%

Of course this is just a guess.


Posted by thomasanderson on February 9th, 2006


yea i did that wrong, but when i change it the same errors come up that
the folder already exists. but the prog runs and makes all the files,
just the files are not named correctly. and not in the correct place.

Posted by Harlan Grove on February 9th, 2006


thomasanderson wrote...
Context is useful, even when you're following up.

Many of the MD commands shown in this thread lack drive/directory
paths, so that would mean that they'd create subdirectories in the
current working directory. Put the commands

dir /a:d
pause

in the batch file above the MD commands. Does it show any pre-existing
directories you're trying to create?

On the other hand, if you're trying to create subdirectories in a
different drive/directory than the current working directory, you
*must* precede the directory name with enough of the intended
drive/directory path to identify it (it's a good idea just to use the
full drive/directory path in such cases).


Posted by Todd Vargo on February 10th, 2006



"thomasanderson" <benpak@gmail.com> wrote in message
news:1139424791.336660.30060@g47g2000cwa.googlegro ups.com...
alt.msdos.batch.nt would be a better place to ask this.

Well, if creating a folder each day is too much work, then running the batch
once each month will be too. Why not shoot for once a year? Note, output is
set to year_month_day format. Modify last two lines to suit your needs.

@echo off
setlocal
set yr=2006

for %%? in (January February March April) do call :getdays %%?
for %%? in (May June July August) do call :getdays %%?
for %%? in (September October November December) do call :getdays %%?
goto :eof

:getdays
set mo=%1
for /l %%? in (1,1,9) do call :MD 0%%?
for /l %%? in (10,1,28) do call :MD %%?
if /i %mo:~0,3% equ Feb goto :isleap
call :MD 29
call :MD 30
for %%? in (Apr Jun Sep Nov) do if /i %mo:~0,3% equ %%? goto :eof
call :MD 31
goto :eof

:isleap
set /a isleap=%yr% %% 4
if %isleap% equ 0 call :MD 29
goto :eof

:MD
if exist c:\jmpak\test\%yr%_%mo%_%1 goto :eof
md c:\jmpak\test\%yr%_%mo%_%1

--
Todd Vargo (double "L" to reply by email)


Posted by Dr John Stockton on February 10th, 2006


JRS: In article <1139450334.012268.95220@g47g2000cwa.googlegroups. com>,
dated Wed, 8 Feb 2006 17:58:54 remote, seen in news:alt.msdos.batch,
Harlan Grove <hrlngrv@aol.com> posted :
He needs 01 to 09 in the second line, not 0 to 9. Slapdash advice is
unhelpful.

It's still not clear how you (and I was replying to foxidrive) would
determine the number of days in the month, which the OP seems to need.
If that's not done automatically, then the user could be forced to type
it in, or the code could inelegantly create 31 folders for every month
with the extras being harmless.

Then I don't see why you consider it necessary to butt in and advise on
the extra lines when, if you had read to the end of my article, you
would have seen that extra lines were already implied.

But I'll change my answer. I'd not create the date in that order; the
advantages of following ISO 8601 are clear to all bar the
chronologically retarded, so I'd use YYYY-MM-DD

NOWMINUS z4 n-1 d1 l4 vYR b6 l2 vMO b9 vEOM

echo. | COLS !%EOM%-1 | COLS 'md * '%YR% '- '%MO% '- ^2,101
-> md 2006-02-01
md 2006-02-02
....
md 2006-02-28

which goes to the end of the month in question; redirect the output to a
batch file and execute it. NOWMINUS can, however, provide the month as
a TLA (in English)[*].

NOWMINUS, COLS via sig line 3.

In OT OSs, the use of NOWMINUS will need modification - see NOWMINUS.TXT
- but the OS itself should be able to provide YR, MO, and EOM.
[*] In a well-designed artificial language, the month names would be in
alphabetical order. So would the day names, starting with Monday (but
giving other days an additional name for use in locations where the week
starts too soon).

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Posted by Harlan Grove on February 10th, 2006


Dr John Stockton wrote...

Playing dumb (or maybe it's not playing) is better?


Newsgroups are wonderful: anyone can butt in, and it's not against
netiquette to do so (actually, expecting private conversations in
newsgroups would contrary to the spirit of netiquette). If you don't
like that, you could always send private e-mail to the OP and foxdrive,
no?

February is the only problem. Brute force isn't so difficult with a
reasonable provision for it. Note: following 2 initial spaces
convention to show line wrapping. The following is a snippet, as that
term is generally understood in American English.


REM all months have at least 28 days
for %%d in (01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28) do md %mon%-%%d-%yr%

REM February has 29 days in leap years - modify to handle leap years
before 2008
if %mon% == february for %%y in (2008 2012 2016 2020 2024 2028 2032
2036 2040) do if %%y == %yr% md %mon%-29-%yr%

REM all other months have at least 30 days
if not %mon% == february for %%d in (29 30) do md %mon%-%%d-%yr%

REM and some months have 31 days
for %%m in (january march may july august october december) do if %%m
== %mon% md %mon%-31-%yr%



Granted, but in this regard the OP gave a sample directory:
march-01-2006. That's a clear specification, isn't it? You could argue
that the OP adopt ISO standards, but that's the OP's choice.

In well-designed operating systems, e.g., Unix, the sort command can
figure out date collation. Fortunately, there's a Win32 (includes many
on-topic OSs) port of GNU sort which could sort directory listings in
date order even using mmmm-dd-yyyy format, e.g.,
dir /b/a:d | sort -M .


Posted by Todd Vargo on February 11th, 2006



"Harlan Grove" <hrlngrv@aol.com> wrote in message
news:1139614561.780470.205970@g43g2000cwa.googlegr oups.com...
Examining the fact that the OP already demonstrated ability to add the
additional lines a day earlier (although still needing corrections), it is
obvious, this is yet another example of his typical disgraceful character
assassination trolls. He has this uncanny method (or ill-mannor) of boosting
his ego by putting others down over the most trifle matters. :-(

--
Todd Vargo (double "L" to reply by email)


Posted by Dr John Stockton on February 11th, 2006


JRS: In article <%oWGf.11569$tb3.3669@newssvr24.news.prodigy.net>,
dated Fri, 10 Feb 2006 06:29:15 remote, seen in news:alt.msdos.batch,
Todd Vargo <toddvargo@alvantage.com> posted :

Seems short-termist.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Posted by Dr John Stockton on February 11th, 2006


JRS: In article <1139614561.780470.205970@g43g2000cwa.googlegroups .com>
, dated Fri, 10 Feb 2006 15:36:01 remote, seen in news:alt.msdos.batch,
Harlan Grove <hrlngrv@aol.com> posted :
I did so assert. Did you fail English 101?

By the way, the OP originally wrote :-
"how would you create a .bat file, that creates folders according to the
number of days in the month. so that when you run it, it will create
say 30 folders with the labels march-01-2006 through march-30-2006."

That indicates that the essential is to put the date in the name, and
that the given format shows how it might be done.

The OP is essentially anonymous, so there's no reason to assume him or
her to be incapable of using a rational date format.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Posted by Charles Dye on February 11th, 2006


On Sat, 11 Feb 2006 12:41:36 +0000, Dr John Stockton
<jrs@merlyn.demon.co.uk> wrote:

For values of "long" less than 400?

--
Charles Dye raster@highfiber.com


Posted by Harlan Grove on February 11th, 2006


"Dr John Stockton" <jrs@merlyn.demon.co.uk> wrote...

Should work reasonably well going forward for the expected working life of
anyone reading or contributing to this newsgroup. It wouldn't fail until the
year 2100. At worst, mid-termist.



Posted by Harlan Grove on February 11th, 2006


Dr John Stockton wrote...
....
How it 'might' be done or how it 'should' be done? I suppose it could
mean the former, but most people would interpret it as the latter.

Other than immediate readability in the OP's locale? Then again,
immediate readability is an aspect of rational design, so perhaps the
OP already has a rational date format as far as he's concerned.


Posted by Todd Vargo on February 12th, 2006



"Harlan Grove" <hrlngrv@aol.com> wrote in message
news:BNOdnZWk46Et73PenZ2dnUVZ_tednZ2d@comcast.com. ..
I doubt the syntax will be supported at that time anyway.

--
Todd Vargo (double "L" to reply by email)



Similar Posts