Grabbing part of a filename from DIR

K

Kok Yong Lee

Hi there,

Let sya in my Y: drive I have a bunch of files that always have teh same
postfix (Src.zip) file name. How can I grab the part of the filename just
before Src.zip??

I tried
for /f "tokens=*" %%i in ('dir *src.zip /b') do @echo %%i;

but not sure how best to use the tokens, delims to my aid. the help menu
isnt really helpful. Can anyone shed some light on this?

thanks.

Y:\>dir *src.zip /b
M10_0_220R10QASrc.zip
M10_0_229R10QASrc.zip
M10_0_232R10QASrc.zip
M11_0_203Src.zip
M11_0_211Src.zip
M11_0_220Src.zip
M11_0_228Src.zip
 
K

Kok Yong Lee

Ha.

found an answer to my question.
for /f "tokens=1* delims=Src" %%i in ('dir *src.zip /b') do @echo %%i

but still like to know if there is any other more elegant solutions

thanks.
 
F

foxidrive

Hi there,

Let sya in my Y: drive I have a bunch of files that always have teh same
postfix (Src.zip) file name. How can I grab the part of the filename just
before Src.zip??

I tried
for /f "tokens=*" %%i in ('dir *src.zip /b') do @echo %%i;

but not sure how best to use the tokens, delims to my aid. the help menu
isnt really helpful. Can anyone shed some light on this?

thanks.

Y:\>dir *src.zip /b
M10_0_220R10QASrc.zip
M10_0_229R10QASrc.zip
M10_0_232R10QASrc.zip
M11_0_203Src.zip
M11_0_211Src.zip
M11_0_220Src.zip
M11_0_228Src.zip

@echo off
for /f "delims=" %%a in ('dir *src.zip /b /a:-d') do call :next "%%a"
goto :EOF
:next
set var=%~1
set var=%var:~0,-7%
echo %var%
 
B

billious

Kok Yong Lee said:
Ha.

found an answer to my question.
for /f "tokens=1* delims=Src" %%i in ('dir *src.zip /b') do @echo %%i

but still like to know if there is any other more elegant solutions

thanks.

Ha.

Think again.

Your "solution" actually locates that part of the filename up to the first
"S", "r" or "c" having skipped any leading "S", "r" or "c" - not up to the
first occurrence of "Src" as you appear to expect.

try

@echo off&setlocal enabledelyedexpansion
for /f %%i in ('dir /b *src.zip') do set yFN=%%~ni&echo !yFN:~0,-3!

if a filename is likely to contain spaces, etc. add "delims=" (with the
quotes) between /f and %%i

You don't need the "tokens=" since you are only using the first token. The
"@" is probably redundant, unless you are doing something very odd...

....or you could omit the ~n and change the -3 to -7

HTH

....Bill
 
P

Phil Robyn

Kok said:
Hi there,

Let sya in my Y: drive I have a bunch of files that always have teh same
postfix (Src.zip) file name. How can I grab the part of the filename just
before Src.zip??

I tried
for /f "tokens=*" %%i in ('dir *src.zip /b') do @echo %%i;

but not sure how best to use the tokens, delims to my aid. the help menu
isnt really helpful. Can anyone shed some light on this?

thanks.

Y:\>dir *src.zip /b
M10_0_220R10QASrc.zip
M10_0_229R10QASrc.zip
M10_0_232R10QASrc.zip
M11_0_203Src.zip
M11_0_211Src.zip
M11_0_220Src.zip
M11_0_228Src.zip

- - - - - - - - - - begin screen capture WinXP - - - - - - - - - -
C:\cmd>dir \junkdir
Volume in drive C has no label.
Volume Serial Number is D441-3BDF

Directory of C:\junkdir

11/30/2005 11:17 AM <DIR> .
11/30/2005 11:17 AM <DIR> ..
11/30/2005 11:17 AM 0 M10_0_220R10QASrc.zip
11/30/2005 11:17 AM 0 M10_0_229R10QASrc.zip
11/30/2005 11:17 AM 0 M10_0_232R10QASrc.zip
11/30/2005 11:17 AM 0 M11_0_203Src.zip
11/30/2005 11:17 AM 0 M11_0_211Src.zip
11/30/2005 11:17 AM 0 M11_0_220Src.zip
11/30/2005 11:17 AM 0 M11_0_228Src.zip
7 File(s) 0 bytes
2 Dir(s) 14,451,576,832 bytes free

C:\cmd>demo\FileNamePrefix
M10_0_220R10QA
M10_0_229R10QA
M10_0_232R10QA
M11_0_203
M11_0_211
M11_0_220
M11_0_228

C:\cmd>wyllist demo\FileNamePrefix.cmd
==========begin file C:\cmd\demo\FileNamePrefix.cmd ==========
001. @echo off
002. for /f "tokens=*" %%a in (
003. 'dir /b c:\junkdir\*Src.zip'
004. ) do call :main %%a
005. goto :EOF
006. :main
007. set filename=%*
008. set prefix=%filename:Src.zip=%
009. echo/%prefix%
010. goto :EOF
==========end file C:\cmd\demo\FileNamePrefix.cmd ==========
- - - - - - - - - - end screen capture WinXP - - - - - - - - - -
 
Top