Sign in to follow this  
Followers 0
k93

Deleting files (with file extension "a") in a directory that do not exist in different directory (with file extension "b")

7 posts in this topic

I am very new to AutoIT and need some help with the following task:

I have two sets of photo image files, each in a separate directory.  One directory contains jpg images and the other contains a parallel set of images in raw format.  The filename for the same image is the same, but the file extension is different.  I would normally go through the jpg directory, view each image, and delete the files of all the images I did not want to retain.  I would like to have the same set of files deleted in the raw directory without having to view the images a second time and very possibly make mistakes (deleting the wrong raw image files).

What would be the best way to do this using AutoIT?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

EDIT: Oops, I misread the original post.

Simplest way is probably:

#include <File.au3>
#include <Array.au3>

$jpegFolder = "c:\JPEG\"
$aFolder = "c:\A_here\"

$jpegArray = _FileListToArray ( $jpegFolder, "*", 1)
;_ArrayDisplay($jpegArray)
for $i = 1 To UBound($jpegArray)
    $tempArray = StringSplit($jpegArray[$i],".")
    ;MsgBox(0,"",$tempArray[1])
    FileDelete($aFolder & $tempArray[1] & ".a")
Next
Edited by Inverted

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

See FileFindFirstFile and FileFindNextFile in the help. These are basic Windows APIs so it's helpful to learn to use them. The pseudocode would be, read from raw dir FileFindFirstFile using *.*. If file found take the base name and append it to jpeg folder name, then append .jpg or .jpeg whatever is used. If FileExists(jpgfilename) do FileFindNextFile. IOW continue the loop if jpg file found. Otherwise delete the current raw file, then continue loop.

Here's what I use to return file base name from a full path. You may find other methods but this works for me. If invalid path returns blank string. Otherwise returns filename with no path or ext.

 

Func _filebasename($path)
    If $path = "" Then Return ""
    If StringLen($path) < 3 Then Return ""
    Local $pos = StringInStr($path, "\", 0, -1)
    $pos += 1
    Local $tmp = StringMid($path, $pos)
    Return StringLeft($tmp, StringInStr($tmp, ".", 0, -1) - 1)
EndFunc
Edit. With these find file functions you may have to screen out the psuedofiles '.' and '..' I'm not sure. I know I do when doing it in c++. Edited by MilesAhead
1 person likes this

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

 

Simplest way is probably:

[snip]

 

be cautious running this, I think this will do the opposite of what you want it to do, if I understand you correctly. Instead of making the directories match their contents with different file formats, it will take all the files that are in the JPG directory and delete their RAW cousins, leaving the RAW images you don't want and the JPG images you do want.

try this instead:

#include <File.au3>
#include <Array.au3>

$jpegFolder = "C:\JPG folder\"
$jpegextension = "jpg"
$RAWFolder = "C:\RAW folder\"
$RAWextension = "RAW"

$jpegArray = _FileListToArray ( $jpegFolder, "*." & $jpegextension, 1) ; create a list of files to check for "RAW" duplicates
$RAWArray = _FileListToArray ( $RAWFolder, "*." & $RAWextension, 1) ; create a list of RAW duplicates to delete. it starts as a full list.
for $i = 1 to $jpegArray[0] ; for each jpeg file found, remove it from the RAW delete list (because you want to keep it)
    $ArrFileName = StringSplit($jpegArray[$i],".") ;split the jpeg file name by the "." character, leaving only the pre-extension file name
    $RawFileName = $ArrFileName[1] & "." & $RAWextension ; add the extension to the raw file name
    $ArrayLocation = _ArraySearch($RAWArray,$RawFileName) ;define where the RAW file name was found at in the RAW array
    if $ArrayLocation >= 1 Then ; if the raw file name was found,
        _ArrayDelete($RAWArray,$ArrayLocation) ; delete the RAW file name from the list to be deleted
    EndIf
next
for $i = 1 to $RAWArray[0] ;for each RAW file still remaining in the delete list,
    if fileexists($RAWFolder & $RAWArray[$i]) then ;if the file exists,
        filedelete($RAWFolder & $RAWArray[$i]) ; delete it from the disk
    endif
Next
Edited by FlashpointBlack

Share this post


Link to post
Share on other sites

Thanks for your suggestions.

Share this post


Link to post
Share on other sites

I have not got into the details of the above scripts (I am reading about the functions being used).  Since this deals file deleting, which is always risky doing in a script, one approach I thought of would be to move the identified files to delete to another folder, and then checking for accuracy and then doing a regular Windows Explore delete.

The one question that came up was how to deal with filenames that have more than one period in their name.  To be safe, one would have to look for the first period by going backwards for the end.

Share this post


Link to post
Share on other sites

MilesAhead had a great function for returning a base file name he posted here (and I'm going to start using myself!)

Func _filebasename($path)
    If $path = "" Then Return ""
    If StringLen($path) < 3 Then Return ""
    Local $pos = StringInStr($path, "\", 0, -1)
    $pos += 1
    Local $tmp = StringMid($path, $pos)
    Return StringLeft($tmp, StringInStr($tmp, ".", 0, -1) - 1)
EndFunc
#include <File.au3>
#include <Array.au3>

$jpegFolder = "C:\JPG folder\"
$jpegextension = "jpg"
$RAWFolder = "C:\RAW folder\"
$RAWextension = "RAW"
$DestinationFolder = "C:\Scheduled For Deletion\"

$jpegArray = _FileListToArray ( $jpegFolder, "*." & $jpegextension, 1) ; create a list of files to check for "RAW" duplicates
$RAWArray = _FileListToArray ( $RAWFolder, "*." & $RAWextension, 1) ; create a list of RAW duplicates to delete. it starts as a full list.
for $i = 1 to $jpegArray[0] ; for each jpeg file found, remove it from the RAW delete list (because you want to keep it)
    $RawFileName = _filebasename($jpegArray[$i]) & "." & $RAWextension ; add the extension to the raw file name
    $ArrayLocation = _ArraySearch($RAWArray,$RawFileName) ;define where the RAW file name was found at in the RAW array
    if $ArrayLocation >= 1 Then ; if the raw file name was found,
        _ArrayDelete($RAWArray,$ArrayLocation) ; delete the RAW file name from the list to be deleted
    EndIf
next
for $i = 1 to $RAWArray[0] ;for each RAW file still remaining in the delete list,
    if fileexists($RAWFolder & $RAWArray[$i]) then ;if the file exists,
        FileMove($RAWFolder & $RAWArray[$i], $DestinationFolder & $RAWArray[$i],8) ; move it from the old folder to the new one
    endif
Next

Func _filebasename($path)
    If $path = "" Then Return ""
    If StringLen($path) < 3 Then Return ""
    Local $pos = StringInStr($path, "\", 0, -1)
    $pos += 1
    Local $tmp = StringMid($path, $pos)
    Return StringLeft($tmp, StringInStr($tmp, ".", 0, -1) - 1)
EndFunc

one thing I'd suggest is to look at the flags available for the FileMove() function. The only one I've set active is to make the directory if it doesn't exist.

Good luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0