richietheprogrammer Posted August 20, 2012 Share Posted August 20, 2012 (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 August 20, 2012 by richietheprogrammer Link to comment Share on other sites More sharing options...
Andreik Posted August 20, 2012 Share Posted August 20, 2012 #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 richietheprogrammer 1 Link to comment Share on other sites More sharing options...
richietheprogrammer Posted August 20, 2012 Author Share Posted August 20, 2012 (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 August 20, 2012 by richietheprogrammer Link to comment Share on other sites More sharing options...
Andreik Posted August 20, 2012 Share Posted August 20, 2012 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? Link to comment Share on other sites More sharing options...
richietheprogrammer Posted August 20, 2012 Author Share Posted August 20, 2012 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. Link to comment Share on other sites More sharing options...
Andreik Posted August 20, 2012 Share Posted August 20, 2012 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 More sharing options...
richietheprogrammer Posted August 20, 2012 Author Share Posted August 20, 2012 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 More sharing options...
Andreik Posted August 20, 2012 Share Posted August 20, 2012 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 More sharing options...
richietheprogrammer Posted August 20, 2012 Author Share Posted August 20, 2012 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 More sharing options...
Andreik Posted August 20, 2012 Share Posted August 20, 2012 You just need to put the path between double quotes. Run(@ComSpec & ' /k Del "' &$sDir & '' & $Files[$Index] & '"', '', @SW_show) Link to comment Share on other sites More sharing options...
richietheprogrammer Posted August 20, 2012 Author Share Posted August 20, 2012 Well, what can I say, you just made my day !! Thank you so much! Link to comment Share on other sites More sharing options...
AZJIO Posted August 21, 2012 Share Posted August 21, 2012 (edited) #include <FileOperations.au3> $sDir = 'D:MyDir' $FileList = _FO_FileSearch($sDir, '*.doc', False) For $i = 1 To $FileList[0] FileDelete($FileList[$i]) Next Edited August 21, 2012 by AZJIO My other projects or all Link to comment Share on other sites More sharing options...
blademonkey Posted August 21, 2012 Share Posted August 21, 2012 (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 August 21, 2012 by blademonkey ---"Educate the Mind, Make Savage the Body" -Mao Tse Tung Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now