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
SET Day=%date:~0,2%
SET Month=%date:~3,2%
SET Year=%date:~6,4%
SET cdate=%Year%%Month%%Day%

SET localDir=D:\New_Files
SET newDir=D:\test

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

Recommended Answers

All 3 Replies

Personally if it was me I would go get a utility containing the Linux find application. It will search a directory structure for files and/or directories that: Have a name like, were created x days ago, were last accessed x days ago, and then exec a command on that found item. Google 'man find' for a copy of the linux find command parameters. A zip file containing Unix utilities for windows can be found at:

http://sourceforge.net/projects/unxutils/

And includes the man pages.

The reason I suggest this is I put together a script to do a similar thing in about 20 minutes with the linux utility find.exe that took a very decent Visual Basic Programmer over a day to get working (almost).

I would listen to rch1231. There is also another utility called rsync you can get for Windows which has extensive copy/backup options. This may be possible to do in a batch script but you will need to run down those guys who used to make batch files for as400 mainframes because nobody knows DOS batch scripts that well anymore :P

Hi,

@echo off
SET Day=%date:~0,2%
SET Month=%date:~3,2%
SET Year=%date:~6,4%
SET cdate=%Year%%Month%%Day%

SET localDir=D:\New_Files
SET newDir=D:\test

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

pause
rem dir c:\windows

The way you are setting the variables of day, month and year are incorrect.

ECHO %date%
Wed 12/16/2009

Therefore 16 would be the day which starts at possition 7 ( the space doesn't count and the ('/') does ) and you want to read 2 character; then:

SET day=%date:~7,2%

Adjusting all the variables is necessary.

@ECHO OFF & SETLOCAL ENABLEDELAYEDEXPANSION

SET day=%date:~7,2%
SET month=%date:~4,2%
SET year=%date:~10,4%
SET cdate=%year%%month%%day%
SET dst_dir="New_file"
IF NOT EXIST %dst_dir%  MD %dst_dir%


FOR /F %%A IN ('DIR /B *.dat') DO (
	SET source=%%A
	SET current=!source:~1,8!
	IF NOT %cdate%==!current! COPY !source! %dst_dir%\
)
ENDLOCAL
commented: wow +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.