Sign in to follow this  
Followers 0
Danyfirex

File Delete doesn't work correctly

14 posts in this topic

Hi mates, I mamade this script to delete files of any directories, but only work in some directories,

I use it in my desktop and work. But I can delete files of "C:\*txt", I don't find any mistake.

borrar("C:\Users\Usuario\Desktop\a.txt")


Func borrar($var)


local $mylg = FileOpen("log.log",1)
local $delimiter="\"

;Obtener Path
$array =StringSplit($var,$delimiter)
$len=UBound($array)-1
$numero= $len-1
$num=StringInStr($var,$delimiter,0,$numero)
$dir=StringMid($var,1,$num)



if FileExists($var) Then
$search = FileFindFirstFile($var)


While 1
Local $file = FileFindNextFile($search)
If @error Then ExitLoop
if FileDelete($file)=1 Then
filewrite($mylg,$dir&$file & @CRLF)

else
filewrite($mylg, $dir & $file & " No se Pudo borrar" & @CRLF)
EndIf
WEnd
EndIf


FileClose($search)
fileClose($mylg)
EndFunc

Share this post


Link to post
Share on other sites



try this

borrar("C:UsersUsuarioDesktop*.txt")

Share this post


Link to post
Share on other sites

try this

borrar("C:UsersUsuarioDesktop*.txt")

that work. but when I try in "C:*.txt", don't delete them.

Share this post


Link to post
Share on other sites

must be something with that $delimiter...

Share this post


Link to post
Share on other sites

must be something with that $delimiter...

$delimiter make some diferent is only to get de path.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

here example from help file with simple change

; Shows the filenames of all files in the current directory.
$dir = "C:"
$files = "*.txt"

$search = FileFindFirstFile($dir & $files)

; 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
;FileDelete($dir & $file)
;MsgBox(4096, "File:", $file)
ConsoleWrite($dir & $file & @CRLF)
WEnd

; Close the search handle
FileClose($search)
Edited by rvn

Share this post


Link to post
Share on other sites

here another script, search multiple ekstention in a folder, maybe it can help :)

search_all_eks("D:Test_Dir","txt,log",1)

Func search_all_eks($sPath,$sEks,$sShow = 0)
Local $search,$file,$result = "",$attrib,$eks,$i,$curEks,$found = 0,$curEksSplit
$eks = StringSplit($sEks,',')
$search = FileFindFirstFile($sPath & '*.*')
If $search = -1 Then
Return ""
EndIf
While 1
$file = FileFindNextFile($search)
If @error Then ExitLoop
$attrib = FileGetAttrib($sPath & '' & $file)
If @error Then

Else

If StringInStr($attrib, "D") Then
$result = $result & search_all_eks($sPath & '' & $file,$sEks,$sShow)
Else
$found = 0
$curEksSplit = StringSplit($file,'.')
$curEks = $curEksSplit[$curEksSplit[0]]
For $i = 1 To $eks[0]
If $curEks = $eks[$i] Then
$found = 1
ExitLoop
Else
$found = 0
EndIf
Next
If $found = 1 Then
If $sShow = 1 Then ConsoleWrite('+> ' & $sPath & '' & $file & @CRLF)
$result = $result & $sPath & '' & $file & '|'
Else
;ConsoleWrite('-> ' & $sPath & '' & $file & @CRLF)
EndIf
EndIf
EndIf
;$result = $result & $file & '|'
;MsgBox(4096, "File:", $file)
WEnd
FileClose($search)

Return $result
EndFunc

Share this post


Link to post
Share on other sites

here example from help file with simple change

; Shows the filenames of all files in the current directory.
$dir = "C:"
$files = "*.txt"

$search = FileFindFirstFile($dir & $files)

; 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
;FileDelete($dir & $file)
;MsgBox(4096, "File:", $file)
ConsoleWrite($dir & $file & @CRLF)
WEnd

; Close the search handle
FileClose($search)

this work but, I don't uderstand why I can't delete file on "C:*.txt" If on desktop I can.

Share this post


Link to post
Share on other sites

I don't uderstand why I can't delete file on "C:*.txt"

Perhaps you don't have permission to delete the files.


Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Share this post


Link to post
Share on other sites

Perhaps you don't have permission to delete the files.

I hava, cuz I try this deletefile("C:file.txt") Success

Share this post


Link to post
Share on other sites

Try using #RequireAdmin at the top of the script and see if you can delete the file from the root of the C: drive, it is probably a permissions issue as stated before. I see that you're on Windows Vista or 7, and there's a lot of restrictions regarding deleting and adding files to the C: root folder using those OSs.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I use #RequireAdmin But don't work.

but is rare because filedelete alone work, but in my function not work.,

I use w7 64x

this is a example of help files.

but I only chage the msgbox() for filedelet and don't work. with msgbox() work correctly

Local $search = FileFindFirstFile("C:public*.txt")

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

While 1
Local $file = FileFindNextFile($search)
If @error Then ExitLoop


FileDelete($file)
WEnd

; Close the search handle
FileClose($search)
Edited by Danyfirex

Share this post


Link to post
Share on other sites

You aren't specifying the folder name when you try to delete the file, FileFindNextFile returns file names without the path. Plus, you're not checking to see if the returned result is a folder or not, although I don't know if you have any folder names that end in .txt in the location you're looking.

FileDelete($file)
;should be written as
FileDelete("C:public" & $file)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

You aren't specifying the folder name when you try to delete the file, FileFindNextFile returns file names without the path. Plus, you're not checking to see if the returned result is a folder or not, although I don't know if you have any folder names that end in .txt in the location you're looking.

FileDelete($file)
;should be written as
FileDelete("C:public" & $file)

hahaha :S I'm a foolish. thank you so much BrewManNH you have good eyes.

thank you rvn, you told me, but I didn't notice.

here is my corrected code:

#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.8.1
Author: myName

Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here



borrar("C:public*.txt")


Func borrar($var)


local $mylg = FileOpen("log.log",1)


$dir=dir($var)




if FileExists($var) Then

$search = FileFindFirstFile($var)
$mydir=dir($var)

While 1
Local $file = FileFindNextFile($search)
If @error Then ExitLoop
if FileDelete($mydir&$file)=1 Then
filewrite($mylg,$dir&$file & @CRLF)

else
filewrite($mylg, $dir & $file & " No se Pudo borrar" & @CRLF)
EndIf
WEnd
EndIf


FileClose($search)
fileClose($mylg)
EndFunc

func dir($path)
local $delimiter=""
$array =StringSplit($path,$delimiter)
$len=UBound($array)-1
$numero= $len-1
$num=StringInStr($path,$delimiter,0,$numero)
$dir=StringMid($path,1,$num)
return $dir
EndFunc

thank you so much for helping me once more.

Edited by Danyfirex

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  
Followers 0