Sign in to follow this  
Followers 0
Lou

Deleting a file after writing in it

3 posts in this topic

Hello,

My congratulations and enthousiastic thanks to the makers of AutoIt.

This is my 1st AutoIt script and I am glad to share it with you.

However it has bug: after writing to a text file, it doesn't want to delete it. (Pl. see FileDelete( $tempfile ) at the bottom of the script)

Thanks in advance for your help and suggestions,

Lou

CODE
#cs ----------------------------------------------------------------------------

AutoIt Version: 3.2.10.0

Author: Lou

Script Version: 0.0.1 buggy

Date: 2008-01-26

Script Function:

Help creating runas (root) shortcuts in the start menu

To Do:

#ce ----------------------------------------------------------------------------

#include <Array.au3>

#include <File.au3>

; CONFIGURATION

; The administrator's access data

$runasUSER = 'USER'

$runasDOMAIN = '@COMPUTER'

$runasPASSWD = InputBox ( "Password", "Enter the administrator's password", "", "*" )

; Various paths

$aut2exepath = @ProgramFilesDir & '\AutoIt3\Aut2Exe\Aut2exe.exe'

$tempdir = @TempDir & "\"

$exedir = "D:\Scripts\Autoit\lanceurs\"

;MAIN PROGRAM

; Dialog to choose a shortcut from Windows stard menu

$lnkfile = FileOpenDialog ( "Choose a shortcut", @StartMenuCommonDir, "Shortcuts (*.lnk)" )

; Compose $tempfile and $exefile and $newlnkfile paths

Dim $szDrive, $szDir, $szFName, $szExt

$pathelems = _PathSplit( $lnkfile, $szDrive, $szDir, $szFName, $szExt )

$tempfile = $tempdir & $pathelems[3] & ".au3"

$exefile = $exedir & $pathelems[3] & ".exe"

$newlnkfile = $pathelems[1] & $pathelems[2] & $pathelems[3] & ' (' & $runasUSER & ')' & $pathelems[4]

; Read parameters of chosen shortcut

$lnkparams = FileGetShortcut ( $lnkfile )

; Compose au3 script code

$tempcode = '; -- Temporary file --' & @LF & @LF

$tempcode &= '; Variables for RunAsSet:' & @LF

$tempcode &= '$user = "' & $runasUSER & '"' & @LF

$tempcode &= '$domain = "' & $runasDOMAIN & '"' & @LF

$tempcode &= '$password = "' & $runasPASSWD & '"' & @LF

$tempcode &= '$options = 1 ; 0 = do not load user profile, 1 = (default) load, 2 = use for net credentials only' & @LF & @LF

$tempcode &= '; Variable for Run:' & @LF

$tempcode &= '$filename = "' & $lnkparams[0] & '"' & @LF

$tempcode &= '$arguments = "' & $lnkparams[2] & '"' & @LF

$tempcode &= '$workingdir = "' & $lnkparams[1] & '"' & @LF

$tempcode &= ';$flag = "' & $lnkparams[6] & '"' & @LF & @LF

$tempcode &= 'If Not IsAdmin( ) Then' & @LF

$tempcode &= ' RunAsSet ( $user, $domain, $password, $options )' & @LF

$tempcode &= ' Run ( $filename & " " & $arguments, $workingdir )' & @LF

$tempcode &= 'EndIf' & @LF

; Create temporary file and write the code into it

_FileCreate ( $tempfile )

$FILE = FileOpen ( $tempfile, 2 )

If $FILE = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

FileWrite( $FILE , $tempcode )

FileClose( $tempfile )

; Compile the temporary file to an exe file

;ShellExecuteWait( @ProgramFilesDir & '\AutoIt3\Aut2Exe\Aut2exe.exe', '/in "' & $tempfile & '" /out "' & $exefile & '" /pass "' & $runasPASSWD & '" /unicode' )

$command = $aut2exepath & ' /in "' & $tempfile & '" /out "' & $exefile & '" /pass "' & $runasPASSWD & '" /unicode'

Run( $command )

; Fix missing icon

if $lnkparams[4] = '' Then $lnkparams[4] = $lnkparams[0]

; Create shortcut in Windows Start menu

FileCreateShortcut ( $exefile, $newlnkfile ,'' ,'' ,'' , $lnkparams[4],'' , $lnkparams[5], $lnkparams[6] )

; Delete temporary file

If FileDelete( $tempfile ) = 0 Then MsgBox( 0, "Error", "FileDelete failed" )

; Open folders for verifications

Run( "explorer " & $tempdir )

Run( "explorer " & $exedir )

Run( "explorer " & $pathelems[1] & $pathelems[2] )

Share this post


Link to post
Share on other sites



You FileClose the Filename in stead of the Filehandle:

FileClose($tempfile) should be FileClose($FILE)

Also use RunWait() in stead of Run() to ensure the process is finished before deleting it.

:D


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thank you Jos,

It worked flawlessly! :D

And there the corrected code:

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

 CreateRootShortcut.au3

 AutoIt Version: 3.2.10.0
 Author:         Lou 
 Script Version: 0.0.2 
 Date:           2008-01-26       

 Script Function:
    Help creating runas (root) shortcuts in the start menu
    
 To Do: 

#ce ----------------------------------------------------------------------------
#include <Array.au3>
#include <File.au3>

; CONFIGURATION
; The administrator's access data
$runasUSER   = 'USER'
$runasDOMAIN = '@COMPUTER'
$runasPASSWD = InputBox ( "Password", "Enter the administrator's password", "", "*" )
; Various paths
$aut2exepath = @ProgramFilesDir & '\AutoIt3\Aut2Exe\Aut2exe.exe'
$tempdir = @TempDir & "\"
$exedir = "D:\Scripts\Autoit\lanceurs\"

;MAIN PROGRAM
; Dialog to choose a shortcut from Windows stard menu
$lnkfile = FileOpenDialog ( "Choose a shortcut", @StartMenuCommonDir, "Shortcuts (*.lnk)"  )
; Compose $tempfile and $exefile and $newlnkfile paths
Dim $szDrive, $szDir, $szFName, $szExt
$pathelems = _PathSplit( $lnkfile, $szDrive, $szDir, $szFName, $szExt )
$tempfile = $tempdir & $pathelems[3] & ".au3"
$exefile  = $exedir & $pathelems[3] & ".exe"
$newlnkfile = $pathelems[1] & $pathelems[2] & $pathelems[3] & ' (' & $runasUSER & ')' & $pathelems[4]
; Read parameters of chosen shortcut
$lnkparams = FileGetShortcut ( $lnkfile )
; Compose au3 script code
$tempcode  = '; -- Temporary file --' & @LF & @LF
$tempcode &= '; Variables for RunAsSet:' & @LF 
$tempcode &= '$user       = "' & $runasUSER   & '"' & @LF
$tempcode &= '$domain     = "' & $runasDOMAIN & '"' & @LF
$tempcode &= '$password   = "' & $runasPASSWD & '"' & @LF
$tempcode &= '$options    = 1 ; 0 = do not load user profile, 1 = (default) load, 2 = use for net credentials only' & @LF & @LF
$tempcode &= '; Variable for Run:' & @LF
$tempcode &= '$filename   = "' & $lnkparams[0] & '"' & @LF
$tempcode &= '$arguments  = "' & $lnkparams[2] & '"' & @LF
$tempcode &= '$workingdir = "' & $lnkparams[1] & '"' & @LF 
$tempcode &= ';$flag       = "' & $lnkparams[6] & '"' & @LF & @LF
$tempcode &= 'If Not IsAdmin( ) Then' & @LF
$tempcode &= '    RunAsSet ( $user, $domain, $password, $options )' & @LF
$tempcode &= '  Run ( $filename & " " & $arguments, $workingdir )' & @LF
$tempcode &= 'EndIf' & @LF
; Create temporary file and write the code into it
_FileCreate ( $tempfile )
$FILE = FileOpen ( $tempfile, 2 )
If $FILE = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
FileWrite( $FILE , $tempcode )
FileClose( $FILE )
; Compile the temporary file to an exe file
;ShellExecuteWait( @ProgramFilesDir & '\AutoIt3\Aut2Exe\Aut2exe.exe', '/in "' & $tempfile & '" /out "' & $exefile & '" /pass "' & $runasPASSWD & '" /unicode' )
$command = $aut2exepath & ' /in "' & $tempfile & '" /out "' & $exefile & '" /pass "' & $runasPASSWD & '" /unicode'
RunWait( $command )
; Fix missing icon
if $lnkparams[4] = '' Then $lnkparams[4] = $lnkparams[0]
; Create shortcut in Windows Start menu
FileCreateShortcut ( $exefile, $newlnkfile ,'' ,'' ,'' , $lnkparams[4],'' , $lnkparams[5], $lnkparams[6] )
; Delete temporary file
If FileDelete( $tempfile ) = 0 Then MsgBox( 0, "Error", "FileDelete failed" )
; Open folders for verifications
;Run( "explorer " & $tempdir )
Run( "explorer " & $exedir )
Run( "explorer " & $pathelems[1] & $pathelems[2] )

;end of the code

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