Search code examples
powershellcmd

get decimal file name as hex powershell


i have a bunch of files that have filename as decimal, like 0 100, 200, 344, etc so i used this code powershell

"gci | sort @{Expression={[int][RegEx]::Match($_, '\d+').Value}} | %%{ $_.Name } | Select-Object -Skip 1" > Headerlist.txt

this take the filename as by expression order, and save into a text file, works perfectly, but how can i convert them to hex? Like. Name of files

0 108 314 450 .....

i want

0 6c 13A 1C2

.....

thanks!


Solution

  • Following one of the examples on how to convert decimal to hex from the linked answer in comments and assuming your filenames only have digits the code would be as follows, note that the use of [RegEx]::Match($_, '\d+').Value is unnecessary assuming the filenames only have numeric digits:

    Get-ChildItem | Sort-Object { [int]$_.BaseName } | ForEach-Object {
        '{0:X}{1}' -f [int]$_.BaseName, $_.Extension
    } | Out-File HeaderList.txt
    

    You can test the code with this example:

    ([System.IO.FileInfo[]]'0.ext,100.ext,200.ext,344.ext,108.ext,314.ext'.Split(',')) |
    Sort-Object { [int]$_.BaseName } | ForEach-Object {
        '{0:X}{1}' -f [int]$_.BaseName, $_.Extension
    }