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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
raghuram.star
Posts: 3
Joined: 28 Sep 2012 01:08

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

#1 Post by raghuram.star » 22 Oct 2012 02:59

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

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

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

#2 Post by abc0502 » 22 Oct 2012 03:29

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#3 Post by foxidrive » 22 Oct 2012 06:26

abc0502 meant to use delayed expansion and which requires the use of !var! instead of %var% in a loop.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

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

#4 Post by abc0502 » 22 Oct 2012 06:29

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#5 Post by foxidrive » 22 Oct 2012 06:30

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"

Post Reply