Jump to content

Recommended Posts

Guest prsobel
Posted

Might someone provide the syntax for removing the contents, files and subdirectories, of the Favorites folder located at D:\Relocated System Stuff\Favorites? The intent is to keep the folder, but to remove its contents. Thank you.

Posted

Use this code:

; Shows the filenames of all files in the current directory, note that "." and ".." are returned.
$location = "D:\Relocated System Stuff\Favorites\"
$search = FileFindFirstFile($location & "*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search)
    If @error Then ExitLoop
    If $file = "." Or $file = ".." Then ContinueLoop
    If Not DirRemove($location & $file, 1) Then FileDelete($location & $file)
WEnd

; Close the search handle
FileClose($search)
Guest prsobel
Posted

Thank you very much. It turns out that the DirRemoveContents function is no longer available.

Because the Favorites folder is a "system" folder it cannot be deleted, merely emptied and refilled. Might it be possible to change the Favorites folder's attributes from "system," replace it and then reassign the "system" attribute? I tried using FileSetAttrib without success.

In any event, thank you for your assistance.

  • 3 weeks later...
Posted

To have empty Favorites with _DirRemoveContents use:

$Dir = @FavoritesDir
_DirRemoveContents($Dir)

MsgBox(262144, "Complete", "Favorites cleaned")

Func _DirRemoveContents($sPath)
    
    Local $sPattern
    Local $sFile
    Local $hFile
    
    If StringRight($sPath, 1) <> "\" Then $sPath = $sPath & "\"
    $sPattern = $sPath & "*.*"
    
    If Not FileExists($sPath) Then Return 0
    
    FileDelete($sPattern)
    
    $hFile = FileFindFirstFile($sPattern)
    If @error Then Return 0
    
    While 1
        $sFile = FileFindNextFile($hFile)
        If @error Then ExitLoop
        
        If $sFile <> "." And $sFile <> ".." Then
            If StringInStr( FileGetAttrib($sPath & $sFile), "D") Then
                DirRemove($sPath & $sFile, 1)
            EndIf
        EndIf
    WEnd
    
    FileClose($hFile)
    Return 1
EndFunc

Posted

Thank you very much. It turns out that the DirRemoveContents function is no longer available.

I think that the function of eJan is easier... Because easily reusable. It is simply necessary to personalize the variable of directory.

----------------------GroumphyMore information about me [Fr]

Posted

Why not to also test :

Func _RemoveDirContents2($iDir)
; success : 1
; failure : 0
    Dim $iPath
    Local $iPath
    $iDir = $iPath
    Return DirRemove($iDir, 1); Delete the directory and all contents
    Return DirCreate($iPath); Create the directory (empty)
EndFunc; ==> _RemoveDirContents2("C:\test")

????

----------------------GroumphyMore information about me [Fr]

Posted (edited)

????

why do you need $iPath???

EDIT: Also. return DirRemove will alway end the function, without calling DirCreate.

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Posted

:(  Grrrrrr...

sorry ... :(

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Posted (edited)

It is not against you I'm sulky but again my script (I don't test it).

Why not try a function who call on other function (function in a function) same of here

Writing quickly and not tested :

Func _GlobaleFileRemoveContents2($iPath)
    Func _FileRemoveContents2($iPath)
        Return FileDelete($iPath & "\*.*")
    EndFunc
    Local _FileRemoveContents2($iPath)
    Return DirCreate($iPath)
EndFunc

... :(

Edited by Groumphy

----------------------GroumphyMore information about me [Fr]

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...