Jump to content

Using FileDelete on a mapped network-drive


Go to solution Solved by orbs,

Recommended Posts

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)
Link to comment
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?

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
Link to comment
Share on other sites

  • Solution

use FileDelete with absolute path:

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
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! :-)

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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