Question

Locked

Making a Batch file asking enter date to copy from network

By barodian_da_dude ·
Hi,

Let me tell you what i want to make so that all you can have a basic idea why i need to make this kind of file.

I m in a BPO sector (System Administrator) we daily gets .zip files to provide work to our operators as they finishes the work the output of the work is automatically generated by internal software to a different location where at that location we are having folders by the name of dates only (MMDDYYYY <-- only) which we need to copy to another location on network need to zip it and have to upload it to remote site. This is all a manual work.

So i would like to make a windows batch file which will ask me to enter date in MMDDYYYY format and after providing the the date it will automatically copy the specified folder to the destination location.

I googled something but found out this thing which is i think just asking a date only and providing the output in different formats

::== runtoday.bat
@echo off
setLocal EnableDelayedExpansion

set /p refDATE=enter date to compare in format: YYYYMMDD
call :now
if !refDATE! lss !today! echo don't run
if !refDATE! geq !today! echo run

goto :eof

:now
:: use debug/BIOS to get DATE into vars

::==
@echo off > d.d

>> d.d echo E 0100 B4 2A CD 21 B4 4C CD 21
>> d.d echo N DAY.COM
>> d.d echo RCX
>> d.d echo 8
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 F0 B4 4C CD 21
>> d.d echo N MONTH.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 89 C8 B4 4C CD 21
>> d.d echo N YEAR.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 D0 B4 4C CD 21
>> d.d echo N DAT.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo Q

debug < d.d > nul
del d.d

dat
for %%a in (1 2 3 4 5 6 7 8 9 ) do if errorlevel %%a set dat=0%%a
for %%a in ( 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) do if errorlevel %%a set dat=%%a
echo Date %dat%

day
if errorlevel 0 set day=Sunday
if errorlevel 1 set day=Monday
if errorlevel 2 set day=Tuesday
if errorlevel 3 set day=Wednesday
if errorlevel 4 set day=Thursday
if errorlevel 5 set day=Friday
if errorlevel 6 set day=Saturday
echo Day %day%

month
for %%a in (1 2 3 4 5 6 7 8 9 ) do if errorlevel %%a set month=0%%a
for %%a in ( 10 11 12) do if errorlevel %%a set month=%%a
echo Month %month%

year
if errorlevel 215 set Year=2007
if errorlevel 216 set Year=2008
if errorlevel 217 set Year=2009
if errorlevel 218 set Year=2010
echo Year %Year%
set today=%year%%month%%dat%
echo Today is %today%
for %%c in (day month year dat) do del %%c.com
::== done


so requesting you to solve my this kind of requirements..

thanks...

Ketan Aagja

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Script it

by LarryD4 In reply to Making a Batch file askin ...

Honestly this would work much easier with a VB Script if you know how to create it.

Collapse -

Batch file Date variable.

by 1bn0 In reply to Making a Batch file askin ...

Don't know if this will help but.

batch file date variable = %date:~%
batch file time variable = %time:~%

Everything below here is a copy of my notes for working with dates and times in batch files. Hope you can make sense of it.


Using the current date in a batch file

The date command displays the current date and gives the operator the opportunity to change the current date.

C>date
Tue 06/03/2008
Enter the new date: (mm-dd-yy)
C>

The %date:~% variable returns the current date as a string

C>echo %date:~%
Tue 06/03/2008
C>

The following syntax can be used to extract desired text from the string returned by %date:~%

%date:~(-)X,Y%

% start replacement variable
date:~ get the current date
-x move the cursor (-)X number of characters,0 is apparently the first character, or move back from the end using - numbers.
>
> C>echo %date:~4,2%%date:~7,2%%date:~12,2%
>
> and
>
> C>echo %date:~-10,2%%date:~-7,2%%date:~-2,2%
>
> both return the same result
>
> 060308
>
> C:>
>
> from the date string "Tue 06/03/2008"

, seperator
y display next Y characters from the cursor point
% end replacement variable


Date string Output Extracted from:
%date:~-4,4% 2008 Tue 06/03/2008
^xxxx
%date:~-7,2% 03 Tue 06/03/2008
^xx
%date:~-0,2% Tu Tue 06/03/2008
^xx
%date:~-0,3% Tue Tue 06/03/2008
^xxx
%date:~-10,4% 06/0 Tue 06/03/2008 (Note: the / counts as one of he four characters)

%date:~-10,2% 06 Tue 06/03/2008
^xx
Date string variables can be combine to pull together the various parts YYY MM DD

%date:~-4,4%%date:~-7,2%%date:~-10,2% 20080306

Tue 06/03/2008Tue 06/03/2008Tue 06/03/2008
^xxxx ^xx ^xx

Using the current time in a batch file

The time command displays the current time and gives the operator the opportunity to change the current date.

C>time
The current time is: 9:24:52.64
Enter the new time:
C>

The %time:~% variable returns the current time as a string

C>echo %time:~%
9:59:11.50

C>

Note that windows returns the time to one-one hundreths of a second. ( at least on this machine it does) and there is a space preceding the hour digit,9.


A little bit of extra work is required to ensure hours between 0 and 9 are handled correctly.


*********************************************************************************************************************************************
Reference information obtained from:
http://talk.bmc.com/blogs/blog-gentle/anne-gentle/dos-timestamp-tips/view?searchterm=batch

"this routine works before and after ten in the morning. Silly DOS, it doesn't put a leading zero in unless you tell it to. Here's how to tell DOS to put in the leading zero if the current time is prior to 10:00am."

REM Create sub directory called \yymmdd_hhmmss
REM where yymmdd_hhmmss is a date_time stamp like 030902_134200

set hh=%time:~0,2%

REM Since there is no leading zero for times before 10 am, have to put in
REM a zero when this is run before 10 am.

if "%time:~0,1%"==" " set hh=0%hh:~1,1%

set yymmdd_hhmmss=%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%

md h:\%yymmdd_hhmmss%

*********************************************************************************************************************************************
In English:

Times from midnight, 0:00 a.m. to 9:59 a.m. will return a single digit preceded by a space. It is usually desired to replace the space with a leading zero (0).

To accomplish this an environment variable to correctly reflect the hour in two digits is created using the %time:~% variable.

"if "%time:~0,1%"==" " set hh=0%hh:~1,1%"
if the left most digit is empty then set the environment variable hh to 0 + second digit from the begining.

%time:~% returns " 9:30:08.69"

set hh=09 0 + " 9:30:08.69"

then use %hh% as a variable in your command string to create a second variable with the complete date & time format yuo wish using %hh% to give you the hour in 2 digits.

"set yymmdd_hhmmss=%date:~12,2%%date:~4,2%%date:~7,2%_%hh%%time:~3,2%%time:~6,2%"


simple example , if the extra space in place of the 10's digit in the hour is not a problem:

c>echo The current hour is %time:~-11,2% the current minute is %time:~-8,2% and the current hundreth seconds are %time:~-2,2%

The current hour is 9 the current minute is 55 and the current hundreth seconds
are 26

Collapse -

got some hints but needs editing

by barodian_da_dude In reply to Making a Batch file askin ...

I have found some codings for the batch file... but i think it need a lot editing as


@echo off

rem get today's date
set Day=%Date:~7,2%
set Mth=%Date:~4,2%
set Yr=%Date:~10,4%
set files=%my network path%

rem handle single-digit days
set Day1=%Day%
set mon1=%Mon%
set year=%Year%
if %Year%%Mon%%Day% LSS 1 GOTO %files% <-- not working
copy %Year%%Mon%%Day% %files% <--- not working
rem set Day1=%Date:~8,1%
rem set Mth1=%Mth%
rem if %Mth% LSS 10 set Mth1=%Date:~5,1%


I don't know how to set it out...

Larry bro i actually don't know scripting hehe sorry... if i was than i was the person answering someone else about batch files... but thanx a lot both of you for your kind support and time... do lemme know if you finds interesting related to my problem...

thanks

Collapse -

Finally I personally made it...!!!

by barodian_da_dude In reply to got some hints but needs ...

I just completed the stated task as mentioned in my first post... after so much googling for more than 3 weeks I finally made it... which can now automatically generate the output and can zip it also... here is the file


@echo off
TITLE KETAN D. AAGJA
REM ----------------------------------------------------------------------------
REM THIS IS A WINDOWS BATCH FILE WHICH AUTOMATICALLY COPIES, GENERATES OUTPUT |
REM AND ZIPS THE OUTPUTFILES |
REM AUTHOR - KETAN D. AAGJA |
REM ----------------------------------------------------------------------------


REM SETTING UP CURRENT DATE TO MMDDYYYY FORMAT
REM ------------------------------------------

set Pdate=%date:~4,2%%date:~7,2%%date:~10,4%%1
COLOR E0

Echo ****************************************************
echo * *
echo * *
echo * -------------------- *
echo ****************************************************
echo.
echo.
echo.
echo.

REM VARIABLES HAVE BEEN DEFINED FOR SOURCE & DESTINATION LOCATIONS
REM --------------------------------------------------------------


set /P d= PLEASE ENTER DATE : -
set f=C:\DOCUME~1\aagjaket\Desktop\test\%d%
set e=C:\DOCUME~1\aagjaket\Desktop\test\output\%d%
set copycmd=xcopy /s /c /d /e /h /i /r /y
ECHO.

REM SETUP CONDITION IF THE ENTERED DATE IS LESS THAN TODAY'S DATE THAN GO TO
REM DESTINATION AND COPY THE ENTERED DATE'S FOLDER TO SOURCE LOCATION
REM ------------------------------------------------------------------------

IF /I %d% LSS %Pdate% %copycmd% "%f%" "%e%"
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
COLOR 1A
echo PLEASE VERIFY THE SOURCE AND DESTINATION FILES IF ITS NOT MATCHING PRESS CTRL+C
echo.
echo.
echo - BELOW LISTED FILES HAVE BEEN COPIED -
echo -----------------------------------
echo.
echo.


REM DISPLAYING SOURCE AND DESTINATION LOCATIONS'S FILES TO CHECK WETHER
REM ALL THE FILES HAVE BEEN SUCCESSFULLY COPIED OR NOT
REM --------------------------------------------------------------------


echo.
echo FILES FROM SOURCE LOCATION
echo ---------------------------
rem echo.
echo.
dir /B %f%
echo.
echo.
echo FILES WHICH HAVE BEEN COPIED TO DESTINATION
echo -------------------------------------------
rEM echo.
echo.
dir /B %e%\*
echo.
echo.
echo.
echo.
ECHO PRESS ANY KEY TO OPEN THE OUTPUT FILES
pause
echo.


REM OPENING EVERY OUTPUT SUMMARY FILES SO THAT ADMINISTRATOR CAN VERIFY
-------------------------------------------------------------------


echo.
echo (--PLEASE COMPARE IT WITH RECEIVED E-MAIL--)
echo ---------------------------------------------------------
COLOR E1
ECHO.
GOTO output
utput

type %e%\*output.txt |MORE
ECHO.
echo.
echo.
echo PRESS ANY KEY TO UNZIP THE FILES AND MAKE IT READY TO UPLOAD
PAUSE

REM MAKING A ZIP FILE & DELETING REMAINING FILES AFTER ZIPPING IN SPECIFIED DATE FOLDER
REM BY USING OPENSOURCE 7ZIP SOFTWARE
REM SO THAT AN ADMINISTRATOR CAN UPLOAD THE ZIP FILE ONTO THE FTP SITE
REM -----------------------------------------------------------------------------------


COLOR EF
"C:\Program Files\7-Zip\7z.exe" a -tzip "acs%d%.zip" -r "%e%"
rem RD /S /Q * %e%
move /y *.ZIP %e%
CD %e%
DEL /S /Q *.txt
DEL /S /Q *.xls
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
COLOR E1
echo.

REM GENERATING FINAL MESSAGE TO ADMINISTRATOR THAT EVERYTHING WENT PERFECT
REM ----------------------------------------------------------------------


echo - OUTPUT HAS BEEN SUCCESSFULLY GENERATED -
echo ------------------------------------
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
ECHO KETAN D. AAGJA
echo.
ECHO (ADMINISTRATOR - )
echo.
echo.
echo.
PAUSE

Back to Storage Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums