Sign in to follow this  
Followers 0
mikep56

Why won't this file delete?

7 posts in this topic

#1 ·  Posted (edited)

Hi All,

Let me start off by saying that this is a great tool and a great forum.

I am a novice at best. I have written this script that will run the net view command, run the ping command, and then put the host names and IP addresses of all of the PCs in use on my home network. A neat little program to see who is on the network. There are 2 bugs that I just cannot figure out:

1)On the line that starts '$ping6' sometimes I get a subscript out of range error, sometimes it runs fine. These are consecutive runs one right after the other; nothing is changed.

2) Why won't the file 'netlist.txt' be deleted? The commented lines at the bottom show that the archive attribute is set, and the 'FileSetAttrib' command removes the archive attribute, yet it still remains on the desktop until I manually remove it.

EDIT: One more question: How do you determine programatically how many subarrays of $array1 the 'StringExplode' command creates. Basically this command cuts a string into substrings separated by defined delimiters. This command is found in the UDF section.

Please feel free to bend, fold , spindle, mutilate, copy or do whatever with this file. However I am not responsible for any damage, unforseen circumstances, etc. incurred as a result of the use of this file.

I have attached a copy, and also copied the program below.

Any help would be appreciated.

Thanks,

Mike

#include <String.au3>

#include <array.au3>

Run("cmd.exe","",@SW_ENABLE)

Sleep(1000)

Send("net view > netlist.txt")

Send("{ENTER}")

Sleep(3000)

ProcessClose("cmd.exe")

$file=FileOpen("netlist.txt",0)

$file=FileRead("netlist.txt")

FileClose($file)

$array1 = _StringExplode($file, "\\",0)

$ping1=StringMid($array1[1],1,15)

$ping2=StringMid($array1[2],1,15)

$ping3=StringMid($array1[3],1,15)

$ping4=StringMid($array1[4],1,15)

$ping5=StringMid($array1[5],1,15)

$ping6=StringMid($array1[6],1,15) ;WHY IS THIS CAUSE AN ERROR?

;CAN'T FIGURE OUT HOW TO DELETE THE NETLIST.TXT FILE FROM THE DESKTOP

TCPStartup()

$IPAddress=TCPNameToIP($ping1)

$final1=$ping1&@TAB&@TAB&$IPAddress

$IPAddress=TCPNameToIP($ping2)

$final2=$ping2&@TAB&@TAB&$IPAddress

$IPAddress=TCPNameToIP($ping3)

$final3=$ping3&@TAB&@TAB&$IPAddress

$IPAddress=TCPNameToIP($ping4)

$final4=$ping4&@TAB&@TAB&$IPAddress

$IPAddress=TCPNameToIP($ping5)

$final5=$ping5&@TAB&@TAB&$IPAddress

$IPAddress=TCPNameToIP($ping6)

$final6=$ping6&@TAB&@TAB&$IPAddress

$full_listing=$final1&@CR&$final2&@CR&$final3&@CR&$final4&@CR&$final5&@CR&$final6

MsgBox(0,"IP Addresses In Use",$full_listing)

;$attrib = FileGetAttrib("netlist.txt")

;MsgBox(0,"Full file attributes:", $attrib)

;FileSetAttrib("netlist.txt","-A")

FileDelete($file)

FileDelete("netlist.txt")

TCPNameToIP.au3

Edited by mikep56

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Your FileDelete command is trying to delete the file by the FileOpen handle, you have to use the path to the file you can't use a handle. Even if you could, you closed the file handle so it doesn't even point to the file any longer. In looking at the code closer, I was mistaken, you never actually close the file, because you reused the variable $file to hold the contents of the FileRead command. You still need to close the file before you can actually delete it first, then you need to delete the file by its pathname and file name, not the handle.

As to determining how large the array is, use Ubound($array1) - 1 to get the last element of the array.

Edited by BrewManNH

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

Hi BrewManNH,

Thanks for the quick response! OK I have gotten the array count working; but I still can't delete the file 'netlist.txt' from the desktop. I commented out the 'FileClose' command and specified the full path to the file, but it just stays on the desktop. The command'Send("net view > netlist.txt")' send the textfile 'netlist.txt' to the desktop. It is a text file, not a string, so the 'FileClose' command shouldn't affect it, right?

Regards,

Mike

Share this post


Link to post
Share on other sites

The file will be locked until either the filehandle is closed properly or the script exits. So to delete it you will need to close it. I would use a different variable for the fileopen command than the one you use for the fileread command so that you can close it properly.


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

Hi BrewManNH,

OK, I did what you suggested, but the 'netlist.txt' file stubbornly refuses to be deleted. Please see the attachment for the mods. What am I missing here?

Regards,

Mike

TCPNameToIP2.au3

Share this post


Link to post
Share on other sites

Hi mikep56,

Here is a working example that I tested which was based from your script. I added loops to handle repetitive code. I added the use of StdOutRead() which may avoid the use of a temp file.

Global $file_contents
Global $use_temp_file = False

If $use_temp_file Then
    ; save server list to a temp file
    RunWait("cmd /c net view > netlist.txt", "", @SW_HIDE)
    ; open file handle for read
    $file_read_handle = FileOpen("netlist.txt", 0)
    ; check if file handle is invalid
    If $file_read_handle = -1 Then
        Exit 1
    EndIf
    ; read the contents of the file by using the file handle
    $file_contents = FileRead($file_read_handle)
    ; close the file handle
    FileClose($file_read_handle)
    ; delete the temp file
    FileDelete("netlist.txt")
Else
    ; save server list to a variable using StdOutRead()
    $stdout_handle = Run("cmd /c net view", "", @SW_HIDE, 2)
    Do
        $file_contents &= StdOutRead($stdout_handle)
    Until @error
EndIf

; discard "\\" and then capture letters or underscores from 1 to 13
$array_of_servers = StringRegExp($file_contents, '\\\\(\w{1,13})', 3)
If @error Then Exit 2

Global $full_listing

; resolve server names to IP addresses
If Not TCPStartup() Then Exit 3
For $index = 0 To UBound($array_of_servers) -1
    $IPAddress = TCPNameToIP($array_of_servers[$index])
    $full_listing &= $array_of_servers[$index] & @TAB & @TAB & $IPAddress & @CRLF
Next
TCPShutdown()

; show result of server names with IP addresses
MsgBox(0,"IP Addresses In Use", $full_listing)

This is how you use a file handle (without error checking). The handle is in bold blue. If you get a handle then use the handle.

$file_read_handle = FileOpen("netlist.txt", 0)

$file_contents = FileRead( $file_read_handle )

FileClose( $file_read_handle )

  • You can get a handle when you open the file, read or write using the handle and then you close the handle.
  • You can just read or write to a file without using FileOpen() and FileClose(). You would use a string as a filename parameter.
You can do the prior or the later. You should not substitute strings with handles or vice versa. Correct use of file handles may help to avoid possible file delete issues. :unsure:

Share this post


Link to post
Share on other sites

Hi MHz,

Thank you very much for working this out for me. I am really a hardware guy, and in no stretch of the imagination do I consider myself a software guy.

Thanks again to both MHz and BrewManNH.

Regards,

mikep56

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