Dos batchfile

Posted on 2007-12-01
Medium Priority
Last Modified: 2012-05-05
I want to create a batch file that will create a subdirectory with the current time and date once a week, (do not need code for the once a week, I will use scheduled tasks to run the batch file)  then move 3 files from a directory into a subdirectoy that was just created (described above).  
Question by:redvipergts
  • 3
  • 2
LVL 40

Accepted Solution

Richard Quadling earned 1920 total points
ID: 20389144
I don't have my Universal Date finder at hand, sorry, but ...

FOR /F "tokens=1,2,3 delims=-/ " %%A IN ("%DATE%") DO SET NewDir=DIR_%%A_%%B_%%C
MK C:\Archive\%NewDir%
MOVE C:\Live\file.1 C:\Archive\%NewDir%
MOVE C:\Live\file.2 C:\Archive\%NewDir%
MOVE C:\Live\file.3 C:\Archive\%NewDir%

Now, a couple of gotchas. The list of separators and the sequence of Ymd is system dependent.

That is covered by the Universal Date finder though.

If you intend to only use this on 1 machine, then ...

1 - Open a dos prompt
2 - Type ECHO %DATE% and then press ENTER
3 - Make a note of the separator
4 - Make a note of the sequence of Y m d / m d Y / d m Y / etc.
5 - Amend the script above to include your separator
6 - Alter the MD command to suit the date format you want to replicate. Normally you would want to sort the directories by name, so having them as DIR_CCYY_MM_DD will be best, but that is up to you. %%A will be the first part, %%B the second and %%C the third.

LVL 40

Expert Comment

by:Richard Quadling
ID: 20389152
The universal date finder will give you YY MM DD environment variables for you to do with as you want.

It deals with any date format by looking at the output of the DATE command.

DATE with no params on my system shows me the format I need to put in. This is grabbed and used to provide YY MM DD. Very clever.

If you need this, I can get it back to you on Monday when I'm in the office.


Author Comment

ID: 20389575
Richard, I did find this.  And if you do run this code it will create a time date subdirectory.  I just do not know how to then copy or move my three files to the new directory that looks like this:

2007-01-12 13h35m19

Here is the code to kick out this if ran : 2007-01-12 13h35m19

FOR /F "TOKENS=2-4 DELIMS=/ " %%a IN ("%date%") DO SET dd=%%a&SET mm=%%b&SET yy=%%c
FOR /F "TOKENS=1-3 DELIMS=:." %%a IN ("%time%") DO SET hh=%%a&SET tt=%%b&SET ss=%%c
MD "%yy%-%mm%-%dd% %hh%h%tt%m%ss%"

With what you have provided and the above code can something be put together ?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 30

Assisted Solution

SteveGTR earned 80 total points
ID: 20390452
Try this:
@echo off
call :GETDATEPARTS "%date%"
set newDir=%yy%%mm%%dd%%h%%m%%s%
echo newDir=%newDir%
REM ** Above line is test code. You would place your code here before the goto :EOF command.
goto :EOF
set dt=%~1
set tok=1-3
if "%dt:~0,1%" GTR "9" set tok=2-4
set yyyy=
for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
if not "%yyyy%"=="" set yy=%yyyy%
if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%
goto :EOF
for /f "tokens=1-3 delims=:." %%a in ('echo %time%') do call :SETTIMEPART h %%a&call :SETTIMEPART m %%b&call :SETTIMEPART s %%c
goto :EOF
set %1=%2
if 1%2 LSS 100 set %1=0%2
goto :EOF

Open in new window


Author Comment

ID: 20393172
Thanks Richard for getting on track... that's all I needed.  SteveGTR thanks for your input but Richard had this thing wraped up as I was testing when you posted.

This is the final code:

FOR /F "TOKENS=2-4 DELIMS=/ " %%a IN ("%date%") DO SET mm=%%a&SET dd=%%b&SET yy=%%c
FOR /F "TOKENS=1-3 DELIMS=:." %%a IN ("%time%") DO SET hh=%%a&SET tt=%%b&SET ss=%%c
MD "%mm%-%dd%-%yy%-%hh%h%tt%m%ss%"
MOVE FILE1.TXT D:\FTPDR\KGB\%mm%-%dd%-%yy%-%hh%h%tt%m%ss%\
MOVE FILE2.TXT D:\FTPDR\KGB\%mm%-%dd%-%yy%-%hh%h%tt%m%ss%\
MOVE FILE3.TXT D:\FTPDR\KGB\%mm%-%dd%-%yy%-%hh%h%tt%m%ss%\
LVL 40

Expert Comment

by:Richard Quadling
ID: 20394503
Thanks for the points.

Here is the Universal Date Extractor for DOS BAT files.

REM What are the default tokens to accept.
SET UnivDateTokens=1-3

REM Get the current date from the DATE command rather than %DATE% due to
REM OS version differences.
REM Only the first part is required.
FOR /F "tokens=1 delims=.:/-, " %%O IN ('DATE /T') DO SET UnivDatePart1=%%O

REM If the first part is not a number (that's basically what this says), then use
REM tokens 2 to 4 - Token 1 is the day name.
IF "%UnivDatePart1:~0,1%" GTR "9" SET UnivDateTokens=2-4

REM Now extract the date parts %%P, %%Q and %%R.
FOR /F "tokens=%UnivDateTokens% delims=.:/-, " %%P in ('DATE /T') DO (

REM Now determine the order of the parts as expected by the DATE command.
REM This deals with YY-MM-DD, DD-MM-YY, MM-DD-YY, etc.
REM %%S, %%T and %%U are retrieved.
      FOR /F "skip=1 tokens=2-4 delims=/-,()." %%S IN ('ECHO.^|DATE') DO (
REM Now for the clever bit.
REM The DATE command displays the text "yy-mm-dd" which the above commands
REM extract, so now set yy, mm and dd to their actual date parts retrieved earlier.
            SET %%S=%%P
            SET %%T=%%Q
            SET %%U=%%R
REM Clean up.
            SET UnivDatePart1=
            SET UnivDateTokens=

REM Get the time using %TIME% as time is always displayed the same.
FOR /F "tokens=1,2 delims=/:. " %%J IN ("%TIME%") DO (
      SET HH=%%J
      SET NN=%%K

REM If the hours are less than 9 add a leading zero.
IF %HH% LEQ 9 (
      SET /A HH+=0
      SET HH=0%HH%

So. At this stage, you now have the following environment variables


So you can now construct any datetimestamped file/folder as you need.

This code runs on Windows 98 (I think) onwards.

It allows ANY date order (Month first, day first, or year first, etc).

As far as I can tell, the only thing not dealt with is short year, but I am pretty sure the date command always outputs the long date. Where as (and I am not 100% sure, but pretty sure), the %DATE% can display the date with a short year if it is so configured.

Obviously, if you are putting this into a separate call file, you can't use SETLOCAL, but if the caller has that command, the environment variables will be cleaned up when the caller finishes.

So. I hope that makes some sort of sense.

I've just read through SteveGTR's solution. The principle is very similar to the one I have. Basically using the DATE command to tell us how dates are represented and then using the parts to create known environment variables.

If you wish to move this to a different computer, I would STRONGLY recommend using one of our scripts. I fell foul here when moving scripts between UK setup Windows XP Pro to badly setup US Windows 2000 Server (where no one had bothered to setup the date format or to set it to UK locale, etc).


Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question