Computer Hope

Microsoft => Microsoft DOS => Topic started by: spiderboy on July 25, 2008, 12:55:15 PM

Title: converting hex filenames to decimal in batch file
Post by: spiderboy 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
Title: Re: converting hex filenames to decimal in batch file
Post by: Sidewinder 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)
Title: Re: converting hex filenames to decimal in batch file
Post by: spiderboy on July 25, 2008, 02:29:03 PM
Thank you Sidewinder.
Title: Re: converting hex filenames to decimal in batch file
Post by: Dias de verano 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