Jump to content
Sign in to follow this  
mdwerne

DirRemove from an array...

Recommended Posts

mdwerne

Can anyone with a better grasp of AutoIt see my mistake/s?

#include <File.au3>
Local $i
;Local $data = @HomeDrive & "\Dir1"|@HomeDrive & "\Dir2"|@HomeDrive & "\Dir3"|@HomeDrive & "\Dir4"
Local $data = '@HomeDrive & "\Dir1"|@HomeDrive & "\Dir2"|@HomeDrive & "\Dir3"|@HomeDrive & "\Dir4"'

Local $arr = StringSplit($data, "|")
For $i = 1 To $arr[0]
    Local $DelDir = DirRemove($arr[$i], 1)
    MsgBox("", "Result", $DelDir)
    If $DelDir = 1 Then
        ConsoleWrite($arr[$i] & @LF)
        _FileWriteLog(@HomeDrive & "\Delete.log", " Directory " & $arr[$i] & " was removed")
    EndIf
Next

I'm guessing it's in my $data variable, but I'm not sure what the correct syntax is... :)

Thanks for your time,

-Mike

Share this post


Link to post
Share on other sites
guinness

Declaring a variable can slow down the Loop, best to declare outside of the For..Next Loop.

And if you want the harder way :) >>

#include <File.au3>

Local $sData = '@HomeDrive & "\Dir1"|@HomeDrive & "\Dir2"|@HomeDrive & "\Dir3"|@HomeDrive & "\Dir4"'
Local $aArray = StringSplit($sData, "|"), $iDelete

For $i = 1 To $aArray[0]
    $aArray[$i] = StringReplace($aArray[$i], "@HomeDrive", @HomeDrive)
    $aArray[$i] = StringReplace($aArray[$i], '"', "")
    $aArray[$i] = StringReplace($aArray[$i], ' & ', "")
    $aArray[$i] = StringStripWS($aArray[$i], 7)

    ConsoleWrite($aArray[$i] & @CRLF)
    $iDelete = DirRemove($aArray[$i], 1)
    If $iDelete = 1 Then
        ConsoleWrite($aArray[$i] & @LF)
;~         _FileWriteLog(@HomeDrive & "\Delete.log", " Directory " & $arr[$i] & " was removed")
    EndIf
Next

But honestly why not use an Array like >>

$aArray[3] = [3, @HomeDrive & "\Dir1", @HomeDrive & "\Dir2", @HomeDrive & "\Dir3"]
Edited by guinness

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites
mdwerne

Declaring a variable can slow down the Loop, best to declare outside of the For..Next Loop.

And if you want the harder way :) >>

#include <File.au3>

Local $sData = '@HomeDrive & "\Dir1"|@HomeDrive & "\Dir2"|@HomeDrive & "\Dir3"|@HomeDrive & "\Dir4"'
Local $aArray = StringSplit($sData, "|"), $iDelete

For $i = 1 To $aArray[0]
    $aArray[$i] = StringReplace($aArray[$i], "@HomeDrive", @HomeDrive)
    $aArray[$i] = StringReplace($aArray[$i], '"', "")
    $aArray[$i] = StringReplace($aArray[$i], ' & ', "")
    $aArray[$i] = StringStripWS($aArray[$i], 7)

    ConsoleWrite($aArray[$i] & @CRLF)
    $iDelete = DirRemove($aArray[$i], 1)
    If $iDelete = 1 Then
        ConsoleWrite($aArray[$i] & @LF)
;~         _FileWriteLog(@HomeDrive & "\Delete.log", " Directory " & $arr[$i] & " was removed")
    EndIf
Next

But honestly why not use an Array like >>

$aArray[3] = [3, @HomeDrive & "\Dir1", @HomeDrive & "\Dir2", @HomeDrive & "\Dir3"]

So the hard way I think is above my grade...your way of doing an array seems very cool,but my array is actually a 100 or so directories and keeps growing so I didn't want to define the number of elements. Here is my stab at it...

$Array = StringSplit(@HomeDrive & "\Dir1", @HomeDrive & "\Dir2", @HomeDrive & "\Dir3", @HomeDrive & "\Dir4", ",")

For $i = 1 To $Array[0]
    Local $DelDir = DirRemove($Array[$i], 1)
    If $DelDir = 1 Then
        _FileWriteLog(@HomeDrive & "\Delete.log", " Directory " & $Array[$i] & " was removed")
    EndIf
Next

Am I way off?

Thanks for the help!!

-Mike

Edited by mdwerne

Share this post


Link to post
Share on other sites
guinness

With response to how to code an Array, with 100 elements this is how you would start it...

$aArray[101] = [100, @HomeDrive & "\Dir1", @HomeDrive & "\Dir2", ...

An Example with 20 Rows (elements)

#include <Array.au3>

Global $aArray[21] = [20, "1", "2", "3", "4", "5", "6", "7", "8", _
        "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]

_ArrayDisplay($aArray)

For $i = 1 To $aArray[0]
    ConsoleWrite($aArray[$i] & @CRLF)
Next
Edited by guinness

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites
mdwerne

Thank you both for the examples...yes, I misunderstood and misspoke. I believe I'm starting to see the light now. :)

-Mike

Share this post


Link to post
Share on other sites
ripdad

A hundred folders? This might work out better .. or something like it:

#include <array.au3>

Local $folders = EnumRootFolders(@HomeDrive)
_ArrayDisplay($folders)

$folders[1] = 'C:\MyKeyName-0001-2011'; Example with KeyName or Pointer (remove when ready)

For $i = 1 To $folders[0]
    If Not StringInStr($folders[$i], 'MyKeyName-') Then ContinueLoop
    MsgBox(0, 'Delete This Folder?', $folders[$i])
Next

Func EnumRootFolders($sPath)
    Local $string, $sFolder
    If StringRight($sPath, 1) <> '\' Then $sPath &= '\'
    Local $hF = FileFindFirstFile($sPath & '*')
    If $hF = -1 Then Return SetError(-1)
    While 1
        $sFolder = FileFindNextFile($hF)
        If @error Then ExitLoop
        If Not @extended Then ContinueLoop
        $string &= $sPath & $sFolder & '|'
    WEnd
    FileClose($hF)
    Return StringSplit(StringTrimRight($string, 1), '|')
EndFunc

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
mdwerne

A hundred folders? This might work out better .. or something like it:

#include <array.au3>

Local $folders = EnumRootFolders(@HomeDrive)
_ArrayDisplay($folders)

$folders[1] = 'C:\MyKeyName-0001-2011'; Example with KeyName or Pointer (remove when ready)

For $i = 1 To $folders[0]
    If Not StringInStr($folders[$i], 'MyKeyName-') Then ContinueLoop
    MsgBox(0, 'Delete This Folder?', $folders[$i])
Next

Func EnumRootFolders($sPath)
    Local $string, $sFolder
    If StringRight($sPath, 1) <> '\' Then $sPath &= '\'
    Local $hF = FileFindFirstFile($sPath & '*')
    If $hF = -1 Then Return SetError(-1)
    While 1
        $sFolder = FileFindNextFile($hF)
        If @error Then ExitLoop
        If Not @extended Then ContinueLoop
        $string &= $sPath & $sFolder & '|'
    WEnd
    FileClose($hF)
    Return StringSplit(StringTrimRight($string, 1), '|')
EndFunc

Thanks for the alternate method ripdad, I appreciate your input!

Share this post


Link to post
Share on other sites
ripdad

You're welcome.

Another suggestion ...

It's generally not a good idea to work off the root of your harddrive, especially with 100 folders.

Windows won't let you delete C:\Windows - but other files/folders would be at risk.

Many accidents can be avoided if you move your project/folders to a subfolder.

Example: C:\MyWorkFolder


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.