Batchfile with several steps...

FezzikJr February 5, 2009 at 11:39:29
Specs: Windows XP Pro SP2
Greetings! I have a batch file which runs on a schedule via Windows Task Scheduler. It grabs several files on a shared network drive, and copies them to a subfolder within the same drive. While doing so, it also appends the date in YYYYMMDD format. This process is all working as designed.

However, what I want it to do is to show the date for %yesterday%, rather than today. Since it's showing the date for %today%, I am having to go to the 'copy' (HKA - 'archive') and manually change the date to (effectively) date-1.

Below is a copy of the .bat I am using currently. This script was modified using a batch file I found on the internet (( I wish I could find the original author, so I can give him credit... :-/ ))...

============= Start Batch Code =============
ECHO OFF
@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
Set All=%%C%%A%%B
)
@For %%a in ("\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_nc*.csv") do copy %%a "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\NCA\%%~na - %All%.csv"
@For %%a in ("\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_tx*.csv") do copy %%a "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\TX\%%~na - %All%.csv"
@For %%a in ("\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\*_west*.csv") do copy %%a "\\SOMESERVER\SOMESHAREDFOLDER\SOMESUBFOLDER\West\%%~na - %All%.csv"
============== End Batch Code ==============
I checked www.bulkrenameutility.co.uk as a possible solution, but it does not seem that it will work. At least, not in a fully scripted environment...

Then, I had a thought: "What about having the batch use variables called from the 'dir' command?" Below is what I got so far...

=========== Start DIR Results ===========

Z:\sharedfolder>dir /?
Displays a list of files and subdirectories in a directory.

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

[drive:][path][filename]
Specifies drive, directory, and/or files to list.

/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files - Prefix meaning not
/B Uses bare format (no heading information or summary).
/C Display the thousand separator in file sizes. This is the
default. Use /-C to disable display of separator.
/D Same as wide but files are list sorted by column.
/L Uses lowercase.
/N New long list format where filenames are on the far right.
/O List by files in sorted order.
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
G Group directories first - Prefix to reverse order
/P Pauses after each screenful of information.
/Q Display the owner of the file.
/S Displays files in specified directory and all subdirectories.
/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written
/W Uses wide list format.
/X This displays the short names generated for non-8dot3 file
names. The format is that of /N with the short name inserted
before the long name. If no short name is present, blanks are
displayed in its place.
/4 Displays four-digit years

Switches may be preset in the DIRCMD environment variable. Override
preset switches by prefixing any switch with - (hyphen)--for example, /-W.

Z:\sharedfolder>dir /tc *.csv
Volume in drive Z is Scratch
Volume Serial Number is 1234-56789

Directory of Z:\sharedfolder

01/28/2009 02:24 PM 22,735 companyfile_cen.csv
01/28/2009 02:24 PM 51,558 companyfile_ms.csv
10/17/2008 02:30 PM 26,193 companyfile_nc.csv
10/20/2008 10:02 AM 55,210 companyfile_tx.csv
10/17/2008 02:30 PM 34,202 companyfile_west.csv
01/28/2009 02:24 PM 60,454 companyfile_wv.csv
6 File(s) 250,352 bytes
0 Dir(s) 8,942,514,176 bytes free

Z:\sharedfolder>dir /tw *.csv
Volume in drive Z is SharedServer
Volume Serial Number is 1234-56789

Directory of Z:\sharedfolder

02/05/2009 12:32 PM 22,735 companyfile_cen.csv
02/05/2009 12:20 PM 51,558 companyfile_ms.csv
02/05/2009 12:13 PM 26,193 companyfile_nc.csv
02/05/2009 12:26 PM 55,210 companyfile_tx.csv
02/05/2009 12:32 PM 34,202 companyfile_west.csv
02/05/2009 12:20 PM 60,454 companyfile_wv.csv
6 File(s) 250,352 bytes
0 Dir(s) 8,942,514,176 bytes free

Z:\sharedfolder>
============ End DIR Results ============

The 'modified date' is always +1 day what I need the 'archive' filename to include.

For example, if file
companyfile_cen.csv
has a modified date of
02/05/2009
Then, I need the 'archived' file to have the name of
companyfile_cen - 20090204.csv

My searches are getting to be too circular, and I'm at my wit's end...

So... I figured I'd come here. I know there must be a way to do this in an entirely .bat environment. I figured I'd look for a way to change the 'create date' of the .csv file properties, but to no avail.


See More: Batchfile with several steps...

Report •


#1
February 5, 2009 at 12:16:54
So far nobody has shown a way to make date math work in BAT.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
February 5, 2009 at 12:41:39
Thanks for the reply.

I just ran across something on ExpertsExchange, written by SteveGTR, where he wrote the following code:
============= Start Batch Code =============
@echo off

for /F "tokens=2-4 delims=/ " %%f in ('date /t') do (
set mm=%%f
set dd=%%g
set yyyy=%%h
)

set CurDate=%mm%/%dd%/%yyyy%

REM Substract your days here
set /A dd=1%dd% - 100 - %1
set /A mm=1%mm% - 100

:CHKDAY

if /I %dd% GTR 0 goto DONE

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through

:SET31

set /A dd=31 + %dd%

goto CHKDAY

:SET30

set /A dd=30 + %dd%

goto CHKDAY

:LEAPCHK

set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto CHKDAY

:SET29

set /A dd=29 + %dd%

goto CHKDAY

:DONE

echo Date %1 days before %CurDate% is %mm%/%dd%/%yyyy%
============== End Batch Code ==============

Unfortuantely, when I run the command as he suggested, I get the following output in the command prompt...

=========== Start CMD Results ===========
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\WINDOWS>cd..

C:\>cd backup

C:\BACKUP>date 1
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy)
C:\BACKUP>date
The current date is: Thu 02/05/2009
Enter the new date: (mm-dd-yy)
C:\BACKUP>date /p
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy)
C:\BACKUP>date -1
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy)
C:\BACKUP>
============ End CMD Results ============

Any ideas on how to make this work?

Select * From Users Where Clue > 0;
0 rows affected


Report •

#3
February 5, 2009 at 13:53:52
I'm not going to plow through his code now, but there *IS NO* general substring handling of DATE.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
February 5, 2009 at 15:05:36
As M2 points out substrings are problematic, this is because date, %date%, and other date information inside cmd is subject to regional settings.

I have a script that uses reg information from reg.exe to get around this in xp(and possibly later).

Here is a script that *should* work with any regional settings is xp that contain enough information to extrapolate the date. This means that "ddd mm yy" will not work because fri 02 09 could be at least 3 dates.

The output date(yesterdays date) is formatted dd/mm/yyyy but can easily be change at the third last line of the script.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "skip=2 tokens=3" %%a in ('reg query "HKEY_CURRENT_USER\Control Panel\International" /v sDate') do set lim=%%a
for /f "skip=4 tokens=2,*" %%a in ('reg query "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate') do (
set dt=%%b
if defined lim set dt=!dt:%lim%= !
for %%c in (!dt!) do (
set /a pcnt+=1
set p!pcnt!=%%c
))
set dvar=%date%
if defined lim set dvar=!dvar:%lim%= !
for %%a in (%dvar%) do set nvar=%%a !nvar!
for %%a in (%nvar%) do (
for %%z in (!pcnt!) do (
if /i "!p%%z:~0,1!"=="y" (
set tyear=%%a
) else (
if /i "!p%%z:~0,3!"=="mmm" (
for %%y in ("jan=1" "feb=2" "mar=3" "apr=4" "may=5" "jun=6" "jul=7" "aug=8" "sep=9" "oct=10" "nov=11" "dec=12") do set %%y
set tmonth=!%%a!
) else (
if /i "!p%%z!"=="dd" (
set tday=%%a
) else (
if /i "!p%%z!"=="d" (
set tday=%%a
) else (
if /i "!p%%z!"=="mm" (
set tmonth=%%a
) else (
if /i "!p%%z!"=="m" (
set tmonth=%%a
)))))))
set /a pcnt-=1
if !pcnt!==0 goto finish
)

:finish
for %%a in (tyear tmonth tday) do if not defined %%a echo An Error Occured&&ENDLOCAL&&exit /b 1
if %tyear% leq 99 set tyear=20%tyear%
if "%tmonth:~0,1%"=="0" set tmonth=%tmonth:~1%
if "%tday:~0,1%"=="0" set tday=%tday:~1%
SET /A tday-=1
IF %tday%==0 (
SET /A tmonth-=1
IF !tmonth!==0 (
SET /A tyear-=1
SET tday=31
SET tmonth=12
) else (
IF !tmonth!==2 (
set /a leap=!tyear! %% 4
IF !leap! GTR 0 (SET tday=28) ELSE SET tday=29
) else (
FOR %%G IN (1,3,5,7,8,10) DO IF !tMONTH!==%%G SET TDAY=31
FOR %%H IN (4,6,9,11) DO IF !tMONTH!==%%H SET TDAY=30
)))
IF %tday% LSS 10 SET tday=0%tday%
IF %tmonth% LSS 10 SET tmonth=0%tmonth%
endlocal & set yesterdaysdate=%tday%/%tmonth%/%tyear%
echo %yesterdaysdate%
PAUSE


Report •

#5
February 5, 2009 at 18:23:35
The following will return yesterday's date regardless of the system date format.

:: Code begins....
@echo off
set filename=%temp%\filename.vbs
echo       YestDate = (Date()-1)>%filename%
echo       YearDate = DatePart("YYYY", YestDate)>>%filename%
echo      MonthDate = DatePart("M"   , YestDate)>>%filename%
echo        DayDate = DatePart("D"   , YestDate)>>%filename%
echo Wscript.Echo YearDate^&" "^&MonthDate^&" "^&DayDate>>%filename%

for /f "tokens=1-3 delims= " %%A in ('cscript //nologo %filename%') do (
        set year=%%A
        set month=%%B
        set day=%%C
)
del %filename%

if %day%   lss 10 set   day=0%day%
if %month% lss 10 set month=0%month%

set yesterday=%year%%month%%day%

echo %yesterday%
:: Code ends....

Hope this helps.


Report •

#6
February 6, 2009 at 14:02:15
After a small modification to the first suggestion, both return the correct date in the format desired. Many thanks to both of you!

Now for the st00pid question: How to I use them to copy, then modify the name of the files as I was doing in the first script I posted?

-Fezz

Select * From Users Where Clue > 0;
0 rows affected


Report •

#7
February 6, 2009 at 16:58:31
It should be very easy to do, all you need to do is remove the part of the code that was getting todays code and add in one of the codes for yesterday changing either %yesterday% or %yesterdaysdate% to %all% or visa versa.


Remove this:

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
Set All=%%C%%A%%B
)


Report •

#8
February 10, 2009 at 08:15:33
Many thanks! It's working! :-)

Select * From Users Where Clue > 0;
0 rows affected


Report •


Ask Question