Jump to content
Sign in to follow this  
7121

Sort and Find Last

Recommended Posts

7121

I was wondering, can anyone help me write a program where it'll sort all the files in a folder from A-Z and then look for the last file in the list and get the name of that file into a string?

I can do it but i'm a noob so i would only end up using mouse clicks, i was hoping maybe someone can use command lines instead so any ideas?

Share this post


Link to post
Share on other sites
Melba23

7121,

Go and look at the following commands in the Help file:

_FileListToArray - this will get all the files from a folder into an array.

_ArraySort - This will sort the array alphabetically.

I can do it

So have a go at coding it yourself. If you have problems - you know where we are! :D

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
7121

okay, thanks :D

Share this post


Link to post
Share on other sites
EdDyreen

Here's a console APP

save it as Get.Last.CMD call it like "Get.Last.CMD" "FullPath"

@echo off &SetLocal EnableExtensions &EnableDelayedExpansion

cd /d %1

for /f "tokens=*" %%! in ( 'dir /a-d /b' ) do set "Last=%%!"

echo.!Last!


• Any number images • Images of any size • Any number of URLs • Any number of lines

Share this post


Link to post
Share on other sites
Malkey

Here is another way to get a list of sorted files of a directory by using the "Dir" DOS command.

Local $sSortedDir = _GetDOSOutput('dir "C:\Program Files\AutoIt3\" /A:A /B /O:N')
;ConsoleWrite(_GetDOSOutput('dir /?') & @CRLF) ; Dir help

Local $sLastLine = StringRegExpReplace($sSortedDir, "(?m)(?s)(?:.*)^(.*)$", "\1")

MsgBox(0, "Results", $sSortedDir & @CRLF & "Last Line is " & $sLastLine)

; http://www.autoitscript.com/forum/index.php?showtopic=106254&view=findpost&p=750640
Func _GetDOSOutput($command)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c ' & $command, '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return $text
EndFunc ;==>_GetDOSOutput

Share this post


Link to post
Share on other sites
7121

Here is another way to get a list of sorted files of a directory by using the "Dir" DOS command.

Local $sSortedDir = _GetDOSOutput('dir "C:\Program Files\AutoIt3\" /A:A /B /O:N')
;ConsoleWrite(_GetDOSOutput('dir /?') & @CRLF) ; Dir help

Local $sLastLine = StringRegExpReplace($sSortedDir, "(?m)(?s)(?:.*)^(.*)$", "\1")

MsgBox(0, "Results", $sSortedDir & @CRLF & "Last Line is " & $sLastLine)

; http://www.autoitscript.com/forum/index.php?showtopic=106254&view=findpost&p=750640
Func _GetDOSOutput($command)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c ' & $command, '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return $text
EndFunc ;==>_GetDOSOutput

umm, how do i get _GetDOSOutput to work? it's not part of the command Edited by 7121

Share this post


Link to post
Share on other sites
Malkey

umm, how do i get _GetDOSOutput to work? it's not part of the command

That post #5 is actually an example of how the function _GetDOSOutput works. Calling _GetDOSOutput is one of two commands that returns the last file of a sorted directory.

By rearranging that _GetDOSOutput script, this attached script when run returns the last file of a sorted directory using one command, or one function, or one UDF (User Defined Function), namely, _GetLastSortedFile()

MsgBox(0, "Results", 'Last sorted file is :- ' & _GetLastSortedFile("C:\Program Files\AutoIt3"))

; http://www.autoitscript.com/forum/index.php?showtopic=106254&view=findpost&p=750640
Func _GetLastSortedFile($sDir)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c dir "' & $sDir & '\" /A:A /B /O:N', '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return StringRegExpReplace($text, "(?m)(?s)(?:.*)^(.*){:content:}quot;, "\1")
EndFunc ;==>_GetLastSortedFile

Edit / PS : If EdDryeen had not posted his method, I would have used Melba23's method and would not have posted my modified EdDryeen method.

Edit 2: the "{:content:}quot;" rubbish must have occurred because of the first edit. I wll try again.

MsgBox(0, "Results", 'Last sorted file is :- ' & _GetLastSortedFile("C:\Program Files\AutoIt3"))
; http://www.autoitscript.com/forum/index.php?showtopic=108678&view=findpost&p=820772
; http://www.autoitscript.com/forum/index.php?showtopic=106254&view=findpost&p=750640
Func _GetLastSortedFile($sDir)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c dir "' & $sDir & '\" /A:A /B /O:N', '', @SW_HIDE, 2 + 4)
    While 1
    $text &= StdoutRead($Pid, False, False)
    If @error Then ExitLoop
    Sleep(10)
    WEnd
    Return StringRegExpReplace($text, "(?m)(?s)(?:.*)^(.*)$", "\1")
EndFunc ;==>_GetLastSortedFile
Edited by Malkey

Share this post


Link to post
Share on other sites
7121

That post #5 is actually an example of how the function _GetDOSOutput works. Calling _GetDOSOutput is one of two commands that returns the last file of a sorted directory.

By rearranging that _GetDOSOutput script, this attached script when run returns the last file of a sorted directory using one command, or one function, or one UDF (User Defined Function), namely, _GetLastSortedFile()

MsgBox(0, "Results", 'Last sorted file is :- ' & _GetLastSortedFile("C:\Program Files\AutoIt3"))

; http://www.autoitscript.com/forum/index.php?showtopic=106254&view=findpost&p=750640
Func _GetLastSortedFile($sDir)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c dir "' & $sDir & '\" /A:A /B /O:N', '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return StringRegExpReplace($text, "(?m)(?s)(?:.*)^(.*){:content:}quot;, "\1")
EndFunc ;==>_GetLastSortedFile

Edit / PS : If EdDryeen had not posted his method, I would have used Melba23's method and would not have posted my modified EdDryeen method.

THANK YOU, I actually get more of it now reading this. I went with the @comspec and figured it out, not before reading like about 5 different threads on DOS output. but yes, THANKS!

Share this post


Link to post
Share on other sites
SmOke_N

That post #5 is actually an example of how the function _GetDOSOutput works. Calling _GetDOSOutput is one of two commands that returns the last file of a sorted directory.

By rearranging that _GetDOSOutput script, this attached script when run returns the last file of a sorted directory using one command, or one function, or one UDF (User Defined Function), namely, _GetLastSortedFile()

MsgBox(0, "Results", 'Last sorted file is :- ' & _GetLastSortedFile("C:\Program Files\AutoIt3"))

; http://www.autoitscript.com/forum/index.php?showtopic=106254&view=findpost&p=750640
Func _GetLastSortedFile($sDir)
    Local $text = '', $Pid = Run('"' & @ComSpec & '" /c dir "' & $sDir & '\" /A:A /B /O:N', '', @SW_HIDE, 2 + 4)
    While 1
        $text &= StdoutRead($Pid, False, False)
        If @error Then ExitLoop
        Sleep(10)
    WEnd
    Return StringRegExpReplace($text, "(?m)(?s)(?:.*)^(.*){:content:}quot;, "\1")
EndFunc ;==>_GetLastSortedFile

Edit / PS : If EdDryeen had not posted his method, I would have used Melba23's method and would not have posted my modified EdDryeen method.

Despite the obvious quote error from the autoit tags there, I don't see this working on all operating systems. First off, my return string from stdoutread has no :content: flag.

As you stated, if this is non-recursive then:

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

Func _FileGetLastSorted($s_dir, $s_filter = "*", $s_file_type = 0)
    ; Get a list of all the files for the $s_file_type, based on the $s_filter/mask
    Local $a_arg = _FileListToArray($s_dir, $s_filter, $s_file_type)
    If @error Then Return SetError(@error, 0, 0)
    ; There was no error, so sort ascending
    ; Start at index 1 of array because index 0 only holds the number of files
    _ArraySort($a_arg, 0, 1)
    ; Return last name in list
    Return $a_arg[$a_arg[0]]
EndFunc
Should be just fine. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
NicePerson

What is mean by " /A:A /B /O:N" in this command?

"C:\WINDOWS\system32\cmd.exe" /c dir "C:\Program Files\AutoIt3\" /A:A /B /O:N

Share this post


Link to post
Share on other sites
Malkey

What is mean by " /A:A /B /O:N" in this command?

"C:\WINDOWS\system32\cmd.exe" /c dir "C:\Program Files\AutoIt3\" /A:A /B /O:N

If you open up a "Command Prompt" window and type in dir /?

You will get the following help explaining all the switches for the dos Dir command.

C:\>dir /?
Displays a list of files and subdirectories in a directory.

DIR [drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
 [/O[[:]sortorder]] [/P] [/Q] [/S] [/T[[:]timefield]] [/W] [/X] [/4]

 [drive:][path][filename]
    Specifies drive, directory, and/or files to list.

 /A     Displays files with specified attributes.
 attributes D Directories   R Read-only files
    H Hidden files  A Files ready for archiving
    S System files  - Prefix meaning not
 /B     Uses bare format (no heading information or summary).
 /C     Display the thousand separator in file sizes. This is the
    default. Use /-C to disable display of separator.
 /D     Same as wide but files are list sorted by column.
 /L     Uses lowercase.
 /N     New long list format where filenames are on the far right.
 /O     List by files in sorted order.
 sortorder  N By name (alphabetic)  S By size (smallest first)
    E By extension (alphabetic) D By date/time (oldest first)
    G Group directories first   - Prefix to reverse order
 /P     Pauses after each screenful of information.
 /Q     Display the owner of the file.
 /S     Displays files in specified directory and all subdirectories.
 /T     Controls which time field displayed or used for sorting
 timefield C Creation
    A Last Access
    W Last Written
 /W     Uses wide list format.
 /X     This displays the short names generated for non-8dot3 file
    names. The format is that of /N with the short name inserted
    before the long name. If no short name is present, blanks are
    displayed in its place.
 /4     Displays four-digit years

Switches may be preset in the DIRCMD environment variable. Override
preset switches by prefixing any switch with - (hyphen)--for example, /-W.

C:\>

Share this post


Link to post
Share on other sites
NicePerson

Thanks Malkey your reply is helpful for me...

Share this post


Link to post
Share on other sites
NicePerson

Thanks Malkey your reply is helpful for me...

Share this post


Link to post
Share on other sites
Ascend4nt

DOS Dir is useless with Unicode directory names unless you use a pipe and the '/u' switch.

Example:

$sFolder="C:"
$sOutFile=@DesktopDir&"\test.txt"
Run(@ComSpec&' /u /c dir "'&$sFolder&'\*.*" /B /A:- /ON > "'&$sOutFile&'"')

You'll have to remember though to open and read that file as a unicode file since there isnt a 'BOM' created for the file.

From cmd /? :

/U  Causes the output of internal commands to a pipe or file to be Unicode

Share this post


Link to post
Share on other sites
NicePerson

I want to get folder tree and copy a file to all folders in that tree i got the tree but how can i copy a file to that tree?

$sFolder="D:\5000 Folders With Defferent Names"
$sOutFile=@ScriptDir&"\Folders tree.txt"
Run(@ComSpec&' /u /c tree "'&$sFolder&'\" /A > "'&$sOutFile&'"')

Share this post


Link to post
Share on other sites
NicePerson

Someone can understand my Question or no?

Share this post


Link to post
Share on other sites
seandisanti

Someone can understand my Question or no?

FileCopy()? why would you want to create a bunch of redundant files though?

Share this post


Link to post
Share on other sites
NicePerson

I am not that noob :blink: I know about FileCopy() but how to copy a file to 5000 folders which have different names..?

Share this post


Link to post
Share on other sites
NicePerson

No idea? :blink:

Share this post


Link to post
Share on other sites
NicePerson

I am still waiting........

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×