Solved Assigning file extension to local variable

October 25, 2011 at 09:06:30
Specs: Windows 7
Hi there

I have only recently started playing with batch files (as you will find out from my messy code :)

I have a bunch of 2D CAD drawing that I have converted over from another program.

I need to rename these files to the company standard.

T12345x3.bog.dxf Should be:

Some of these file names are different in structure and most have bogus extensions prefixing the genuine extensions as seen above.

I have been using the for loop method to sucessfully extract different parts of the filename which I have then pieced together to make the correct filenames:

What I am looking for (and know exists as I have seen it in a long lost thread) is a modifier to put infront of my token number so that I can start counting tokens from the end of the filename instead of the beginning to allow me to sucessfully pick out the correct extension every time. I also want to know this as it would be very useful in my other codes. (I am aware there are better methods of modifying simple filenames such as these but I really want to know what this modifier is so as I can extract the extensions for file metadata later down the line.

for /F "tokens=1 delim................................. Counts from begginning
for /F "tokens=<HERE>1 delim....................Counts from end

Here is the shabby code in question:


setlocal enabledelayedexpansion

:: Set Filename
for /F "tokens=*" %%j in ('dir /b *x*.dxf') do (set Filename=%%j)&(set endy=1)

:: Isolates the Part Number & Revision & assigns them to var's %%N & %%R respectively

for /F "tokens=1 delims=x." %%N in ("!Filename!") DO (set Number=%%N)
for /F "tokens=2 delims=x." %%R in ("!Filename!") DO (set Revision=%%R)

ECHO:Filename: %Filename%
ECHO:Number: %Number%
ECHO:Revision: %Revision%
ECHO:Endy: %endy%

rename "!Filename!" "!Number!_!Revision!.dxf"

if "%endy%" EQU "0" (GOTO:eof)



I anyone can help I'd be increadibly gratefull as I've sifted through google for hours!

See More: Assigning file extension to local variable

Report •

October 25, 2011 at 09:28:21
✔ Best Answer
There is no such modifier allowing to count tokens from end; that may be done by a recursive coding not too hard but taylored to the specific case. Anyway to catch the file extension just use the ~x modifier that recognizes as extension the last string of the name prefixed by a dot, i.e. in a for loop as

for %%j in (*X*) do (

%%~xj extracts .dxf from T12345x3.bog.dxf .

Report •

October 26, 2011 at 10:14:43
Good work!

Thanks IVO, that was exactly what I was after. I was going round in circles looking for this token modifier when I'd obviously seen this when looking into parameters.

Cheers :)

Report •

Related Solutions

Ask Question