Jump to content

Remove all files except those with specific extension


Recommended Posts

Hello,

I am looking for a script to do the following:

1- Search in a directory and retrieve all files (_FileListToArray?)

2- Delete all the files that arent of extension ".doc"

3- Execute a cmd command on each of the remaining (doc) files.

Any help would be much appreciated!!

Edited by richietheprogrammer
Link to comment
Share on other sites

#include <File.au3>

$sDir = FileSelectFolder("Select","")
If @error Then Exit

$Counter = 1
$Files = _FileListToArray($sDir,"*",1)
For $Index = 1 To $Files[0]
    If StringRight($Files[$Index],4) = ".doc" Then
        FileMove($sDir & "" & $Files[$Index],$sDir & "Document_" & $Counter & ".doc")    ; Rename
        $Counter += 1
    Else
        FileDelete($sDir & "" & $Files[$Index])
    EndIf
Next

Link to comment
Share on other sites

Thank you much for the reply. Would there be a way to remove all other files, instead of moving all doc files to a new folder and wiping the root folder? Also, how would I go about executing a command on each of the files?

Can we maybe use

If StringRight($Files[$Index],4) <> ".doc"
FileDelete(?)

Thanks again for your help!

Edited by richietheprogrammer
Link to comment
Share on other sites

You mentioned something about renameing the doc files. In this case you need just this code.

#include <File.au3>

$sDir = FileSelectFolder("Select","")
If @error Then Exit

$Files = _FileListToArray($sDir,"*",1)
For $Index = 1 To $Files[0]
    If StringRight($Files[$Index],4) <> ".doc" Then
        FileDelete($sDir & "" & $Files[$Index])
    EndIf
Next
Link to comment
Share on other sites

You sir, are awesome! I apologize, I meant to say "remaining" not "renaming". Thats where the confusion happened. So the only left step is, how can I now loop through the remaining files and execute a command such as :

Run(@ComSpec & 'command+filename' , '', @SW_HIDE)

I am going to be applying a command from a third party program that runs in a command prompt. So the only question I have is, how do I loop through them and execute the command while changing it every time since it contains the file name? Thank you again for all your help!

Link to comment
Share on other sites

So you want for every doc file to run a DOS command that containt the name of file?

#include <File.au3>
#include <Process.au3>

$sDir = FileSelectFolder("Select","")
If @error Then Exit

$Files = _FileListToArray($sDir,"*",1)
For $Index = 1 To $Files[0]
    If StringRight($Files[$Index],4) <> ".doc" Then        ;if is not doc file then delete
        FileDelete($sDir & "" & $Files[$Index])
    Else                                                ;if is doc file then run DOS command
        ;  $sDir contain the directory name
        ;  $Files[$Index] contains the name of doc file
        _RunDos("command")    ; some DOS command
    EndIf
Next
Link to comment
Share on other sites

Thats exactly what I am looking for. One tiny issue, though, when I use the variable "sDir", my command is failing because "sDir" contains spaces. how can I escape the spaces in the path? For example, if the command is

Run(@ComSpec & ' /k Del '&$sDir &''&$Files[$Index]', '', @SW_show)

then it fails if $sDir's value is "New Folder" or anything with a space. Anyway to escape that?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...