Jump to content

Remove files from directories


Recommended Posts

I have a single directory that I store stuff in and in that directory are sub directories with stuff stored in them. Those directories will contain a video file and a bunch of other garbage files. There may also be a sample video file in that directory as well.

I am wanting to create a script that I can pass a directory to it and then have it clean that directory out of the garbage. The files that can be in that directory are .txt files internet shortcut files and a sample video file or directory.

I want to keep the main video file and one other file which can be a .nfo file if that is present. This doesn't have to have a gui as it will be called from another program so a cmd line window would be fine.

Now if autoit is not what I need then please let me know and I will move on to find something else. This was recommended to me based on what I am using.

Thanks for taking the time to look at this and having paitence with a beginner.

Link to comment
Share on other sites

  • Replies 74
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Link to comment
Share on other sites

_FileListToArray ; Or search for _RecFileListToArray,

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Well after playing with _FileListToArray it revealed the extension for the shortcuts so I then made a test FileDelete script using that extension and it worked.

So what I have found out is the app that is going to use this script will pass the full path to the last directory created by it. Its passed as %1 from it. This will be enclosed in Double quotes "c:somedir". I don't believe that will be an issue. How do I get my .au3 file to read that parameter or do I have to compile it and make an exe out of it and use $CMDLINE[0] to get the path.

Link to comment
Share on other sites

Ok folks thanks for the help that got what I needed.

I didn't have to use the _FileListToArray at all as I had the full path to the directory I needed to delete the files in.

Global $iReturnValue
Local $path = $CMDLINE[1]
FileChangeDir($path)
$iReturnValue = FileDelete ("*.url")
Select
    Case $iReturnValue = 0
        MsgBox(0,"Deletion", "No Files Deleted", 10)
    Case $iReturnValue = 1
        MsgBox(0,"Deletetion", "Files Deleted", 10)
EndSelect

This script took me over an hour to write lol. I have not programmed in some 20 yrs. I wanted to make it indicate what the results of the file deletion was.

Now is there a way to put up a msgbox with out an ok button or is that not possible?

Link to comment
Share on other sites

The msgbox you posted has an ok button. :)

If you change the flag to 64 it will add a nice information icon.

Btw what is "Deletetion"? :P

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Its a misspelling lol. The editor doesn't have spell check lol. Might be a good suggestion

Now how can I do multiple file deletions. Like I may have txt files nfo files and possible other files in there in these directories. I still want to return successfull deletion or no deletions for each set of files deleted. This way if I am at the comp when this script runs I can see if its working or I could just write a log file instead and then can review it later. I might try that using a for loop and read an array with what I want to delete and write a log entry.

Thanks for the heads up on the msgbox. I for the life of me couldn't figure out how to get rid of the ok button. I tried the 64 and it still had an ok button.

Edited by sparker366
Link to comment
Share on other sites

Just a tip:

Instead of

Select
Case $iReturnValue = 0
MsgBox(0,"Deletion", "No Files Deleted", 10)
Case $iReturnValue = 1
MsgBox(0,"Deletion", "Files Deleted", 10)
EndSelect

You could use

Switch Int($iReturnValue)
Case 0
MsgBox(0,"Deletion", "No Files Deleted", 10)
Case 1
MsgBox(0,"Deletion", "Files Deleted", 10)
EndSwitch

It's not a big difference when you only have two options (even an if statement would be fine for that), but its usefulness would show if you had a dozen options.

The 64 is just to give it the information icon. By default, it will have an OK button, but you can specify for it to have other buttons. You cannot specify for it to be buttonless.

Edited by Rhyono
Link to comment
Share on other sites

Now to have a log it's going to require something more.

Something along this lines could work.

$pfile = "log.txt"
FileOpen($pfile, 2)
$aList = _RecFileListToArray($Opened_File, "*.bak;*.log;*.whatever", 1, $Recur, 2, 2)
If IsArray($aList) Then
For $u = 1 To $aList[0]
         FileWrite($pfile, $aList[$u] & @CRLF)
Next
EndIf
FileClose($pfile)
Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Well I tried if then elseif statements and it gave me errors for some reason. The Select statement worked fine for me. Not sure what the difference between the 2 is but I can change over to Switch thats not hard

Here is my current code I am not using msgbox instead using SplashText as its plainer

#comments-start
    This script will take path passed to it from the commandline then go to that directory
    and Delete any files other then a video and a nfo file.
#comments-end
Global $iReturnValue
Local $path = $CMDLINE[1]
FileChangeDir($path)
$iReturnValue = FileDelete ("*.txt")
Select
    Case $iReturnValue = 0
        SplashTextOn("Deletion", "No Files Deleted",100,100,700,500,"", 24)
        Sleep(5000)
        SplashOff()
    Case $iReturnValue = 1
        SplashTextOn("Deletion", "Files Deleted",100,100,700,500,"", 24)
        Sleep(5000)
        SplashOff()
EndSelect

_RecFileListToArray where can I find information on that? I did google it and found a post about it and someone passing the array to filedelete.

I just have to assume what type of video files are in that directory based on experience. Exclude them and the .nfo file from the created array and then delete those files in that array

When the resultant array is made is the [0] location in that array storing the amount of files added?

I was wondering this as I want to just run some tests on some directories to see what is in there and display the file list.

Edited by sparker366
Link to comment
Share on other sites

  • Moderators

sparker366,

You can find a link to RecFileListToArray in my sig below. The UDF allows you to include/exclude various file types from the returned array very easily - and yes, the [0] element is the number of files returned. Please ask if you have any problem using the UDF. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

So after figuring out I had to download that zip. Yes I was foggy headed this morning. I looked at the example in it and see at the top you have these 2 includes

#include <Array.au3>

#include "RecFileListToArray.au3"

Now any time I want to use RecFileListToArray I have to put those 2 includes in my script file? i also have to place the recFileListToArray.au3 in the /autoit3/include directory? Again its been a very long time since I have done any programming so am getting back up to speed.

Edited by sparker366
Link to comment
Share on other sites

  • Moderators

sparker366,

To use the UDF you only need #include RecFileListToArray - the other UDF is only in the example script to display the returned arrays. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Melba I have some questions about passing the include list to the function

I can have a directory that could have the following scenario A file with sample in its name, internet shortcuts (which end it .url and a directory. I need to delete those. i thought your function would be best to use to return an array with those items.

I tried this

Local $FileList = _RecFileListToArray($path, "*sample.*", "*.url"

and it didn't work

Now I can pass *sample.* to your function and it returns what I want. How can I tell it to return that and any directories in the directory? Or does that have to be 2 calls? I looked at your example file for some hints but came up short.

Now the resultant array or arrays the files within need to be deleted. I can't pass the array to FileDelete can I? I did try that and nothing was deleted. So I will have to use a for loop to go through the array if its more then 2 entries or just use Filedelet($filelist[1]) if its only one entry using an if statement to test that.

Again sorry for asking these newbish questions but I tried searching for examples of your function and game up short.

Is there a way you can indicate some sample calls to your function with multiple includes I really don't need alot of the other aspects of it at this time maybe down the line i may.

Again thanks for the help

Edited by sparker366
Link to comment
Share on other sites

  • Moderators

sparker366,

You need to read the UDF function header - that is why I wrote it! ;)

"$sMask - Optional: Filter for result. Multiple filters must be separated by " ; "

So in the case you stated, you would need:

Local $FileList = _RecFileListToArray($path, "*sample.*;*.url")

Now you will get all files that match those wildcards. :)

I am unsure why you want folders as well. If it is because you just want a list of the folders, then you will have to run then UDF again like this:

Local $FolderList = _RecFileListToArray($path, "*", 2)

But if you want to look for those filenames in all the subfolders on the path then you can do it very easily like this:

Local $FullFileList = _RecFileListToArray($path, "*sample.*;*.url", 1, 1) ; Note the $iRecur parameter is now set

That will search all the subfolders on the path and return all the matching files within them. ;)

Is that good enough? Keep asking if not. :)

M23

Edit:

And yes, you need to run a loop through the returned array in order to delete them - note the count is returned in the [0] element. ;)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks for the reply. I reread that function header and must have missed the " ; " to separate the multiple filters. I actually found that after I made this reply. Tested it and it worked. then you replied with that information Sorry about that

The reason I want folders as well as these directories are created by a program extracting archives and I only want the video file contained in it any images and one other file and nothing else. I have done tons of these directories and the .url files are the most prevalent files and there are sometimes directories in there as well. So 2 calls to the function won't be an issue. I was figuring that would have to be done.

So I would have to use a for loop if the [0] >1 to delete the items and just use $filelist[1] to delete a single element array item.

This will do the trick. Now if I can just get more efficient coding.

Whats the best way to learn Autoit?

Again thanks for your assistance with this and again I do apologize for not seeing the " ;" to separate items my first time reading the function header.

Link to comment
Share on other sites

  • Moderators

sparker366,

No need to apologise - the learning curve can be quite steep at the beginning! :D

As to the best way to learn AutoIt, reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at this excellent tutorial - you will find other tutorials in the Wiki (the link is at the top of the page). There are even video tutorials on YouTube if you prefer watching to reading. :)

And never be afraid to ask questions here - we might bark a bit if the question is really simple and you have not searched beforehand, but we seldom bite that hard! ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I for the life of me couldn't get it to work with my test directory. The directory I was passing the UDF had spaces in its name. Not sure why it did but it did. It finally dawned on me that the path could be the issue so I put a msgbox in displaying @error and @extended and that told me there was an error with the path. I looked at the directory and it had spaces in it and a -. I just tested it with 2 other directories with the spaces in there names and its giving me error 1 and extended 1. Not sure how these directories got spaces in there names but they did. I can pretty much say that the directories it will be looking at won't have spaces in them but there could be an odd ball one created.

I was also getting a subscript used with non Array variable error when testing it with the directory with spaces in it. That drove me up a wall till I started to dig into it further. Now that makes sense cause the array is never getting created thus when using a statement accessing the [0] entry in the array.

Now I guess I need to put some code in there to check the passed path to make sure there are no spaces in there and if there are replace them . or _. I will look into creating code for that and adding it after the cmdline is passed to my variable for the path. I guess I also need to put some code in there to check to may sure the UDF processed correctly every time its called. If it failed pop a msgbox up with the extended code and description

Man frustrating but fun. I spent hours on this with blindfolds on to the issue with the directory name. But using this UDF will make things alot easier on me as I can tailor the script for extra things.

Again thanks for all your help M23

Edited by sparker366
Link to comment
Share on other sites

  • Moderators

sparker366,

It seems your UDF may have issues with directories with spaces in the name

It copes perfectly with folder names including spaces - give me some credit. ;)

Can you give me a sample of a structure containing a folder you say you cannot read and the code you are using to try - I will take a look and see what I can find out for you. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I removed that statement after I posted my reply and then went back in and edited the post as I wasn't happy making that statement.

The full path gets passed from a python script to this script. So I just open a text file put the full path in there and paste it into a dos window after the script name and test my script.

The spaces in the directory name are an oddity. At this time I am looking at the directory that stores the files I am processing and there are quite a few directories created with spaces in them. These directories are created from unarchiving archives. So I have no control over the original archive. I am just going to test it in the script for spaces if there are space clean it up and rename the directory. The directory name example is d:worktempword1 word2 word3 - 1. The UDF returns an error 1 an extended 1. for that directory. i never tested for errors generated by your UDF so its my fault. I only respond to errors from Autoit

Local $path = $CMDLINE[1]
...
FileChangeDir($path)
$aArray = _RecFileListToArray($path, "*.url; *.txt; *.sfv; *.nzb")
$err = @error & " " & @extended
MsgBox(0, "Errors", $err)
_ArrayDisplay($aArray, "Files")

I put in the ... as there is some stuff after that variable assignment.

Edited by sparker366
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...