Jump to content

Appending username to a file


 Share

Recommended Posts

Hi,

I'm looking to copy a file from a local PC to a network share using an AutoIT exe. That in itself is easy - but as multiple people will be writing to this share, I'd like to append the currently-logged-in username to the filename that is being copied to the share. I have searched but am either missing something or perhaps it can't be done?

Anyhow, any help is appreciated. Thank you!

Link to comment
Share on other sites

One way with a useful UDF (not mine)

$FileName = 'C:\My Documents\Report.doc'

$SplitName = _PathSplitByRegExp($FileName)
If Not IsArray($SplitName) Then Exit

$NewName = $SplitName[6] & '-' & @USERNAME & '.' & $SplitName[7]
MsgBox(0, 'New Name of file', $NewName)





;===============================================================================
; Function Name:    _PathSplitByRegExp()
; Description:      Split the path to 8 elements.
; Parameter(s):     $sPath - Path to split.
; Requirement(s):   AutoIt 3.2.2.0.
; Return Value(s):  On seccess - Array $RetArray that contain 8 elements:
;               $RetArray[0] = Full path ($sPath)
;               $RetArray[1] = Drive letter
;               $RetArray[2] = Path without FileName and extension
;               $RetArray[3] = Full path without File Extension
;               $RetArray[4] = Full path without drive letter
;               $RetArray[5] = FileName and extension
;               $RetArray[6] = Just Filename
;               $RetArray[7] = Just Extension of a file
;
;               On failure - If $sPath not include correct path (the path is not splitable),
;               then $sPath returned.
;               If $sPath not include needed delimiters, or it's emty,
;               then @error set to 1, and returned -1.
;
; Note(s):      The path can include backslash as well (exmp: C:/test/test.zip).
;
; Author(s):        G.Sandler a.k.a CreatoR (MsCreatoR) - Thanks to amel27 for help with RegExp
;===============================================================================
Func _PathSplitByRegExp($sPath)
    If $sPath = "" Or (StringInStr($sPath, "\") And StringInStr($sPath, "/")) Then Return SetError(1, 0, -1)
    Local $RetArray[8], $pDelim = ""
    If StringRegExp($sPath, '^(?i)([A-Z]:|\\)(\\[^\\]+)+$') Then $pDelim = "\"
    If StringRegExp($sPath, '(?i)(^.*:/)(/[^/]+)+$') Then $pDelim = "//"
    If $pDelim = "" Then $pDelim = "/"
    If Not StringInStr($sPath, $pDelim) Then Return $sPath
    If $pDelim = "\" Then $pDelim &= "\"
    $RetArray[0] = $sPath
    $RetArray[1] = StringReplace($sPath, StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*|\\w*\\)', ''), "")
    $RetArray[2] = StringRegExpReplace($sPath, $pDelim & '[^' & $pDelim & ']*$', '')
    $RetArray[3] = StringRegExpReplace($sPath, '\.[^.]*$', '')
    $RetArray[4] = StringRegExpReplace($sPath, '(?i)([A-Z]:' & $pDelim & '|\\\\\w*\\|\\w*\\)', '')
    $RetArray[5] = StringRegExpReplace($sPath, '^.*' & $pDelim, '')
    $RetArray[6] = StringRegExpReplace($RetArray[5], '\.[^.]*$', '')
    $RetArray[7] = StringRegExpReplace($sPath, '^.*\.', '')
    Return $RetArray
EndFunc   ;==>_PathSplitByRegExp

Link to comment
Share on other sites

Thanks to both of you for your help! I've done a little bit of both. As soon as I've got it finalized I'll drop the code back here!

Ok, this is what I've put together based on code suggestions from both of you, and from code posted by jgira from here:

FileCopy("C:\Program Files\Can_Be_Any_Dir\sample_file.txt", "\\network_share\outlook$\sample_file - " & @USERNAME)
#include <INet.au3> 

$s_SmtpServer = "your_smtp_server_name" 
$s_FromName = "Who the email notification should appear to be from" 
$s_FromAddress = "Reply-to address" 
$s_ToAddress = "Address of person who should receive notification" 
$s_Subject = "Test Email" 
Dim $as_Body[2] 

; From -- _INetSmtpMail() in the Inet.au3 UDF 
Local $aResult = _Date_Time_GetTimeZoneInformation() 
Local $bias = -$aResult[1] / 60 
Local $biasH = Int($bias) 
Local $biasM = 0 
If $biasH <> $bias Then $biasM = Abs($bias - $biasH) * 60 
$bias = StringFormat(" (%+.2d%.2d)", $biasH, $biasM) 

; Debug display, single quotes added to see leading white space 
$as_Body[0] = "File deposited by " & @USERNAME

; As actually used in the function: 
$as_Body[1] = "Task performed on " & _DateDayOfWeek(@WDAY, 1) & ", " & @MDAY & " " & _DateToMonth(@MON, 1) & " " & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & $bias 

$Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body,"","-1") 
$err = @error 
If $Response = 1 Then 
Else 
EndIf

Thank you to everyone for the help!

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