Sign in to follow this  
Followers 0
XorpiZ

Using FileDelete on a mapped network-drive

10 posts in this topic

Hi,

I've developed a small script that reads several lines from a textfile and then deletes any files matching those lines from a specific folder. It works splendidly on my local disk, but when I try running it on a mapped network-drive it's unable to delete any files.

If I run the script on ie. c:testfolder FileFindFirstFile will return a "handle" if a match is found. If I run the script on M:testfolder (M: is a mapped drive from our server) it will always return -1.

I have full rights to the folder.

Any ideas? I realize it's not 'comme il faut' to post a question as your first post on a forum.. but I'm really curious as to why it's not working :-) Also, the code below isn't beautiful.. :-)

BR,

Thomas

#include <File.au3>

$file = FileOpenDialog("Choose the file containing the list", "c:\", "Text files (*.txt)");
FileOpen($file, 0); Open the file
$folder = FileSelectFolder("Choose the folder containing the files", "")
Local $search = FileFindFirstFile($folder & "\*.*"); Make a list of files
MsgBox(0, "", $folder)
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern");If empty folder, show error
    Exit
EndIf

$counter = 0

For $i = 1 to _FileCountLines($file) ;Loop through the file
    $line = FileReadLine($file, $i) ;Get line $i
    $filesearch = FileFindFirstFile($line & "*");Search for $line + wildcard
    If $filesearch <> -1 Then;Check if match found
        FileDelete("*" & $line & "*");Delete any matching files
        $counter += 1
    Else
        ;MsgBox(0, "", "Mappe: " & $folder & " --- Filnavn: " & $line) DEBUG - Can be ignored
    EndIf
Next
If $counter == 0 Then
    MsgBox(0, "", "No files were found!");
    Else
        MsgBox(0, "", "Done! Deleted " & $counter & " unique files.")
EndIf
FileClose($file)

Share this post


Link to post
Share on other sites



Just a follow up - If I manually write the filename - FileDelete($folder & "filename.txt") it works just fine, so it could perhaps be the FileFindFirstFile that's having issues searching on a mapped drive?

Share this post


Link to post
Share on other sites

Welcome to AutoIt and the forum!

In the loop you should replace FileFindFirstFile with FileFindNextFile.

FileFindFirstFile creates a list of files and returns a handle, FileFindNextFile returns entry by entry of this list, FileFindClose releases the handle and the list.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Welcome to AutoIt and the forum!

In the loop you should replace FileFindFirstFile with FileFindNextFile.

FileFindFirstFile creates a list of files and returns a handle, FileFindNextFile returns entry by entry of this list, FileFindClose releases the handle and the list.

 

Hi, and thanks! :-)

I just replaced it with "FileFindNextFile", and now I do get results that match my expectations. But FileDelete doesn't do anything now :(

If $filesearch <> -1 Then;Check if match found
    FileDelete("*" & $line & "*");Delete any matching files
    $counter += 1
Endif

Share this post


Link to post
Share on other sites

use FileDelete with absolute path:

FileDelete($folder & "*" & $line & "*");Delete any matching files

Share this post


Link to post
Share on other sites

You need to pass the handle not the filename:

#include <File.au3>

$file = FileOpenDialog("Choose the file containing the list", "c:\", "Text files (*.txt)");
FileOpen($file, 0); Open the file
$folder = FileSelectFolder("Choose the folder containing the files", "")
Local $search = FileFindFirstFile($folder & "\*.*"); Make a list of files
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern");If empty folder, show error
    Exit
EndIf

$counter = 0

For $i = 1 to _FileCountLines($file) ;Loop through the file
    $line = FileReadLine($file, $i) ;Get line $i
    $filesearch = FileFindNextFile($search)
    If $filesearch <> -1 Then;Check if match found
        FileDelete($filesearch);Delete any matching files
        $counter += 1
    Else
        ;MsgBox(0, "", "Mappe: " & $folder & " --- Filnavn: " & $line) DEBUG - Can be ignored
    EndIf
Next
If $counter == 0 Then
    MsgBox(0, "", "No files were found!");
    Else
        MsgBox(0, "", "Done! Deleted " & $counter & " unique files.")
EndIf
FileClose($search)
FileClose($file)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

What I don't get is what you want to do.
Delete all files in the folder the user selects or delete the files you take from the text files?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

water, does FileDelete (and FileCopy, FileMove & friends) support file handles? the help file does not mention that...

Share this post


Link to post
Share on other sites

use FileDelete with absolute path:

FileDelete($folder & "*" & $line & "*");Delete any matching files

 

Thanks! That did the trick! :-)

 

What I don't get is what you want to do.

Delete all files in the folder the user selects or delete the files you take from the text files?

 

I have a textfile containing anything from 1 to 300 hundred filenames, each on their own line.

I need the script to read each line from the textfile and then delete any file matching that filename in the folder that the user selects.

I couldn't get it to work by passing the handle to FileDelete, but giving it an absolute path worked. I'm a bit curious as to why it didn't need an absolute path when working with local files though, but I guess that's one of the big mysteries of life! :-)

Share this post


Link to post
Share on other sites

You could then strip down your script a bit:

#include <File.au3>
Global $aFileToDelete, $iCounter = 0
$sFile = FileOpenDialog("Choose the file containing the list", "c:\", "Text files (*.txt)");
If $sFile = "" Or @error Then Exit
_FileReadToArray($sFile, $aFileToDelete)
If @error Then Exit MsgBox(16, "Error", "Error reading " & $sFile)
$sFolder = FileSelectFolder("Choose the folder containing the files", "")
If $sFolder = "" Or @error Then Exit
For $i = 1 To $aFileToDelete[0] ; Loop through the file
    If FileDelete($sFolder & "\" & $aFileToDelete[$i] & "*") <> 0 Then $iCounter += 1 ; Delete any matching files and increment counter if successful
Next
If $iCounter = 0 Then
    MsgBox(64, "Information", "No files were deleted!");
Else
    MsgBox(64, "Information", "Done! Deleted " & $iCounter & " unique files.")
EndIf

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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