DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   c++ or shell script to delete some files (http://www.daniweb.com/forums/thread49156.html)

uxohus2b Jul 2nd, 2006 7:48 pm
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.

Ancient Dragon Jul 2nd, 2006 9:04 pm
Re: c++ or shell script to delete some files
 
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.

uxohus2b Jul 2nd, 2006 9:17 pm
Re: c++ or shell script to delete some files
 
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.

WolfPack Jul 2nd, 2006 11:25 pm
Re: c++ or shell script to delete some files
 
Quote:

Originally Posted by uxohus2b
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

uxohus2b Jul 2nd, 2006 11:59 pm
Re: c++ or shell script to delete some files
 
Quote:

Originally Posted by WolfPack
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.


All times are GMT -4. The time now is 5:57 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC