Question : DOS Batch script to Copy/Delete Files with Date in File Name

Can someone help me write a dos batch script that will do the following when run:

1) Move all files with .trn or .bak extension into a network path with a date in the file name that is more then 1 week old from the day run.
Example:
Run Date 7/23/2010
Files in Folder: Test_file_201007100020.trn
                        Test_file_201007220020.trn
                        Test_file_201007100020.bak
                        Test_file_201007100020.temp
The files Test_file_201007100020.trn,Test_file_201007100020.bak are moved to the network location, all other files are left alone

2)Delete files with a .trn or .bak from the network location with a date in the file name older than 4 weeks from today:
Example:
Run Date 7/23/2010
Files in Folder: Test_file_201006100020.trn
                        Test_file_201007220020.trn
                        Test_file_201006100020.bak
                        Test_file_201006100020.temp

The files Test_file_201006100020.trn,Test_file_201006100020.bak are deleted from the network location, all other files are left alone

Answer : DOS Batch script to Copy/Delete Files with Date in File Name

Okay, going back to where I was -1 changes ago, and correcting the problem there (I was using %%~tA instead of %%~nA to get the date from he files name), see how this goes.

~bp
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
 
REM Define source and destination locations, and file patterns to copy
set SourceDir=c:\temp\EE26352923\source
set DestDir=c:\temp\EE26352923\dest
set MatchExts=*.trn *.bak
 
REM Get todays date and convert to Julian
for /F "tokens=2-4 delims=/ " %%A in ("%DATE%") do set TodaysDate=%%C%%A%%B
call :jdate TodaysJDate %TodaysDate%
 
REM Move to source directory, find all files matching desired pattern
pushd "%SourceDir%"
for %%A in (%MatchExts%) do (
  REM Get files modification date, convert to Julian
  set FileDate=%%~nA
  call :jdate FileJDate !FileDate:~-12,8!
  REM Calculate age of file in days, if older than 7 days move to destination
  set /A FileAge=%TodaysJDate%-!FileJDate!
  if !FileAge! GTR 7 move "%%~A" "%DestDir%"
)
popd
 
REM Move to destination directory, find all files matching desired pattern
pushd "%DestDir%"
for %%A in (%MatchExts%) do (
  REM Get files modification date, convert to Julian
  set FileDate=%%~nA
  call :jdate FileJDate !FileDate:~-12,8!
  REM Calculate age of file in days, if older than 28 days delete from destination
  set /A FileAge=%TodaysJDate%-!FileJDate!
  if !FileAge! GTR 28 del "%%~fA"
)
popd
 
exit /b
 
REM Subroutine to calculate Julian date
:jdate return-variable date-string(YYYYMMDD) 
  set DateStr=%~2
  set yy=%DateStr:~0,4%
  set /A mm=1%DateStr:~4,2%-100
  set /A dd=1%DateStr:~6,2%-100
  set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
  set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
  exit /b
Random Solutions  
 
programming4us programming4us