7121 Posted January 21, 2010 Share Posted January 21, 2010 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? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 21, 2010 Moderators Share Posted January 21, 2010 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 itSo have a go at coding it yourself. If you have problems - you know where we are! M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
7121 Posted January 21, 2010 Author Share Posted January 21, 2010 okay, thanks Link to comment Share on other sites More sharing options...
EdDyreen Posted January 21, 2010 Share Posted January 21, 2010 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 Link to comment Share on other sites More sharing options...
Malkey Posted January 22, 2010 Share Posted January 22, 2010 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 Link to comment Share on other sites More sharing options...
7121 Posted July 31, 2010 Author Share Posted July 31, 2010 (edited) 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 July 31, 2010 by 7121 Link to comment Share on other sites More sharing options...
Malkey Posted July 31, 2010 Share Posted July 31, 2010 (edited) 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 July 31, 2010 by Malkey Link to comment Share on other sites More sharing options...
7121 Posted July 31, 2010 Author Share Posted July 31, 2010 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! Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted July 31, 2010 Moderators Share Posted July 31, 2010 (edited) 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]] EndFuncShould be just fine. Edited July 31, 2010 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. Link to comment Share on other sites More sharing options...
NicePerson Posted July 31, 2010 Share Posted July 31, 2010 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 Link to comment Share on other sites More sharing options...
Malkey Posted July 31, 2010 Share Posted July 31, 2010 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. expandcollapse popupC:\>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:\> Link to comment Share on other sites More sharing options...
NicePerson Posted July 31, 2010 Share Posted July 31, 2010 Thanks Malkey your reply is helpful for me... Link to comment Share on other sites More sharing options...
NicePerson Posted July 31, 2010 Share Posted July 31, 2010 Thanks Malkey your reply is helpful for me... Link to comment Share on other sites More sharing options...
Ascend4nt Posted July 31, 2010 Share Posted July 31, 2010 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 My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
NicePerson Posted July 31, 2010 Share Posted July 31, 2010 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&'"') Link to comment Share on other sites More sharing options...
NicePerson Posted July 31, 2010 Share Posted July 31, 2010 Someone can understand my Question or no? Link to comment Share on other sites More sharing options...
seandisanti Posted July 31, 2010 Share Posted July 31, 2010 Someone can understand my Question or no?FileCopy()? why would you want to create a bunch of redundant files though? Link to comment Share on other sites More sharing options...
NicePerson Posted August 1, 2010 Share Posted August 1, 2010 I am not that noob I know about FileCopy() but how to copy a file to 5000 folders which have different names..? Link to comment Share on other sites More sharing options...
NicePerson Posted August 1, 2010 Share Posted August 1, 2010 No idea? Link to comment Share on other sites More sharing options...
NicePerson Posted August 3, 2010 Share Posted August 3, 2010 I am still waiting........ Link to comment Share on other sites More sharing options...
Recommended Posts