Jump to content

Autoit problem with $Filename creation please.. help


gh0stid
 Share

Recommended Posts

Hello,

Sorry to bother you ... but .. I have a problem

im pretty sure its something you can figure out in least than 5second, but for a reason I just cant get.... I dont.

 

Look its pretty simple. the script does as follow : search for different folder and subdirectories for NSF files. once found, it log it and copy to to a $storage of my choice .

the problem is the following, theres is a "IF" if a file with the same name is found, it add _1 to the end of the filename... unfortunately for a reason i just dont get... I cant get it to work if I use anything else than : $Filename = @UserName & $suffixe & ".nsf""

 

I tried $aFiles $aNSFFiles etc.. nothing work. but @username does the increement..  ( around line 38 )

#include <Date.au3>

Local $logfile = "c:\bulk\NSF.log"
Local $storage = "C:\bulk\backup_test\" & @UserName & "\"

Local $hLogFile = FileOpen($logfile, 1) ;1 = append au fichier, utiliser 2 si on veut overwrite

; ##### Pour le traitement sur c:\ g:\ h:\ ################################################################################
;creation des variables de recherche a l'aide de la fonction homemade _DirGetSizeEx
FileWriteLine($hLogFile, "[" & _Now() & "] Recherche des fichiers NSF sur C:\ G:\ et H:\ pour l'utilisateur " & @USERNAME)
Local $aFiles = _DirGetSizeEx("c:\", "*.nsf", 1)
Local $aFilesG = _DirGetSizeEx("\\fs1\groupes\DTH\C - SGPI-SEFI\SGPI\PROJETS\T1200-99\T1206\4.0 Dents administratifs\spondance\", "*.nsf", 1)
Local $aFilesH = _DirGetSizeEx("\\fs2\home\SAVa3A0002\notes\", "*.nsf", 1)

If IsArray($aFiles) Then
    FileWriteLine($hLogFile, "[" & _Now() & "] -> " & $aFiles[0][0] & " fichiers trouves pour une taille de " & _ReadableSize($aFiles[0][1]))
    _ProcessNSFCopy($aFiles)
EndIf
If IsArray($aFilesG) Then
    FileWriteLine($hLogFile, "[" & _Now() & "] -> " & $aFilesG[0][0] & " fichiers trouves pour une taille de " & _ReadableSize($aFilesG[0][1]))
    _ProcessNSFCopyG($aFilesG)
EndIf
If IsArray($aFilesH) Then
    FileWriteLine($hLogFile, "[" & _Now() & "] -> " & $aFilesH[0][0] & " fichiers trouves pour une taille de " & _ReadableSize($aFilesH[0][1]))
    _ProcessNSFCopyH($aFilesH)
EndIf
; #################################################################################################################

FileWriteLine($hLogFile, "[" & _Now() & "] Fin")


Func _ProcessNSFCopy($aNSFFiles)
    Local Static $x = 0
    Local $suffixe
    ;Local $prefixe = $aFiles
    For $i = 1 To $aNSFFiles[0][0]
        If $x Then $suffixe = "_" & $x
        $sFileName = $aFiles & "-C" & $suffixe & ".nsf"

        FileWriteLine($hLogFile, "[" & _Now() & "] Copie de " & $aNSFFiles[$i][0] & " (" & _ReadableSize($aNSFFiles[$i][1]) & ") => " & $storage & $sFileName & @CRLF)
        If FileCopy($aNSFFiles[$i][0], $storage & $sFileName, 9) Then
            $sText = "Copie effectuee avec success"
        Else
            $sText = "ERREUR lors de la copie du fichier"
        EndIf
        FileWriteLine($hLogFile, "[" & _Now() & "] " & $sText)
        $x += 1
    Next

EndFunc

Func _ProcessNSFCopyG($aNSFFiles)
    Local Static $x = 0
    Local $suffixe
    For $i = 1 To $aNSFFiles[0][0]
        If $x Then $suffixe = "_" & $x
        $sFileName = @UserName & "-G" & $suffixe & ".nsf"

        FileWriteLine($hLogFile, "[" & _Now() & "] Copie de " & $aNSFFiles[$i][0] & " (" & _ReadableSize($aNSFFiles[$i][1]) & ") => " & $storage & $sFileName & @CRLF)
        If FileCopy($aNSFFiles[$i][0], $storage & $sFileName, 9) Then
            $sText = "Copie effectuee avec success"
        Else
            $sText = "ERREUR lors de la copie du fichier"
        EndIf
        FileWriteLine($hLogFile, "[" & _Now() & "] " & $sText)
        $x += 1
    Next

EndFunc

Func _ProcessNSFCopyH($aNSFFiles)
    Local Static $x = 0
    Local $suffixe
    For $i = 1 To $aNSFFiles[0][0]
        If $x Then $suffixe = "_" & $x
        $sFileName = @UserName & "-H" & $suffixe & ".nsf"

        FileWriteLine($hLogFile, "[" & _Now() & "] Copie de " & $aNSFFiles[$i][0] & " (" & _ReadableSize($aNSFFiles[$i][1]) & ") => " & $storage & $sFileName & @CRLF)
        If FileCopy($aNSFFiles[$i][0], $storage & $sFileName, 9) Then
            $sText = "Copie effectuee avec success"
        Else
            $sText = "ERREUR lors de la copie du fichier"
        EndIf
        FileWriteLine($hLogFile, "[" & _Now() & "] " & $sText)
        $x += 1
    Next

EndFunc
FileClose($hLogFile)


; #FUNCTION#====================================================================================================================
; Name ..........: _DirGetSizeEx ; Description ...: Returns the size in bytes of a file list by extension.
; Syntax ........: _DirGetSizeEx($sDir[, $sMask = "*"[, $iFlag = 0]])
; Parameters ....: $sDir                - The directory to search in.
;                  $sMask               - [optional] Filter for results. Default is "*" (all).
;                                         Filter for result. Multiple filters must be separated by ";"
;                                         Use "|" to separate 3 possible sets of filters: "Include|Exclude|Exclude_Folders"
;                                             Include = Files/Folders to include (default = "*" [all])
;                                             Exclude = Files/Folders to exclude (default = "" [none])
;                                             Exclude_Folders = only used if $iRecur = 1 AND $iReturn <> 2 to exclude defined folders (default = "" [none])
;                  $iFlag               - [optional] 0 (default) = Returns the size
;                                                    1 = Extended mode is On -> returns an array that contains the file of each file (see Remarks).
;                                                    2 = Don't get the size of files in subdirectories (recursive mode is Off)
; Return values .: Success = The size in bytes, or a single dimension array
;                  Failure = -1 and sets the @error flag to 1 if the path doesn't exist.
; Author ........: jguinch
; Remarks .......: If you use the extended mode then the array returned from this function is a single dimension array containing the followingelements:
;                    $aArray[0][0] = Files count
;                    $aArray[0][1] = Total files size
;                    $aArray[1][0] = Full name of 1st file
;                    $aArray[1][1] = Size of 1st file
;                    $aArray[2][0] = Full name of 2nd file
;                    $aArray[2][1] = Size of 2nd file
;                    $aArray[n][1] = Size of nth file
;                    $aArray[n][1] = Size of nth file
;===============================================================================================================================
Func _DirGetSizeEx($sDir, $sMask = "*", $iFlag = 0); OK
    If NOT FileExists($sDir) Then Return SetError(1, 0, -1)
    If NOT StringInStr(FileGetAttrib($sDir), "D") Then Return SetError(1, 0, -1)

    Local $iExtMode = BitAND($iFlag, 1) > 0
    Local $iRecMode = NOT BitAND($iFlag, 2) > 0
    Local $aDirs[1] = [ StringRegExpReplace($sDir, "\\$", "") ]
    Local $iCountDir = 0, $iCountFile = 0, $n = 0, $iSize = 0, $iFullSize = 0
    Local $aFiles[1][2] = [[0]]
    Local $hSearch, $sFileName, $sRegexFilesInclude, $sRegexFilesExclude = "^$", $sRegexFoldersExclude = "^$"

    Local $sRegexMask = StringReplace( StringReplace( StringReplace($sMask, "|", "\|") , "?", "\E(?:.|.?$)\Q"), "*", "\E.*?\Q")
    Local $aFilters = StringSplit($sRegexMask, "\|", 3)

    $sRegexFilesInclude = "(?i)^(?:" & StringRegExpReplace(StringReplace($aFilters[0], ";", "|") , "([^|]+)", "\\Q$1\\E") & ")$"
    If UBound($aFilters) > 1 Then $sRegexFilesExclude = "(?i)^(?:" & StringRegExpReplace(StringReplace($aFilters[1], ";", "|") , "([^|]+)", "\\Q$1\\E") & ")$"
    If UBound($aFilters) > 2 Then $sRegexFoldersExclude = "(?i)^(?:" & StringRegExpReplace(StringReplace($aFilters[2], ";", "|") , "([^|]+)", "\\Q$1\\E") & ")$"

    While 1
        $hSearch = FileFindFirstFile( $aDirs[$n]  & "\*.*"  )
        If $hSearch <> -1 Then

            While 1
                $sFileName = FileFindNextFile($hSearch)
                If @error Then ExitLoop

                If @Extended Then
                    If NOT StringRegExp($sFileName, $sRegexFoldersExclude) Then
                        $iCountDir += 1
                        If $iCountDir >= UBound($aDirs) Then Redim $aDirs[UBound($aDirs) * 2]
                        $aDirs[$iCountDir] = $aDirs[$n] & "\" & $sFileName
                    EndIf
                Else
                    If StringRegExp($sFileName, $sRegexFilesInclude) AND NOT StringRegExp($sFileName, $sRegexFilesExclude) Then
                        $iSize = FileGetSize($aDirs[$n] &  "\" &$sFileName)
                        $iFullSize += $iSize
                        If $iExtMode Then
                            $iCountFile += 1
                            If $iCountFile >= UBound($aFiles) Then Redim $aFiles[UBound($aFiles) * 2][2]
                            $aFiles[$iCountFile][0] = $aDirs[$n] &  "\" &$sFileName
                            $aFiles[$iCountFile][1] = $iSize
                        EndIf
                    EndIf
                EndIf
            WEnd

            If NOT $iRecMode Then ExitLoop
        EndIf

        FileClose($hSearch)

        If $n = $iCountDir Then ExitLoop
        $n += 1
    WEnd

    If NOT $iExtMode Then Return $iFullSize

    Redim $aFiles[$iCountFile + 1][2]
    $aFiles[0][0] = $iCountFile
    $aFiles[0][1] = $iFullSize
    Return $aFiles
EndFunc

Func _ReadableSize($iSize, $decimalplaces = 2)
    Local $aUnites = [1024, "octets", "Ko", "Mo", "Go", "To"]
    For $i = 2 To UBound($aUnites) - 1
        If $iSize < $aUnites[0] Then ExitLoop
        $iSize /= $aUnites[0]
    Next
    Return Round($iSize, $decimalplaces) & " " & $aUnites[$i - 1]
EndFunc

thank you ...

Link to comment
Share on other sites

Show your scipt's what you have tried. You need If then, FileExists and the concatenate Operator &, FileMove.  So where is your Problem? Ithink a look in the help, espacily the Exmple 2 of FileMove will help you.

Edited by AutoBert
Link to comment
Share on other sites

Yes, it is. 

thats actually my problem, i cannot figure what the hell should i use to get it work.

 

Func _ProcessNSFCopy($aNSFFiles)
    Local Static $x = 0
    Local $suffixe
    ;Local $prefixe = $aFiles
    For $i = 1 To $aNSFFiles[0][0]
        If $x Then $suffixe = "_" & $x
        $sFileName = $aNSFFiles[$i][0]

        FileWriteLine($hLogFile, "[" & _Now() & "] Copie de " & $aNSFFiles[$i][0] & " (" & _ReadableSize($aNSFFiles[$i][1]) & ") => " & $storage & $sFileName&@CRLF)
        If FileCopy($aNSFFiles[$i][0], $storage & $sFileName, 9) Then
            $sText = "Copie effectuee avec success"
        Else
            $sText = "ERREUR lors de la copie du fichier"
        EndIf
        FileWriteLine($hLogFile, "[" & _Now() & "] " & $sText)
        $x += 1
    Next

EndFunc

Using $aNSFFIles00  as $sFileName get me the fullpath of the file .. so in my log ( debug ) its shown as that : 

[2015-10-21 11:55:13] Copie de c:\Notes\data\Help\readme.nsf (6 Mo) => C:\bulk\backup_test\poa11546\c:\Notes\data\Help\readme.nsf
[2015-10-21 11:55:13] ERREUR lors de la copie du fichier

 

obviously, its not working 

im completely stuck at the point.. i just want to copy the file using the same exact name and if theres duplicate add _($x) before .nsf

thatssss it

 

I cant seem to see where is my error...

Link to comment
Share on other sites

Sorry i don't see the spoiler. I had a quick look in your script, in line 38 you use $aFiles, bvut isn't this an array?

Please ?

pretty, please ?

 

haha .. sorry .. im just.. really pissed that I cant figure it out my self.. been on it for the last 4hours... trying here and here.. im no coder....

Link to comment
Share on other sites

So, your problem is that it's including the full path to the file in the log and all  you want is the file name?

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

Link to comment
Share on other sites

So, your problem is that it's including the full path to the file in the log and all  you want is the file name?

a little bit more than that.

 

my problem was that the fullname was indeed returned cause its an array but I think i solved it using : 

Func _ProcessNSFCopy($aNSFFiles)
    Local Static $x = 0
    Local $suffixe


*** For $i = 1 To $aNSFFiles[0][0]
        If $x Then $suffixe = "_" & $x
***     $sFileName = $aNSFFiles[$i][0]
***     $sFileName = StringRegExpReplace($sFileName, "^.*\\(.*)$", "$1")
***     $sFileName = $sFilename & $suffixe

        FileWriteLine($hLogFile, "[" & _Now() & "] Copie de " & $aNSFFiles[$i][0] & " (" & _ReadableSize($aNSFFiles[$i][1]) & ") => " & $storage & $sFileName&@CRLF)

        If FileCopy($aNSFFiles[$i][0], $storage & $sFileName, 9) Then
            $sText = "Copie effectuee avec success"
        Else
            $sText = "ERREUR lors de la copie du fichier"
        EndIf
        FileWriteLine($hLogFile, "[" & _Now() & "] " & $sText)
        $x += 1
    Next

The star, are the emphasys on the change I did.

STILL the problem remain, simply put. my script scan for all files with .NSF extension

put em in an array, ( so i can get the size of those later )
then COPY them to a new location $storage and finaly LOG it to a $logfile

my problem, is in the management of duplicate name possibility.

hence the :

For $i = 1 To $aNSFFiles[0][0]
        If $x Then $suffixe = "_" & $x
        $sFileName = $aNSFFiles[$i][0]
        $sFileName = StringRegExpReplace($sFileName, "^.*\\(.*)$", "$1")
        $sFileName = $sFilename & $suffixe

intended to correct that possibility, simply put, IF a duplicate names exist it simply add _1 _2 _3 _4 and so on...

unfortunately, that part doesnt work anymore, ( too much change to my code ) the "first" version posted was working, but only with @username & $suffixe 

 

im really sorry if im unclear.. but long story made short. I need it to keep the name of the files it found, and rename it *_1.nsf *_2 etc in case ONLY of duplicate...

english isnt my first language, sorry for that

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

×
×
  • Create New...