Jump to content

Recommended Posts

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
Posted

#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

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
Posted

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:

Posted

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.

Posted

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!

Posted

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
Posted

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?

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

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
  • Recently Browsing   0 members

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