User Current Date in batch file command

Posted on 2003-11-21
Last Modified: 2008-01-16
I need to manipulate my server log file where the file name is in the format:
ex031121.log or 'ex'+year+month+day+'.log'.

So if I need to execute:
[a bunch of stuff]
copy C:\WINDOWS\system32\LogFiles\W3SVC1\ex031121.log c:\holder
[a bunch more stuff]

how would I generalize that so I don't have to edit the batch each day.
Question by:juststeve
  • 2
LVL 57

Expert Comment

by:Pete Long
ID: 9797362
Hi juststeve,
@echo off
set filename=%date:~10,4%-%date:~4,2%-%date:~7,2% %date:~0,3%.txt
xcopy \\dc1\data\*.* d:\public\data /s /d /c /y /i
call :report %errorlevel% \\dc1\data
xcopy \\dc1\doc\*.* d:\public\doc /s /d /c /y /i
call :report %errorlevel% \\dc1\doc
xcopy \\dc1\archive\*.* d:\public\archive /s /d /c /y /i
call :report %errorlevel% \\dc1\archive
xcopy \\server2b\public2\*.* d:\public\public2 /s /d /c /y /i
call :report %errorlevel% \\server2b\public2
set filename=
goto :eof

if %1==0 set status=OK
if not %1==0 set status=Failed
echo %2 %status% at %time%>>"%filename%"
set status=
goto :eof

Should do what you want, provided xcopy complies with the claimed 'errorlevel 0 if it works' behaviour.



Author Comment

ID: 9797453
Thankx fault for phrasing the question specific to the COPY command. Actaully this is the line than needs to be generalized:

"c:\program files\log parser\preplog" C:\WINDOWS\system32\LogFiles\W3SVC174669028\ex031121.log > C:\WINDOWS\system32\LogFiles\W3SVC174669028\out.txt

Accepted Solution

Archie_Gremlin earned 300 total points
ID: 9799046
In that case you want something like this:-

set filename=ex%DATE:~12,2%%DATE:~7,2%%DATE:~4,2%.log
"c:\program files\log parser\preplog" C:\WINDOWS\system32\LogFiles\W3SVC174669028\%filename% > C:\WINDOWS\system32\LogFiles\W3SVC174669028\out.txt


Author Comment

ID: 9802563
Thankx Archie....a minor adjustment for sake of the archives... to get the month and day in the correct order the code should read:

set filename=ex%DATE:~12,2%%DATE:~4,2%%DATE:~7,2%.log

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 7/OS X dual boot 10 76
How to run a server as a backend service on Raspberry Pi 14 136
Window 7 local DNS 9 88
Utilities to reset local Win 10 passwords 13 126
Introduction Often we come across situations wherein our batch files would be needing to reboot Windows for a variety of reasons. A few of them would be like: (1) Setup files have been updated whose changes can take effect only after a reboot …
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, ( because one time I did this and I essentially had a bricked …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

778 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