1.

Solve : How do I delete the extension file on batch program?

Answer»

Hi,

I have a for loop that listed some files on a directory. In the for loop, the files have been saved as variable %%f. How do I filter or remove the extention of the file. For example: %%f = abc.fm I want to remove the .fm. The result should be %%f= abc

I tried to use few methods below but not working. Please see my simple program below:

rem saveasfm2xml.bat
@echo off
:STARTLOOP

rem %1 is directory passing
SET _d="%1"
cd %_d%
if "%1" == "" goto ENDLOOP
for %%f in ("*.fm") do (
echo %%f

SET z=%%f:~0,-3%
ECHO %z%

SET z=%%f:.fm=%
ECHO %z%
)
shift
goto STARTLOOP
:ENDLOOP

Please help.

Thank you
Quote from: txnguy3 on MARCH 04, 2009, 02:53:29 PM

I have a for loop that listed some files on a directory. In the for loop, the files have been saved as variable %%f. How do I filter or remove the extention of the file. For example: %%f = abc.fm I want to remove the .fm. The result should be %%f= abc

You need to study the FOR variable modifiers.

Type FOR /? at the prompt.

The ~n modifier is the one you need. Others will get the drive, path, extension (including DOT), size, date, etc.

e.g. if the file was called abc.fm then Echo %%~nF would echo abc to the console.

Also, your %z% variable which you think is going to hold the last 3 chars of the FILENAME + extension string, won't work in a loop. (Study "delayed EXPANSION".)

Also that string substitution ( SET z=%%f:.fm=% ) would be a problem. For the same reason and others. Anyway you don't need these now.








Discussion

No Comment Found