Dos batchfile

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).  
Who is Participating?
Richard QuadlingConnect With a Mentor Senior Software DeveloperCommented:
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.

Richard QuadlingSenior Software DeveloperCommented:
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.

redvipergtsAuthor Commented:
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 ?

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

SteveGTRConnect With a Mentor Commented:
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

redvipergtsAuthor Commented:
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%\
Richard QuadlingSenior Software DeveloperCommented:
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).

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.