954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

c++ or shell script to delete some files

Hi,

Could someone tell me how I can delete files using a shell script or c++? The condition for deleting files: compare all files (say *.deb files) in folder A to those in folder B. If any file in B is also contained A, then delete the one in B.
This could probably done in two lines of shell scripting but I'm very new to programming.

Thanks in advance for your help. Regards, h.y.

uxohus2b
Newbie Poster
5 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

what operating system? MS-Windows? use FindFirstFile() and FindNextFile() to get a list of all the files in dir A, store the filenames in a string array. Then use the same functions to get the files in directory B. For each file in B search the array of filenames you created from directory A, if found the delete the file.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks. I use Ubuntu and Windows. I'm looking for a shell script that does this. But I'll try what you're saying as well. Thanks.

uxohus2b
Newbie Poster
5 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 
Thanks. I use Ubuntu and Windows. I'm looking for a shell script that does this. But I'll try what you're saying as well. Thanks.


Shell scripting is much easier. But this is not the forum for that. There is a UNIX support forum for scripting in daniweb. But you could try this.

#!/bin/bash
pathA='/blah/blah/blah/'
pathB='/blah/blah/blah/blah/'
ext="deb"
for file in $(ls "$pathA"); do
	#
	# Only process DEB files.
	#
	file_ext=${file##*.}
	case $file_ext in
		deb | DEB )
		if [ -e "$pathB$file" ]; then
			echo deleting "$pathB$file"
			rm -f "$pathB$file"
		fi
	esac
done
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Shell scripting is much easier. But this is not the forum for that. There is a UNIX support forum for scripting in daniweb. But you could try this.

#!/bin/bash
pathA='/blah/blah/blah/'
pathB='/blah/blah/blah/blah/'
ext="deb"
for file in $(ls "$pathA"); do
    #
    # Only process DEB files.
    #
    file_ext=${file##*.}
    case $file_ext in
        deb | DEB )
        if [ -e "$pathB$file" ]; then
            echo deleting "$pathB$file"
            rm -f "$pathB$file"
        fi
    esac
done

Thanks very much for the code, and for directing me to the right forum. I really appreciate it.

uxohus2b
Newbie Poster
5 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You