Jump to content

FileFindFirstFile & FileGetTime (solved)


 Share

Recommended Posts

I'm trying to move the files from one directory into another based on their timestamp. If I run this from the source directory and use FileFindFirstFile("*.dss") it works. If I change that to a path, it returns a 1 which seems good but then the $t = FileGetTime($file, 0) line doesn't work. I don't understand what is going on here. I've read a bunch of FileFindFirst File topics but don't see how mine is different.

I want to have the source path in there because a user need to run it and I don't want to run the risk of her moving it out of the directory and suddenly moving all sorts of files from her desktop or wherever into the folders on the server.

$sourcePath = "\\Dictations\Backups\Backup\"
$destinationPath = "\\Dictations\Backups\FolderA\"

$search = FileFindFirstFile($sourcePath & "*.dss") ;;Everything works fine if this is "*.dss"

If $search = -1 Then
MsgBox(0, "Error", "No files/directories matched the search pattern")
Exit
EndIf
While 1
$file = FileFindNextFile($search)
If @error Then ExitLoop

  ;Get the date of the source file which will be the destination folder name
  $t =  FileGetTime($file, 0)
    If Not @error Then
   $yyyymd = $t[0] & $t[1]& $t[2]
  Else
   MsgBox(0, "Error", "Can't get the date")
  EndIf
  ;See if destination folder exsists, create folder if it doesn't
  If FileExists($destinationPath & $yyyymd) Then
  Else
   ;MsgBox(4096,"Creating dir" , "Creating directory \\Dictations\Backups\FolderA\" & $yyyymd)
   DirCreate($destinationPath & $yyyymd)
  EndIf
  ; See if the file exists at destination, if not then move it there
  If FileExists($destinationPath & $yyyymd & "\" & $file) Then
  Else
   ;MsgBox(4096,"Copying" , "Copying " & $file & " to \FolderA\" & $yyyymd)
   FileMove($file, $destinationPath& $yyyymd & "\" & $file)
  EndIf
WEnd
; Close the search handle
FileClose($search)
Edited by muncherw
Other People's Stuff:Andy Flesner's AutoIt v3: Your Quick Guide[topic="34302"]Locodarwin's ExcelCom_UDF[/topic][topic="61090"]MrCreatorR's Opera Library[/topic]
Link to comment
Share on other sites

  • Moderators

muncherw,

I always wonder why people insist on using FileFindFirst/NextFile when there is a perfectly good function which makes everything so simple - _FileListToArray. :)

This works for me when I use your paths on my machine - see if it does for you: ;)

#include <File.au3>

$sSourcePath = "DictationsBackupsBackup"
$sDestinationPath = "DictationsBackupsFolderA"

; Get a list of .dss files
$aList = _FileListToArray($sSourcePath, "*.dss", 1)

; Are there any files?
If @error then 
    MsgBox(0, "Error", "No files matched the search pattern")
Else
    ; Loop through the found files
    For $i = 1 To $aList[0]
        ; Get the date
        $aDTG = FileGetTime($aList[$i])
        If @error Then
            MsgBox(0, "Error", "Cannot read the date for " & $aList[$i])
        Else
            ; ; Set date string
            $sDate = $aDTG[0] & $aDTG[1] & $aDTG[2]
            ; Does folder exist?
            If Not FileExists($sDestinationPath & $sDate) Then ; Note use of "Not"
                ; Create folder if not
                DirCreate($sDestinationPath & $sDate)
            EndIf
            ; Check if file exists in folder
            If Not FileExists($sDestinationPath & $sDate & "" & $aList[$i]) Then
                ; Move file if it does not
                FileMove($sSourcePath & $aList[$i], $sDestinationPath & $sDate & "" & $aList[$i])
            EndIf
        EndIf
    Next
EndIf

Please ask if anything is unclear - or does not work. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

muncherw,

Try changing the FileGetTime line to read:

FileGetTime($sSourcePath & $aList[$i])

I was running the script from the source folder so I did not need a path - you probably do. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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