Page 1 of 1

How to get File Name and Extension without (.) dot

Posted: 22 Oct 2012 02:59
by raghuram.star
Hi

I want to get the File Name and Extension without (.) dot.

I have File Name
My_File_Name.txt
File Version : 10

I want to get it as
My_File_Name_txt_10.O

I was able to get the File Name & Version in required format using

Code: Select all

"%%~na_%%b.o"

&
I have tried to get extension through

Code: Select all

"%%~nx1_%%b.o"
"%%~nx2_%%b.o"
"%%~na_%%~xa_%%b.o"

But no luck.

I'm wondering is it possible to get the File Name & Extension without (.) dot?

Thanks in advance

Re: How to get File Name and Extension without (.) dot

Posted: 22 Oct 2012 03:29
by abc0502
set the extension "%%~xa" to a variable e.g "ext" then the ext without the dot "."
should be like this "%ext:~1%" by using string substitution here

Test:
This will get the name only "file_name"
For /F %%A in ("file_name.txt") Do echo %%~na


This will set the extension to variable then take all chars except the first which is the dot
For /F %%A in ("file_name.txt") Do (
set "ext=%%~xA"
echo %ext:~1%
)


Edit:
Or maybe this:
For /F %%A in ("file_name.txt") Do (
set "ext=%%~xA"
echo %ext:.=_%
)

see the red text ".=_" this will replace the dot to "_" when it find it in the text, you can see the example here

Re: How to get File Name and Extension without (.) dot

Posted: 22 Oct 2012 06:26
by foxidrive
abc0502 meant to use delayed expansion and which requires the use of !var! instead of %var% in a loop.

Re: How to get File Name and Extension without (.) dot

Posted: 22 Oct 2012 06:29
by abc0502
sorry about that i thought he wanted to run that on a one file,
BTW, is there a way to do that with the %%A directly?, i tried few ways but not working

Re: How to get File Name and Extension without (.) dot

Posted: 22 Oct 2012 06:30
by foxidrive
This is untested but should rename all .txt files with the extension and _10.o on the end.

Code: Select all

@echo off
for /f "delims=" %%a in ('dir *.txt /b /a-d') do call :next "%%a"
pause
goto :EOF
:next
set "ext=%~x1"
set "ext=%ext:~1%"
ren %1 "%~n1_%ext%_10.o"