Reputation: 575
I have a directory with following files:
PPP1234MM.xml
PPP4532MM.xml
PPP9843MM.xml
I need to extract the numeric parts of the filenames using batch command. Please help!
Upvotes: 1
Views: 7394
Reputation: 70923
for /f "tokens=*" %%f in ('dir /l /a-d /b *.xml') do (
for /f "tokens=1 delims=abcdefghijklmnopqrstuvwxyz" %%n in ("%%~nf") do (
echo File: %%~nxf = Number %%n
)
)
Use the non numeric characters as delimiters in a for
command to remove them of the name. The list of files is retrieved in lowercase to limit the number of delimiters in list.
Upvotes: 1
Reputation: 80023
@ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN (
'dir /b /a-d "ppp*.xml" '
) DO (
SET "number="
SET "name=%%a"
CALL :process "%%a"
)
GOTO :EOF
:notnum
IF DEFINED number GOTO show
:num
SET "name=%name:~1%"
:process
IF NOT DEFINED name GOTO show
if "%name:~0,1%" lss "0" GOTO notnum
if "%name:~0,1%" gtr "9" GOTO notnum
SET "number=%number%%name:~0,1%"
GOTO num
:show
ECHO file %1 --^> %number%
GOTO :eof
This should extract and show the numeric part. You may wish to change the filematch mask. I simply used ppp*.xml
for my testing
Upvotes: 0
Reputation: 934
If the pattern is the same of your question (3 characters, 4-digit, 2 characters), use the code below, if you have other standards to help inform you better.
@echo off
@title Get Part of filename batch
@color 0a
setlocal EnableDelayedExpansion
for %%A in ( "*.xml" ) do (
REM Set Filename
set "FileFull=%%~nxA"
REM Set Number, offset equal 3 (skip 3 first characteres), length equal 4 (four characteres)
set "FileNumber=!FileFull:~3,4!"
echo !FileNumber!
)
pause
exit
Upvotes: 0