[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: C programming problems...



Here is a sample C program to do what you requested.
It is not perfect and I leave it up to you to find and repair.
--
Daniel R. Oldham Ph.D.
Computer Engineer
Satellite Networks and Architectures Branch
NASA Glenn Research Center
21000 Brookpark Road (MS 54-5)
Cleveland OH 44135
216.433.6307 - voice
216.433.8705 - fax
oldham grc nasa gov

>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<

On 2/4/02, 12:40:09 PM, Antti Hakulinen 
<Antti Hakulinen fi flextronics com> wrote regarding C programming problems...:


> Hi there.
> I'm a C language newbie and i'm not cabable of doing one program on my 
own
> yet, so care to point the best "What you personally think as..." site to 
me
> about learing C language.
> Mostly i am interested in time functions etc etc...

> I would like to construct a little program that moves files of certain 
date
> from one directory to another.
> For example, a program that would move all files to another directory 
which
> are two days older than the current date...
> Any examples anywhere??
> I must be blind but i didn't find any...


> Thankz in advance.


> Regards: Antti...

> ***********************************************************
> Antti Hakulinen, Production IT Support
> Flextronics Holding Finland Oy
> P.O Box 11 (Jaakontie 1)
> 39201 KYRÖSKOSKI, FINLAND
> GSM +358 40 7789 682
> e-mail: Antti Hakulinen fi flextronics com
> www.flextronics.com
> ***********************************************************


> ###########################################

> This message has been scanned by F-Secure Anti-Virus for Microsoft 
Exchange.



> _______________________________________________
> Seawolf-list mailing list
> Seawolf-list redhat com
> https://listman.redhat.com/mailman/listinfo/seawolf-list
/*******************************************************************************
*
* FILE:		movefiles.c 
*
* DESC:		move files from a directory to another if within time frame
*
* AUTHOR:	Dr. Daniel R. Oldham, NASA Glenn Research Center
*
* DATE:		February 5, 2002
*
* EDIT HISTORY:	
*
*
*******************************************************************************/

#include	<stdio.h>
#include	<stdlib.h>
#include	<time.h>
#include	<dirent.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<unistd.h>

/*
 *	define global variables
 */
int	interval;	/* in seconds */
char	fromdirname[ 128];
char	todirname[ 128];

/*
 *	process the command line parameters
 */
void	process_command_line( char *command)
{
	if( !strncmp( command, "-F", 2))
	{
		if( command[2])
		{
			strcpy( fromdirname, &command[2]);
			return;
		}
	}
	if( !strncmp( command, "-T", 2))
	{
		if( command[2])
		{
			strcpy( todirname, &command[2]);
			return;
		}
	}
	if( !strncmp( command, "-I", 2))
	{
		if( command[2])
		{
			sscanf( &command[2], "%d", &interval);
			return;
		}
	}
	printf( "Usage: movefiles [-Ffromdirname] [-Ttodirname] [-Iinterval]\n");
	printf( "Where: fromdirname = where to check file from directory\n");
	printf( "Where: todirname = where to move file to directory\n");
	printf( "Where: interval = time interval in seconds of file modification date to move\n");
	exit( -1);
}

/*
 *	main program
 */
int	main( int argc, char *argv[])
{
	time_t	timeval;
	DIR	*dir;
	struct dirent	*dent;
	struct stat	stat;
	char	command[ 256];
/*
 *	initialize global variables
 */
	interval = 60 * 60 * 24 * 2;	/* 60 seconds * 60 minutes * 24 hours * 2 days */
	strcpy( fromdirname, ".");
	strcpy( todirname, "..");
/*
 *	process the command line args
 */
	while( --argc)
		process_command_line( *++argv);
/*
 *	get the current time
 */
	time( &timeval);
/*
 *	open the directory 
 */
	dir = opendir( fromdirname);
	if( dir)
	{
		for( dent = readdir( dir); dent; dent = readdir( dir))
		{
/*
 *	check for a valid file name, NOT a directory "." ".." or a hidden file ".something"
 */
			if( *dent->d_name == '.')
				continue;
/*
 *	get the file status information
 */
			if( ! lstat( dent->d_name, &stat))
			{
/*
 *	check if the file modify time plus time interval is greater than current time
 */
				if( timeval < stat.st_mtime + interval)
				{
					sprintf( command, "mv %s/%s %s/\n", fromdirname, dent->d_name, todirname);
/*
 *	execute the command but commented out at this time
 */
					printf( "%s", command);
//					system( command);
				}
			}
		}
	}
/*
 *	exit
 */
	exit( 0);
}
#-----------------------------------------------------------------------------
#	makefile
#	Dr. Daniel R. Oldham	February 5, 2002
#
# REVISION HISTORY
#
#-----------------------------------------------------------------------------
.KEEP_STATE:
SHELL=/bin/bash

#
#	c compiler and linker
#
CC=	gcc
LINK=	gcc

#
#	yacc lex compiler linker flags
#
YFLAGS	= -dv
LFLAGS	=
CFLAGS	= 
LDFLAGS	= 

#
#	linker
#
LIBRARIES=	-lm 

#
#	to make all
#
all:		movefiles 

#
#	movefiles
#
MOVEFILES_SRCS	= movefiles.c

MOVEFILES_OBJS	= $(MOVEFILES_SRCS:%.c=%.o)

movefiles:	$(MOVEFILES_OBJS)
		$(LINK) $(MOVEFILES_OBJS) $(LIBRARIES) $(LDFLAGS) -o movefiles

movefiles.o:	movefiles.c Makefile 

#	clean
#
clean:
		-rm -f *.o lex.c yacc.c y.output

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]