Go Back  DVD Talk Forum > General Discussions > Tech Talk
Reload this Page >

help with DOS command MD needed

Tech Talk Discuss PC Hardware, Software, Internet and Other Technology

help with DOS command MD needed

Old 11-16-04, 12:00 PM
  #1  
Senior Member
Thread Starter
 
Join Date: Mar 2003
Posts: 789
Likes: 0
Received 0 Likes on 0 Posts
help with DOS command MD needed

Hi,

A very simple problem today, but I cannot find the answer even if my life depended on it.

I need a batch file that can create a directory. The only catch is that the directory has to have the name of the date the batch file is executed.

Example: if I were to launch my batch file on Oct 15, 2004, a directory c:\blabla\20041015 needs to be created (YYYYMMDD format). If I were to launch the batch file on say February 5, 2005, a directory c:\blabla\20050205 needs to be created.

Anybody knows how to do this?
Old 11-16-04, 12:06 PM
  #2  
Banned
 
Join Date: Sep 1999
Location: Memphis, TN
Posts: 11,544
Likes: 0
Received 0 Likes on 0 Posts
Code:
for /f "tokens=1-4 delims=/ " %%a in ('date/t') do (
set dow=%%a
set month=%%b
set day=%%c
set year=%%d
)
md c:\blabla\%year%%month%%day%
Old 11-16-04, 12:12 PM
  #3  
Senior Member
Thread Starter
 
Join Date: Mar 2003
Posts: 789
Likes: 0
Received 0 Likes on 0 Posts
Otto, man, you rock! That was insanely fast...

The day and month were inversed in your version, but that was easy enough to change.

Would you mind quickly explaining how that for loop works?

Thanks again!
Old 11-16-04, 12:19 PM
  #4  
X
Administrator
 
X's Avatar
 
Join Date: Oct 1987
Location: AA-
Posts: 10,764
Likes: 0
Received 4 Likes on 3 Posts
In one statement:

MD c:\blabla\%date:~10,4%%date:~4,2%%date:~7,2%
Old 11-16-04, 12:54 PM
  #5  
Senior Member
Thread Starter
 
Join Date: Mar 2003
Posts: 789
Likes: 0
Received 0 Likes on 0 Posts
X,

That worked great as well! Awesome.

Extra question, seeing as we are on a roll here. The "IF exists" can test on the existence of a file. How do you test on the existence of a directory?

Basically, I would like to extend my batchfile to only create the directory if it doesn't exist yet.

Thanks!
Old 11-16-04, 01:24 PM
  #6  
X
Administrator
 
X's Avatar
 
Join Date: Oct 1987
Location: AA-
Posts: 10,764
Likes: 0
Received 4 Likes on 3 Posts
This generally will work, but your environment may make a difference since some don't contain the nul device by default.

IF EXIST c:\blabla\{directory}\nul GOTO abc

where {directory} is the directory name (probably date in your case)
and abc is the statement to go to if it's found.

You can use the IF NOT EXIST form as well.
Old 11-16-04, 01:47 PM
  #7  
Senior Member
Thread Starter
 
Join Date: Mar 2003
Posts: 789
Likes: 0
Received 0 Likes on 0 Posts
Hi X,

That works great! Thanks again.

Does somebody know of a website where one goes into these kind of details that you guys just showed me? I know the basics of DOS commands, but clearly lack thourough understanding.

Any great links you know of?
Old 11-16-04, 01:48 PM
  #8  
Banned
 
Join Date: Sep 1999
Location: Memphis, TN
Posts: 11,544
Likes: 0
Received 0 Likes on 0 Posts
Originally posted by LolaRennt
The day and month were inversed in your version, but that was easy enough to change.

Would you mind quickly explaining how that for loop works?
Huh. Day and month weren't inverted on my XP box. Oh well.

Basically, it runs "date /t" and parses the result into four variables. The delimiters, as it says in the delims= bit, are a space and a /. That's what it uses to split the output apart with. I like this method better than X's %date% trick because it's a bit more readable, IMO. Also it sets variables and so you can use those multiple times in the function without that weird :10,4 kind of notation. You could use that notation to set those variables, I grant you, but I don't think %date% is supported on all platforms while date/t works most anywhere.

Originally posted by LolaRennt
Extra question, seeing as we are on a roll here. The "IF exists" can test on the existence of a file. How do you test on the existence of a directory?
Actually, same way will work. IF EXIST c:\my\directory\name blah blah blah. The trailing \NUL is not strictly needed.

Last edited by Otto; 11-16-04 at 01:52 PM.
Old 11-16-04, 01:54 PM
  #9  
X
Administrator
 
X's Avatar
 
Join Date: Oct 1987
Location: AA-
Posts: 10,764
Likes: 0
Received 4 Likes on 3 Posts
Originally posted by LolaRennt
Does somebody know of a website where one goes into these kind of details that you guys just showed me? I know the basics of DOS commands, but clearly lack thourough understanding.
I don't know of a specific site.

An example of the date parsing I posted is shown in the help for the SET command. (SET /?) There's a lot in there.

I find a basic knowledge of the commands and then use of their help files to get specific syntax or option details works well.

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Thread Tools
Search this Thread

Archive - Advertising - Cookie Policy - Privacy Statement - Terms of Service - Do Not Sell My Personal Information

Copyright 2018 MH Sub I, LLC dba Internet Brands. All rights reserved. Use of this site indicates your consent to the Terms of Use.