From f8024654021d5e49f65d83453396e7e5d5f25d5a Mon Sep 17 00:00:00 2001 From: phil294 Date: Thu, 20 Jul 2023 11:02:02 +0200 Subject: [PATCH] Add FileGetSize --- README.md | 8 ++++---- docs/index.html | 8 ++++---- src/cmd/control-flow/loop.cr | 18 +++++++++++------- src/cmd/file/file-get-size.cr | 22 ++++++++++++++++++++++ 4 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 src/cmd/file/file-get-size.cr diff --git a/README.md b/README.md index 9f9acbc..0438584 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ AHK_X11 can be used completely without a terminal. You can however if you want u
CLICK TO SEE WHICH COMMANDS ARE IMPLEMENTED AND WHICH ARE MISSING. Note however that this is not very representative. For example, no `Gui` sub command is included in the listing. For a better overview on what is already done, skim through the FULL DOCUMENTATION HERE. ```diff -DONE ?% (120/220): +DONE ?% (121/220): + Else, { ... }, Break, Continue, Return, Exit, GoSub, GoTo, IfEqual, Loop, SetEnv, Sleep, FileCopy, + SetTimer, WinActivate, MsgBox, Gui, SendRaw, #Persistent, ExitApp, + EnvAdd, EnvSub, EnvMult, EnvDiv, ControlSendRaw, IfWinExist/IfWinNotExist, SetWorkingDir, @@ -72,7 +72,7 @@ DONE ?% (120/220): + PixelSearch, #Include, InputBox, ClipWait, EnvSet, SetKeyDelay, SetMouseDelay, MouseClickDrag, + #NoTrayIcon, TrayTip, Random, Shutdown, RunAs, SoundGet, SoundSet, SoundPlay, Sort, + StringTrimLeft, StringTrimRight, WinMinimizeAll, WinMinimizeAllUndo, WinSetTitle, WinWait, -+ WinWaitClose, WinWaitActive, WinWaitNotActive, DriveSpaceFree ++ WinWaitClose, WinWaitActive, WinWaitNotActive, DriveSpaceFree, FileGetSize NEW ?% (9/220): (not part of spec or from a more recent version) @@ Echo, ahk_x11_print_vars, FileRead, RegExGetPos, RegExReplace, EnvGet, Click @@ @@ -87,12 +87,12 @@ REMOVED ?% (11/220): # AutoTrim: It's always Off. It would not differentiate between %a_space% and %some_var%. # It's possible but needs significant work. -TO DO ?% (76/220): alphabetically +TO DO ?% (75/220): alphabetically - BlockInput, Control, ControlFocus, ControlGet, ControlGetFocus, - ControlMove, - DetectHiddenText, DetectHiddenWindows, Drive, DriveGet, - FileCopyDir, FileCreateShortcut, -- FileInstall, FileGetAttrib, FileGetShortcut, FileGetSize, FileGetTime, FileGetVersion, +- FileInstall, FileGetAttrib, FileGetShortcut, FileGetTime, FileGetVersion, - FileMove, FileMoveDir, FileRecycle, FileRecycleEmpty, FileRemoveDir, - FormatTime, GroupActivate, GroupAdd, - GroupClose, GroupDeactivate, GuiControlGet, diff --git a/docs/index.html b/docs/index.html index 59d7bcc..a6e19a9 100644 --- a/docs/index.html +++ b/docs/index.html @@ -117,7 +117,7 @@

Table of contents

FileGetShortcut
  • - FileGetSize + FileGetSize
  • FileGetTime @@ -2203,7 +2203,7 @@

    The "Last Found" Window

    Retrieves information about a shortcut (.lnk) file, such as its target file. - FileGetSize + FileGetSize Retrieves the size of a file in bytes, KB, or MB. @@ -3622,7 +3622,7 @@

    The "Last Found" Window

    -
    +
    #

    FileGetSize


    @@ -3666,7 +3666,7 @@

    The "Last Found" Window

    FileGetAttrib, FileSetAttrib, FileGetTime, FileSetTime, FileGetVersion, File-loop

     

    Example

    -

    FileGetSize, size, C:\My Documents\test.doc ; Retrieve the size in bytes.
    FileGetSize, size, C:\My Documents\test.doc, K ; Retrieve the size in Kbytes.
    +

    FileGetSize, size, %A_Home%/test.doc ; Retrieve the size in bytes.
    FileGetSize, size, /tmp/test.doc, K ; Retrieve the size in Kbytes.

    diff --git a/src/cmd/control-flow/loop.cr b/src/cmd/control-flow/loop.cr index 9b1155d..6e7f74e 100644 --- a/src/cmd/control-flow/loop.cr +++ b/src/cmd/control-flow/loop.cr @@ -68,19 +68,18 @@ class Cmd::ControlFlow::Loop < Cmd::Base when LoopType::Count fin = @index > @count.not_nil! when LoopType::Files - fin = @index > @files.size - if ! fin - match = @files[@index - 1] - file = ::File.new(match) - path = Path.new(match) + file = current_file + fin = ! current_file + if file + path = Path.new(file.path) stat = uninitialized LibC::Stat - Crystal::System::File.stat(match.check_no_null_byte, pointerof(stat)) + Crystal::System::File.stat(file.path.check_no_null_byte, pointerof(stat)) access_time = ::Time.new(stat.st_atim, ::Time::Location::UTC) # st_ctim is not creation time. Creation time is not available on Crystal, see # https://github.com/crystal-lang/crystal/issues/12416 # creation_time = ::Time.new(stat.st_ctim, ::Time::Location::UTC) thread.runner.set_global_built_in_static_var("A_LoopFileName", path.basename) - thread.runner.set_global_built_in_static_var("A_LoopFileFullPath", match) + thread.runner.set_global_built_in_static_var("A_LoopFileFullPath", file.path) thread.runner.set_global_built_in_static_var("A_LoopFileShortName", path.basename) thread.runner.set_global_built_in_static_var("A_LoopFileDir", path.dirname) thread.runner.set_global_built_in_static_var("A_LoopFileTimeModified", file.info.modification_time.to_YYYYMMDDHH24MISS) @@ -124,4 +123,9 @@ class Cmd::ControlFlow::Loop < Cmd::Base @parse_iter = nil thread.loop_stack.pop end + def current_file + match = @files[@index - 1]? + return nil if ! match + ::File.new(match) + end end \ No newline at end of file diff --git a/src/cmd/file/file-get-size.cr b/src/cmd/file/file-get-size.cr new file mode 100644 index 0000000..d4ac521 --- /dev/null +++ b/src/cmd/file/file-get-size.cr @@ -0,0 +1,22 @@ +# FileGetSize, OutputVar [, Filename, Units] +class Cmd::File::FileGetSize < Cmd::Base + def self.min_args; 1 end + def self.max_args; 3 end + def self.sets_error_level; true end + def run(thread, args) + out_var = args[0] + filename = args[1]? + if ! filename || filename.empty? + file = (thread.loop_stack.reverse.find &.current_file).try &.current_file + return "1" if ! file + else + return "1" if ! ::File.exists?(filename) + file = ::File.new(filename) + end + size = case args[2]?.try &.downcase + when "k" then file.info.size / 1024 + when "m" then file.info.size / 1024 / 1024 + else file.info.size end + thread.runner.set_user_var(out_var, size.to_i.to_s) + end +end \ No newline at end of file