Parsing a filename into seperate pieces

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dlrosencrans
Posts: 1
Joined: 18 Dec 2008 17:04

Parsing a filename into seperate pieces

#1 Post by dlrosencrans » 18 Dec 2008 17:22

Hello!

I have a need to parse a set of filenames into seperate pieces for additional processing. The files will be of varing length, but with the same prefix and file extension. I need to strip the prefix and file extension off and the do another task on the part of the filename that is remaining and of course loop through a list of files.

In this example there are 3 files:
PrefixTable1.dat
PrefixDatabase.dat
PrefixArchive.dat

What I need assitance with is to strip Prefix and .dat from the 3 files, so that what is stored in a variable or variables is Table1, Database, and Archive.

Any help?

Thanks,
Doug

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 18 Dec 2008 22:39

dlrosencrans,

Let's say the files are in c:\FullPath\:

Code: Select all

Setlocal Enabledelayedexpansion
for %%A in ("c:\FullPath\Prefix*.dat") do (
    set "FileNameWithoutExtension=%%~nA"
    set "SubString=!FileNameWithoutExtension:*Prefix=!"
    echo.Do somthing with !SubString!
)

DosItHelp? :wink:

Post Reply