Jump to content

Sort by date and type


Recommended Posts

Hi All,

Last time I got some great help here so I'm banking on your helpfulness once again :unsure:

This is a bit complicated, so I'll start with the bottom line and go back.

I need to:

1. Go to a folder that has subfolders and different type of files

2. Sort all items by date modified (latest on top)

3. Choose only certain text files: file names don't change, date modified does (newly-modified files overwrite old ones)

4. Copy those files (33) to another folder (easy)

I don't want a displayed list of the files. I simply want them to be copied to a specified folder.

I could create an array with all 33 files, but then how do I make sure they have the latest date?

After going through the help forum, I've come across several scripts, but I don't always understand them in order to change them according to my needs.

Here's what seems relevant:

$aArray = _RecFileListToArray($sProgFiles & "\AutoIt3\", "*.txt", 0, 1, 1) Do not scan subfolders

$asItem = _GetList("A:\File_To_Catalog\*.txt")

for $nX = 1 to $asItem[0]

MsgBox(0,$nx, $asItem[$nX])

next

Func _GetList($psFolder)

;Sort a list of filenames or directories

$sCmd = "dir " & $psFolder & " /b /on /ad";filenames, not folders

$sFileList = "C:\~listfile.tmp"

RunWait(@Comspec & " /c " & $sCmd & ">" & $sFileList,"",@SW_HIDE)

$sList = FileRead($sFileList,FileGetSize($sFileList))

$sList = StringTrimRight(StringReplace($sList,@CRLF, @LF),1)

$asList = StringSplit($sList,@LF)

;cleanup

FileDelete($sFileList)

Return $asList

EndFunc

#include <Array.au3>

#include <Constants.au3>

Global $iLine = 0

Global $sLine = ""

Global $aLine = ""

Dim $aFiles[30000]

$aRaw = Run(@ComSpec & " /c dir n:\music\*.mp3 /b /o /s", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While 1

$SLine = StdoutRead($aRaw)

If @error Then ExitLoop

$aLine = StringSplit($SLine, @CRLF)

If $aLine[0] > 1 Then

For $i = 1 To $aLine[0]

If $aLine[$i] = "" Then Continueloop

$iLine = $iLine + 1

$aFiles[$iLine] = $aLine[$i]

Next

Else

$iLine = $iLine + 1

$aFiles[$iLine] = $sLine

EndIf

Wend

By the way - how do I know which files I need to include in the script?

E.g.: #include <Array.au3>

There doesn't seem to be a list of existing .au3 files with explanations of what they're used for.

I hope this is not too much to ask for.

Thanks!

Miriam

Link to comment
Share on other sites

Hi Miriam,

you're right, with Melba's _RecFileListToArray, you can "scan" the directory for the filenames. After you have all Filenames (incl. path) in a array, you can loop through the array and check the modified time with FileGetTime().

If you take a look at the helpfile, you can see for every function, what includes are needed. :unsure:

Maybe this can help you for a start.

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Another point MiriamSapir when you post code (which is helpful) use the [autoit][/autoit] tags (blue A) as this saves a lot of space in the thread, plus its easier to read.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Hi Miriam,

you're right, with Melba's _RecFileListToArray, you can "scan" the directory for the filenames. After you have all Filenames (incl. path) in a array, you can loop through the array and check the modified time with FileGetTime().

If you take a look at the helpfile, you can see for every function, what includes are needed. :unsure:

Maybe this can help you for a start.

Thanks for the quick answer.

I'm a realy newbie, I don't understand Melba's script completely. So I'm going to have to go step by step.

I checked the help forum and files.

For starters, If I understand correctly, I need to create an array with the file names and paths.

Tried this:

#Include <File.au3>
#include <Array.au3>

_FileListToArray ($_"A:\File_To_Catalog","*_Added_Journals.txt" [, $iFlag = 1 )

Kept getting syntax error.

The help file and examples in the forum show the following and I don't know how to translate it to the path I need: _FileListToArray(@DesktopDir)

Let's say I get the script right, I still receive only the files but don't know which one is that latest.

I would need to use a loop, but then how do I compare dates modified?

Can you just give me please a clue, not the whole script obviously, but just something I can work with (search forum, try to write and modify it myself).

If I wanted the most updated files copied to another directory, I would have to put the file copy command inside the loop, or alternatively just copy the results in the end?

Thanks for your help,

Miriam

Link to comment
Share on other sites

Thanks for the quick answer.

I'm a realy newbie, I don't understand Melba's script completely. So I'm going to have to go step by step.

I checked the help forum and files.

For starters, If I understand correctly, I need to create an array with the file names and paths.

Tried this:

#Include <File.au3>
#include <Array.au3>

_FileListToArray ($_"A:\File_To_Catalog","*_Added_Journals.txt" [, $iFlag = 1 )

Kept getting syntax error.

The help file and examples in the forum show the following and I don't know how to translate it to the path I need: _FileListToArray(@DesktopDir)

Let's say I get the script right, I still receive only the files but don't know which one is that latest.

I would need to use a loop, but then how do I compare dates modified?

Can you just give me please a clue, not the whole script obviously, but just something I can work with (search forum, try to write and modify it myself).

If I wanted the most updated files copied to another directory, I would have to put the file copy command inside the loop, or alternatively just copy the results in the end?

Thanks for your help,

Miriam

Anyone?

Link to comment
Share on other sites

Anyone?

Just an start, this script will create a 2D array with filenames and their modified dates, then it is sorted so latest files are the first:

#include <file.au3>
#include <array.au3>

Global $spath = @WindowsDir ;Change it to whatever you need
$checkpath = StringRegExp($spath,"^.+\\$",0)
If $checkpath = 0 Then $spath = $spath & "\"

$aArray = _FileListToArray($spath,"*",1)
If Not IsArray($aArray) Then
    Msgbox(0,"","Sorry but " & $spath & " has no files")
    Exit
EndIf

Global $aArray2D[10][2]
ReDim $aArray2D[$aArray[0]+1][2]
$aArray2D[0][0] = $aArray[0]

For $i=1 to $aArray[0]
    $aArray2D[$i][0] = $aArray[$i]
    $aArray2D[$i][1] = FileGetTime($spath & $aArray[$i],0,1)
Next

_ArraySort($aArray2D,1,1,"",1)
_ArrayDisplay($aArray2D)

Edit: I forgot to add full path to FileGetTime function. Now it is fixed.

Edited by sahsanu
Link to comment
Share on other sites

Just an start, this script will create a 2D array with filenames and their modified dates, then it is sorted so latest files are the first:

#include <file.au3>
#include <array.au3>

Global $spath = @WindowsDir ;Change it to whatever you need
$checkpath = StringRegExp($spath,"^.+\\$",0)
If $checkpath = 0 Then $spath = $spath & "\"

$aArray = _FileListToArray($spath,"*",1)
If Not IsArray($aArray) Then
    Msgbox(0,"","Sorry but " & $spath & " has no files")
    Exit
EndIf

Global $aArray2D[10][2]
ReDim $aArray2D[$aArray[0]+1][2]
$aArray2D[0][0] = $aArray[0]

For $i=1 to $aArray[0]
    $aArray2D[$i][0] = $aArray[$i]
    $aArray2D[$i][1] = FileGetTime($spath & $aArray[$i],0,1)
Next

_ArraySort($aArray2D,1,1,"",1)
_ArrayDisplay($aArray2D)

Edit: I forgot to add full path to FileGetTime function. Now it is fixed.

Apologize I didn't answer earlier. I was not at my computer.

Thanks a lot for your help.

Just a tiny questions: I keep getting syntax error, though I tried different ways:

Global $spath = @A:\File_To_Catalog\*.txt

Global $spath = $"_A:\File_To_Catalog\*.txt"

What am I missing?

Thanks

Miriam

Link to comment
Share on other sites

Apologize I didn't answer earlier. I was not at my computer.

Thanks a lot for your help.

Just a tiny questions: I keep getting syntax error, though I tried different ways:

Global $spath = @A:\File_To_Catalog\*.txt

Global $spath = $"_A:\File_To_Catalog\*.txt"

What am I missing?

Thanks

Miriam

Are you trying to access drive A:?

Then try:

#include <file.au3>
#include <array.au3>

Global $spath = "A:\File_To_Catalog\";<<<  ======================
$checkpath = StringRegExp($spath,"^.+\\$",0)
If $checkpath = 0 Then $spath = $spath & "\"

$aArray = _FileListToArray($spath,"*.txt",1);<<<   ======================
If Not IsArray($aArray) Then
    Msgbox(0,"","Sorry but " & $spath & " has no files")
    Exit
EndIf

Global $aArray2D[10][2]
ReDim $aArray2D[$aArray[0]+1][2]
$aArray2D[0][0] = $aArray[0]

For $i=1 to $aArray[0]
    $aArray2D[$i][0] = $aArray[$i]
    $aArray2D[$i][1] = FileGetTime($spath & $aArray[$i],0,1)
Next

_ArraySort($aArray2D,1,1,"",1)
_ArrayDisplay($aArray2D)

ReArranging

Edited by JoHanatCent
Link to comment
Share on other sites

Are you trying to access drive A:?

Then try:

#include <file.au3>
#include <array.au3>

Global $spath = "A:\File_To_Catalog\";<<<  ======================
$checkpath = StringRegExp($spath,"^.+\\$",0)
If $checkpath = 0 Then $spath = $spath & "\"

$aArray = _FileListToArray($spath,"*.txt",1);<<<   ======================
If Not IsArray($aArray) Then
    Msgbox(0,"","Sorry but " & $spath & " has no files")
    Exit
EndIf

Global $aArray2D[10][2]
ReDim $aArray2D[$aArray[0]+1][2]
$aArray2D[0][0] = $aArray[0]

For $i=1 to $aArray[0]
    $aArray2D[$i][0] = $aArray[$i]
    $aArray2D[$i][1] = FileGetTime($spath & $aArray[$i],0,1)
Next

_ArraySort($aArray2D,1,1,"",1)
_ArrayDisplay($aArray2D)

ReArranging

This worked!

Thanks!

The only other question I have is this:

Instead of displaying the list, I want to copy/paste the selected files:

FileCopy("A:\File_To_Catalog\, A:\Templates (From Sharon)\BIBSYS\BIBSYS_Temp\)

The question is: where do I add this line?

Or can I write something like this?

FileCopy("A:\File_To_Catalog\_ArraySort($aArray2D,1,1,"",1), A:\Templates (From Sharon)\BIBSYS\BIBSYS_Temp\)

Thanks,

Miriam

Link to comment
Share on other sites

The only other question I have is this:

Instead of displaying the list, I want to copy/paste the selected files:

FileCopy("A:\File_To_Catalog\, A:\Templates (From Sharon)\BIBSYS\BIBSYS_Temp\)

The question is: where do I add this line?

Remove_ArrayDisplay($aArray2D) and put this code:

For $i=1 to $aArray2D[0][0]
    ;The number 1 at the end of Filecopy means that if file exists it will be overwritten. Take a look to FileCopy help
    FileCopy("A:\File_To_Catalog\" & $aArray2D[$i][0], "A:\Templates (From Sharon)\BIBSYS\BIBSYS_Temp\",1) 
Next

Anyway, I don't really know what you need to do with this script.... because you are doing nothing with the sorted file list. Once you have sorted all the files then... you copy all of them ???. If you just need to copy all txt files in a directory just put below line and you will save yourself to write more than one line of code:

FileCopy("A:\File_To_Catalog\*.txt", "A:\Templates (From Sharon)\BIBSYS\BIBSYS_Temp\",1)
Link to comment
Share on other sites

Remove_ArrayDisplay($aArray2D) and put this code:

For $i=1 to $aArray2D[0][0]
    ;The number 1 at the end of Filecopy means that if file exists it will be overwritten. Take a look to FileCopy help
    FileCopy("A:\File_To_Catalog\" & $aArray2D[$i][0], "A:\Templates (From Sharon)\BIBSYS\BIBSYS_Temp\",1) 
Next

Anyway, I don't really know what you need to do with this script.... because you are doing nothing with the sorted file list. Once you have sorted all the files then... you copy all of them ???. If you just need to copy all txt files in a directory just put below line and you will save yourself to write more than one line of code:

FileCopy("A:\File_To_Catalog\*.txt", "A:\Templates (From Sharon)\BIBSYS\BIBSYS_Temp\",1)

Hi sahsanu,

After doing the reports for 2 months, I realized now that I don't have to sort the files at all, just copy them to my local directory! :unsure:

For some reason, I thought the process creates same file name but with different dates.

Don't kill me!

Sorry for all the hassel.

Would it be too much to ask for one last advice?

If you don't respond, I'll totally understand.

- Every week, a new folder is created in the following path: A:\Marc_Files\Marc_May_15_2011

- Every week, I create a folder in my local directory (with AutoIt, successfully) that has this path : C:\Sunday Reports\May-19-2011\MARC

I need to copy files from that folder to my local directory

I tried this but keep getting syntax errors:

FileCopy("A:\Marc_Files\" & "Marc" & __DateToMonth ( @MON ) & "_" & @MDAY & "_" & @YEAR\*_Added.txt", "C:\Sunday Reports\" & _DateToMonth ( @MON ) & "-" & @MDAY & "-" & @YEAR & '\MARC\Unicode')

Thanks a million!

Miriam

Edited by MiriamSapir
Link to comment
Share on other sites

  • 6 months later...

Just an start, this script will create a 2D array with filenames and their modified dates, then it is sorted so latest files are the first:

#include <file.au3>
#include <array.au3>

Global $spath = @WindowsDir ;Change it to whatever you need
$checkpath = StringRegExp($spath,"^.+$",0)
If $checkpath = 0 Then $spath = $spath & ""

$aArray = _FileListToArray($spath,"*",1)
If Not IsArray($aArray) Then
    Msgbox(0,"","Sorry but " & $spath & " has no files")
    Exit
EndIf

Global $aArray2D[10][2]
ReDim $aArray2D[$aArray[0]+1][2]
$aArray2D[0][0] = $aArray[0]

For $i=1 to $aArray[0]
    $aArray2D[$i][0] = $aArray[$i]
    $aArray2D[$i][1] = FileGetTime($spath & $aArray[$i],0,1)
Next

_ArraySort($aArray2D,1,1,"",1)
_ArrayDisplay($aArray2D)

Edit: I forgot to add full path to FileGetTime function. Now it is fixed.

How can get all files of a directory and the subdirectory
Link to comment
Share on other sites

  • Moderators

q113960096,

Look at the RecFileListToArray UDF in my sig. :)

M23

P.S. And next time start a new thread rather than hijacking an old one. ;)

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

q113960096,

Is that supposed to be a question? And am I to understand by the larger bolded font that you find my last answer unsatisfactory? ;)

Not the way to get help here, my friend. :)

The answer to your problem is to use the RecFilelistToArray UDF to get all the files you want to sort into an array. You then create a suitably sized 2D array and fill it in a loop by transferring the filename to one dimension and the result of FileGetTime on that filename into the other. Finally sort the array on the date-time dimension. And then you will have a date-time sorted list of the files. :D

I did something very similar to this with FileGetSize a short while ago here - look at it and have a go at coding something it yourself. Come back with some code if you want more help. :)

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

  • 5 years later...

i would like to get all the files with a name sorted rather than just in one folder.

I have a directory with multiple folders and each folder contains an exe, i want to list the build.exe in all folders(of the directory) in sorted order, but when i try the below code:

#include <file.au3>
#include <array.au3>

Global $spath = "Directory Path";<<<  ======================
$checkpath = StringRegExp($spath,"^.+\\$",0)
If $checkpath = 0 Then $spath = $spath & "\"

$aArray = _FileListToArray($spath,"*build.exe",1);<<<   ======================
If Not IsArray($aArray) Then
    Msgbox(0,"","Sorry but " & $spath & " has no files")
    Exit
EndIf

Global $aArray2D[10][2]
ReDim $aArray2D[$aArray[0]+1][2]
$aArray2D[0][0] = $aArray[0]

For $i=1 to $aArray[0]
    $aArray2D[$i][0] = $aArray[$i]
    $aArray2D[$i][1] = FileGetTime($spath & $aArray[$i],0,1)
Next

_ArraySort($aArray2D,1,1,"",1)
_ArrayDisplay($aArray2D)

 

The sorry message appears even though the build.exe exists in the directory, however when i give a folder path rather than just the directory, it shows the sorted array with build.exe listed.Can anyone help me through

Link to comment
Share on other sites

  • Moderators

rahulsics,

Welcome to the AutoIt forums.

My answer is much as above - except that the functions required are now in the standard include set. Use _FileListToArrayRec to get an array of the required files - you can get them sorted automatically using the $FLTAR_SORT flag.

Have a go at coding something yourself and post again  if you have problems.

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

Melba23,

 Thanks for the quick response, the function _FileListToArrayRec works well but in my scenario since I am searching in the whole directory it takes infinitely long, instead can you suggest me any functions so that,

First sort the directory in date order and then search in the latest 2 or 3 folders, in which I will be having the latest build.

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