Tech Support Forum banner
Status
Not open for further replies.
1 - 4 of 4 Posts

·
Registered
Joined
·
2 Posts
Discussion Starter · #1 ·
Hi,
I am very new when it comes to batch files and I need to copy some files from a directory to another on a windows 2003 server.
I have writeen a simple batch file which looks like this:

@echo off
echo Hello this is a test batch file

COPY "D:\New_Files\*.dat" "D:\test\*.dat"

pause
rem dir c:\windows

This is all work, bt the problem I am having is that I only need to copy across files whose date is in the past, i.e.:
The list of files is this
a20091211.dat
a20091212.dat
b20091216.dat
c20091214.dat
c20091215.dat
e20091101.dat

Today is the 15th December 2009 and the file for today would like *20091215.dat. I need to copy all and only the files whose date is prior to today, so I should just move the below files:

a20091211.dat
a20091212.dat
c20091214.dat
e20091101.dat

I have searched high and low on the web and could not find anything: can you please advise on a solution or anywhere where I can find a way to do it?
Thanks
 

·
Microsoft MVP
Joined
·
3,339 Posts
Welcome to TSF!

This will do it.
Edit line 10 as desired. If set to true, the program will list the files it finds, and you can copy all, have it prompt for each one, or quit.
If set to false, it copies all files. If a file with the same name already exists, you'll always be prompted
Set the source and destination paths on lines 12 and 13, change the extension on line 14.
Set line 8 to 0 to include files with today's date.

This assumes the file names are as you listed, one character followed by the date in YYYYMMDD format
Code:
@Echo Off
:: Copy files by date contained in file name
:: Written by TheOutcaste for http://www.techsupportforum.com
Setlocal EnableDelayedExpansion
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
Set _Switches=/B /-Y
:: User Variables
Set _DaysSkip=1
:: Set this next line to False to automatically copy without prompting
Set _Prompt=False
:: Set these variables to the source and destination folders, and set the extension to be copied
Set _SrcPath=C:\Test Dir
Set _DstPath=C:\Test Dir1
Set _Ext=*.dat
:::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Get todays date
Call :GetDate
Set _yy=%_fDate:~,4%
Set _mm=%_fDate:~4,2%
Set _dd=%_fDate:~6,2%
:: Convert todays date to Julian
Call :JDate %_yy% %_mm% %_dd%
Set _JToday=%_JDate%
:: Set Copy date
Set /a _CopyDate=_JToday-%_DaysSkip%
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_SrcPath%
Set _s=s
If %_DaysSkip%==1 set _s=
If /I %_Prompt%==True Echo Please wait, searching for files more than %_DaysSkip% day%_s% old
>"%temp%\tf}1{" Echo.
>>"%temp%\tf}1{" Echo Do you wish to copy the following files?
>>"%temp%\tf}1{" Echo      Name
For /F "Tokens=1 Delims=" %%I In ('Dir "%_SrcPath%\%_Ext%" /A-D /B /ON') Do (
  Set _Fname=%%~nI
  Call :JDate !_Fname:~1,4! !_Fname:~5,2! !_Fname:~7,2!
  If !_JDate! LEQ %_CopyDate% Echo %%I>>"%temp%\tf}1{"
)
:_allFound
If Not Exist "%temp%\tf}1{" Echo No Files Found to copy&Goto _Done
If /I %_Prompt%==False (Set _resp=A)&Goto _CopyFiles
Type "%temp%\tf}1{" | More
:_Prompt1
Set /P _resp=Copy All, Prompt for each, or Cancel (A/P/Q)?
Set _Resp=%_resp:~0,1%
If /I "%_resp%"=="Q" Goto _Done
If /I "%_resp%"=="A" Goto _CopyFiles
If /I NOT "%_resp%"=="P" (Echo (A/P/Q only please)&Goto _Prompt1
:_CopyFiles
If /I "%_resp%"=="A" If %_Prompt%==True Echo Copying Files
For /F "skip=3 Delims=" %%I In ('type "%temp%\tf}1{"') Do (
If /I "%_resp%"=="A" (
Copy %_Switches% "%%I" "%_DstPath%\%%I"
) Else (
  Set /P _In="Copy %%I (Y/N)?: 
  If /I "!_In:~0,1!"=="Y" Copy %_Switches% "%%I" "%_DstPath%\%%I"
))
:_Done
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PopD
Goto:EOF
::===================================::
::                                         ::
::   -   S u b r o u t i n e s   -   ::
::                                         ::
::===================================::
:JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns   : Julian date in variable _JDate
:: Usage
::Call :JDate %__GYear% %_GMonth% %_GDay%
:: First strip leading zeroes; a logical error in this
:: routine was corrected with help from Alexander Shapiro
::Code taken from datediff.bat written by Rob van der Woude
::http://www.robvanderwoude.com
:: Modified to handle months and days witout leading zeros
:: By TheOutcaste http://forums.techguy.org
Set _JMM=%2
Set _JDD=%3
IF 1%_JMM% LSS 110 Set _JMM=%_JMM:~-1%
IF 1%_JDD% LSS 110 Set _JDD=%_JDD:~-1%
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
Set /A _JMonth1 = ( %_JMM% - 14 ) / 12
Set /A _JYear1  = %1 + 4800
Set /A _JDate  = 1461 * ( %_JYear1% + %_JMonth1% ) / 4 + 367 * ( %_JMM% - 2 -12 * %_JMonth1% ) / 12 - ( 3 * ( ( %_JYear1% + %_JMonth1% + 100 ) /100 ) ) / 4 + %_JDD% - 32075
For %%A In (_JMonth1 _JYear1) Do Set %%A=
Goto:EOF
:GetDate
:: This subroutine will always display the same results,
:: for the date independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified from SortDate Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
If NOT [%1]==[] Set Date=%1
If "%date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Default Delimiter of TAB and Space are used
For /F "SKIP=3 TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "SKIP=3 TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%B%%C%%D
Goto:EOF
Jerry
 

·
Registered
Joined
·
2 Posts
Discussion Starter · #3 · (Edited)
Jerry, thanks ever so much.
I have tested it and it is moving the files prior to today's date, althoug it is also moving tomorrow's files. i.e.:
Today being 16th December, it is moving a20091215, b20091214 and not c20091216 as requested. Unfortunately, it is moving also b20091219,c20091220, a20091230 etc. which I do not want to do as they are in the future.
I am trying different things to see if I can correct it, but any help would be appreciated as this type of language is really confusing for me.
Thanks
 

·
Microsoft MVP
Joined
·
3,339 Posts
It's working fine for me.
It's the 17th now.
Here's the list of 10 files, and the list of files that were copied. Only the five containing dates prior to 1217 are being copied, 5 in total
Code:
C:\Test Dir>dir *.dat /b
a20091101.dat
a20091213.dat
a20091217.dat
a20091222.dat
a20091230.dat
b20091219.dat
c20091214.dat
c20091215.dat
c20091220.dat
d20091216.dat

C:\Test Dir>CopyByDateInName
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.

C:\Test Dir>dir "C:\Test Dir1\*.dat" /b
a20091101.dat
a20091213.dat
c20091214.dat
c20091215.dat
d20091216.dat

C:\Test Dir>
Changed one to 1999 and another to 2010:
Code:
C:\Test Dir>dir *.dat /b
a19991230.dat
a20091213.dat
a20091217.dat
a20091222.dat
a20101101.dat
b20091219.dat
c20091214.dat
c20091215.dat
c20091220.dat
d20091216.dat

C:\Test Dir>CopyByDateInName
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.

C:\Test Dir>dir "C:\Test Dir1\*.dat" /b
a19991230.dat
a20091213.dat
c20091214.dat
c20091215.dat
d20091216.dat

C:\Test Dir>
Are you sure the files only have ONE letter and nothing else before the date?
for example, aa20101101.dat would be copied, as it sees the date as a2010110, not 20101101. So that would be seen as Jan 10, year 0, not Nov 1, year 2010
 
1 - 4 of 4 Posts
Status
Not open for further replies.
Top