filelist.txt There has someway to take" /> filelist.txt There has someway to take" /> filelist.txt There has someway to take"/>

Get File size in hex using forfiles

579 Views Asked by At

I am using this command in CMD to take all file size in a directory.

forfiles /s /c "cmd /c echo @file @fsize" >filelist.txt

There has someway to take this size but in hex format?

Example:

"00000000.png" 219457

to

"00000000.png" 50A6E
2

There are 2 best solutions below

6
lit On BEST ANSWER

While this does not use FORFILES, it can be used in a windows cmd batch-file. Since this is a recursive search, I assume you would want the fully qualified path included to avoid problems when the same filename is used in multiple directories.

powershell -NoLogo -NoProfile -Command ^
    "$q='\"';Get-ChildItem -File -Recurse ^| ForEach-Object {$($q+$_.FullName+$q) + ' ' + $($_.Length.ToString('X'))}"
2
aschipfl On

Well, forfiles does certainly not support to convert decimal to hexadecimal numbers.

Converting numbers in pure conventional batch scripting is not natively supported, so you will have to either borrow from another language (like PowerShell, VBScript, JavaScript, all of which are supplied with a modern Windows system), or to code it yourself the hard way (step by step as if you would do it on paper).

Anyway, luckily there is a hidden and undocumented dynamic pseudo-variable called =ExitCode, which holds the hexadecimal value of the most recent exit code, which we can make use of:

rem // Iterate through all files in the current working directory:
for /R %%I in (*.*) do (
    rem // Store name of currently iterated item:
    set "ITEM=%%~I"
    rem // Toggle delayed expansion to avoid issues with `!` and `^`:
    setlocal EnableDelayedExpansion
    rem // Explicitly set exit code to the size of the current file:
    cmd /C exit 0%%~zI
    rem // Return the exit code, hence the file size, as hex number:
    echo(!ITEM!  !=ExitCode!
    endlocal
)

Note, that files must be less than 2 GiB in size, because exit codes are given as signed 32-bit integers.