Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: converting hex filenames to decimal in batch file  (Read 11690 times)

0 Members and 1 Guest are viewing this topic.

spiderboy

    Topic Starter


    Newbie

    converting hex filenames to decimal in batch file
    « on: July 25, 2008, 12:55:15 PM »
    Hello All,

    I was wondering if there is a simple batch command that will go into a dir and convert all the filenames in hex to decimal.

    Some example filenames:
    0000->0000
    1000->4096
    AAAA->43690

    Thank you for your time,
    Peter

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: converting hex filenames to decimal in batch file
    « Reply #1 on: July 25, 2008, 01:41:17 PM »
    Hello All,

    I was wondering if there is a simple batch command that will go into a dir and convert all the filenames in hex to decimal.

    I'm not aware of any batch command that can do this. You can do this is VBScript however:

    Code: [Select]
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set f = fso.GetFolder("c:\temp")                                     'change directory here
    Set fc = f.Files

    For Each fs In fc
       fname = fso.GetBaseName(fs)
       fext = fso.GetExtensionName(fs)
       newName = CLng("&H" & fname) & "." & fext
       fs.Name = newName
    Next

    Save with a VBS extension and run from the command line as cscript scriptname.vbs

    You may have to change the directory name.

    Good luck. 8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    spiderboy

      Topic Starter


      Newbie

      Re: converting hex filenames to decimal in batch file
      « Reply #2 on: July 25, 2008, 02:29:03 PM »
      Thank you Sidewinder.

      Dias de verano

      • Guest
      Re: converting hex filenames to decimal in batch file
      « Reply #3 on: July 25, 2008, 02:40:39 PM »
      batch can do this directly.

      set /a treats numerical strings beginning 0 as octal (pitfall for the unwary!), and those beginning 0x as hexadecimal.

      Code: [Select]
      @echo off                                                                 
      setlocal enabledelayedexpansion                                           
                                                                                 
      echo abc > 0000.txt                                                       
      echo def > 1000.txt                                                       
      echo ghi > aaaa.txt                                                       
      echo jkl > 000f.txt                                                       
      echo mno > 00ff.txt                                                       
      echo pqr > 0fff.txt                                                       
      echo stu > ffff.txt                                                       
      echo vwx > aef2.txt                                                       
                                                                                 
      for /f %%H in ('dir /on /b *.txt') do (                                   
      set hexstring=%%~nH                                               
      set extension=%%~xH                                               
      set /a decstring=0x!hexstring!                                     
      set pad=                                                           
      if !decstring! leq 9999 set pad=0!pad!                             
      if !decstring! leq 999 set pad=0!pad!                             
      if !decstring! leq 99 set pad=0!pad!                               
      if !decstring! leq 9 set pad=0!pad!                               
      set decstring=!pad!!decstring!                                     
      echo !hexstring!!extension! =^> !decstring!!extension!             
      )                                                                 


      Code: [Select]
      0000.txt => 00000.txt
      000f.txt => 00015.txt
      00ff.txt => 00255.txt
      0fff.txt => 04095.txt
      1000.txt => 04096.txt
      aaaa.txt => 43690.txt
      aef2.txt => 44786.txt
      ffff.txt => 65535.txt
      « Last Edit: July 25, 2008, 05:45:45 PM by Dias de verano »