Sign in to follow this  
Followers 0
richietheprogrammer

Remove all files except those with specific extension

13 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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

1 person likes this

When the words fail... music speaks

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

There's no move of any file but FileMove it's used to rename the files (in your case doc files). All others files, with other extensions are deleted. Did you tried at least the code? :rolleyes:


When the words fail... music speaks

Share this post


Link to post
Share on other sites

Indeed I did. It works correctly, but the problem is, I dont want to rename the files. I want them to stay the way they were named. That's why Im wondering if it would be better not to touch them, by identifying all the files that are not .doc.

Share this post


Link to post
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

When the words fail... music speaks

Share this post


Link to post
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!

Share this post


Link to post
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

When the words fail... music speaks

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

You just need to put the path between double quotes.

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

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Well, what can I say, you just made my day !! Thank you so much!

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

here's the one liner in dos if you're interested.

for /f "tokens=*" %I in ('dir /b ^| find /v /i ".doc"') do del %I
Edited by blademonkey

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

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