Solved

Dos batchfile

Posted on 2007-12-01
6
703 Views
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).  
0
Comment
Question by:redvipergts
  • 3
  • 2
6 Comments
 
LVL 40

Accepted Solution

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

@ECHO OFF
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.

0
 
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.

Richard.
0
 
LVL 1

Author Comment

by:redvipergts
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 ?

Thanks
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 30

Assisted Solution

by:SteveGTR
SteveGTR earned 20 total points
ID: 20390452
Try this:
@echo off
 
setlocal
 
call :GETDATEPARTS "%date%"
call :GETTIMEPARTS
 
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
 
:GETDATEPARTS
 
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
 
:GETTIMEPARTS
 
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
 
:SETTIMEPART
 
set %1=%2
 
if 1%2 LSS 100 set %1=0%2
 
goto :EOF

Open in new window

0
 
LVL 1

Author Comment

by:redvipergts
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:

@ECHO OFF
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
CD D:\FTP\
MD "%mm%-%dd%-%yy%-%hh%h%tt%m%ss%"
CD D:\FTP\
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%\
0
 
LVL 40

Expert Comment

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

Here is the Universal Date Extractor for DOS BAT files.


@ECHO OFF
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

YY
MM
DD
HH
NN

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).

Richard.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

770 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