Jump to content
Sign in to follow this  
atnextc

Help with File "Close/Open/Delete"

Recommended Posts

atnextc

I have a script where I am adding several files into one file, and then deleting each of the multiple files and the final file once its contents have been read to the clipboard and pasted elsewhere.

Problem is that it's not deleting the files until the app is closed.  I"ve read the help file several times, and I've made changes but the files are still "locked"

Any help would be appreciated.

 

Sample code below.

Local $HardwareAdd
            Local $HardwareAdd2
            Local $HardwareAdd3
            Local $HardwareAdd4
            Local $HardwareAdd5
            Local $HardwareAdd6
            Local $HardwareAdd7
            Local $FinalHardwareAddConfig
            GUISetState(@SW_DISABLE)
            $FinalHardwareAddConfig = FileOpen(@TempDir & "\Final Hardware Add Config.txt", 1)

            ClipPut($HardwareAdd = FileOpen(@TempDir & "\Multiple Hardware Add.txt", 1))
            FileOpen(@TempDir & "\Final Hardware Add Config.txt", 1)
            Local $test = FileRead(@TempDir & "\Multiple Hardware Add.txt")
            FileWrite($FinalHardwareAddConfig, $test & @CRLF)

            ;FileClose(@TempDir & "\Final Hardware Add Config.txt")
            ;FileClose($FinalHardwareAddConfig)

            ClipPut($HardwareAdd2 = FileOpen(@TempDir & "\Multiple Hardware Add2.txt", 1))
            FileOpen(@TempDir & "\Final Hardware Add Config.txt", 1)
            Local $test = FileRead(@TempDir & "\Multiple Hardware Add2.txt")
            FileWrite($FinalHardwareAddConfig, $test & @CRLF)
            ;FileClose(@TempDir & "\Final Hardware Add Config.txt")
            ;FileClose($FinalHardwareAddConfig)

            ClipPut($HardwareAdd3 = FileOpen(@TempDir & "\Multiple Hardware Add3.txt", 1))
            FileOpen(@TempDir & "\Final Hardware Add Config.txt", 1)
            Local $test = FileRead(@TempDir & "\Multiple Hardware Add3.txt")
            FileWrite($FinalHardwareAddConfig, $test & @CRLF)
            ;FileClose(@TempDir & "\Final Hardware Add Config.txt")
            ;FileClose($FinalHardwareAddConfig)

            ClipPut($HardwareAdd4 = FileOpen(@TempDir & "\Multiple Hardware Add4.txt", 1))
            FileOpen(@TempDir & "\Final Hardware Add Config.txt", 1)
            Local $test = FileRead(@TempDir & "\Multiple Hardware Add4.txt")
            FileWrite($FinalHardwareAddConfig, $test & @CRLF)
            ;FileClose(@TempDir & "\Final Hardware Add Config.txt")
            ;FileClose($FinalHardwareAddConfig)

            ClipPut($HardwareAdd5 = FileOpen(@TempDir & "\Multiple Hardware Add5.txt", 1))
            FileOpen(@TempDir & "\Final Hardware Add Config.txt", 1)
            Local $test = FileRead(@TempDir & "\Multiple Hardware Add5.txt")
            FileWrite($FinalHardwareAddConfig, $test & @CRLF)
            ;FileClose(@TempDir & "\Final Hardware Add Config.txt")
            ;FileClose($FinalHardwareAddConfig)

            ClipPut($HardwareAdd6 = FileOpen(@TempDir & "\Multiple Hardware Add6.txt", 1))
            FileOpen(@TempDir & "\Final Hardware Add Config.txt", 1)
            Local $test = FileRead(@TempDir & "\Multiple Hardware Add6.txt")
            FileWrite($FinalHardwareAddConfig, $test & @CRLF)
            ;FileClose(@TempDir & "\Final Hardware Add Config.txt")
            ;FileClose($FinalHardwareAddConfig)

            ClipPut($HardwareAdd7 = FileOpen(@TempDir & "\Multiple Hardware Add7.txt", 1))
            FileOpen(@TempDir & "\Final Hardware Add Config.txt", 1)
            Local $test = FileRead(@TempDir & "\Multiple Hardware Add7.txt")
            FileWrite($FinalHardwareAddConfig, $test & @CRLF)
            ;FileClose(@TempDir & "\Final Hardware Add Config.txt")



            FileClose($HardwareAdd)
            FileClose($HardwareAdd2)
            FileClose($HardwareAdd3)
            FileClose($HardwareAdd4)
            FileClose($HardwareAdd5)
            FileClose($HardwareAdd6)
            FileClose($HardwareAdd7)
            FileClose($FinalHardwareAddConfig)
            Filedelete(@TempDir & "\Multiple Hardware Add.txt")
            Filedelete(@TempDir & "\Multiple Hardware Add2.txt")
            Filedelete(@TempDir & "\Multiple Hardware Add3.txt")
            Filedelete(@TempDir & "\Multiple Hardware Add4.txt")
            Filedelete(@TempDir & "\Multiple Hardware Add5.txt")
            Filedelete(@TempDir & "\Multiple Hardware Add6.txt")
            Filedelete(@TempDir & "\Multiple Hardware Add7.txt")

All of my data that I want is successfully written to the new file so that good, I just can't figure out why the files won't get deleted.

Edited by atnextc

Share this post


Link to post
Share on other sites
orbs

DOS internal copy command can concatenate files in a single command line.

anyway, i must confess i'm having trouble following the logic of the script, you declare func _CombinedFiles()

but you don't call it anywhere? and you use the clipboard for what, you have ClipPut but never ClipGet? and you declare $FinalHardwareAddConfig inside the func, but use it outside?

Share this post


Link to post
Share on other sites
atnextc

@orbs see updated post.

essentially i'm writing a router config, and the way i have to do it is write each part to a seperate file, and then coming them into one complete file, which is copied to the clipboard and pasted into an editbox during another part of the script. I'm just trying to figure out why the files aren't being deleted.

Share this post


Link to post
Share on other sites
atnextc

Edano every file opened is closed towards the bottom...am I not using file handles I was under the impression that I was

Share this post


Link to post
Share on other sites
atnextc

Why does the formatting haunt you... I'm open to better was of writing it

Share this post


Link to post
Share on other sites
Jos

you got it ? :)

Could you get yourself some chill-pills and take them at regular intervals?

Seriously: You are becoming pretty annoying around here with:

  • Your multiple subsequent replies in threads
  • Your so called opinion which often doesn't contribute to the topic.

 

So, please slow down, relax and stick to a single reply at the time. Use the Edit button when you forgot to mention something.

Jos 

Edited by Jos
  • Like 1

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
atnextc

Unfortunately the suggested option of creating the file in the same directory as the script isn't possible neither is the method of looping the file names as is this was just a small posting of the rest of the code which is over 5000 lines and the file names are hard coded for other purposes. But thank you for the reply :)

Share this post


Link to post
Share on other sites
BrewManNH

Try this, as it's a lot better than what you were doing, there's no need to use the clipboard for any part of this script, and as your files are named the same except for the final digit it's easy to create a loop to add them all to the main file and delete them as soon as you're done with them.

Local $HardwareAdd, $TempFileName, $FinalHardwareAddConfig
Local $FileName = @TempDir & "\Multiple Hardware Add"
GUISetState(@SW_DISABLE)
$FinalHardwareAddConfig = FileOpen(@TempDir & "\Final Hardware Add Config.txt", 1)
For $I = 1 To 7
    $TempFileName = $FileName
    If $I = 1 Then
        $HardwareAdd = FileOpen($TempFileName & ".txt")
    Else
        $TempFileName &= $I 
        $HardwareAdd = FileOpen($TempFileName & ".txt")
    EndIf
    Local $test = FileRead($HardwareAdd)
    FileWrite($FinalHardwareAddConfig, $test & @CRLF)
    FileClose($HardwareAdd)
    FileDelete($TempFileName & ".txt")
Next

FileClose $FinalHardwareAddConfig

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

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  

×