I have several files in a folder; for each file, I need to parse its filename according to a specific format.
The filenames can be something like:
.dir1value1-value2-value3-2.45.13.EXT
.dir1value1-value2-value4-value5-1.17.04.EXT
In short, the format for each file I need:
- Remove the relation path
- Remove the first 2 words including the hyphens.
- Remove the file extension.
- Split the remaining to 2 text values, one is the remaining text without the numbers, and the second text is the remaining numbers.
So, the output for the 2 filenames should be:
| value3 | 2.45.13 |
| value4-value5 | 1.17.04 |
I tried the following code:
@echo off
setlocal enabledelayedexpansion
for %%p in (
.dir1*.EXT
) do (
rem This is the full name of the file (relative path + file extension )
set "relationFileName=%%p"
rem This is the full name of the file without its relative path
for %%f in ("!relationFileName!") do set filename=%%~nxf
rem Removing the file extension
set "stripped_file=!filename:~0,-4!"
rem Getting the length of the filename - function is located below
call :strlen filename_length stripped_file
rem echo file=!stripped_file!(!filename_length!^)
set "delimiter=-"
rem Iterate over the string from the end to the beginning
for /l %%i in (0, 1, !filename_length!) do (
set "index=%%i"
set char=!stripped_file:~%%i,1!
rem echo !stripped_file![!index!] = !char!
if "!char!"=="!delimiter!" set lastIndex=!index!
)
set /a nextIndex=lastIndex + 1
rem deducting 11 from the package name length since this is the prefix of "one-source-" text
rem reducing the prefix of `value1-value2-` which we don't want to print out.
set "prefixLength=14"
set /a packageNameLength = lastIndex - prefixLength
rem breaking the filename where the last hyphen character was found
set packageName=!stripped_file:~%prefixLength%,%packageNameLength%!
set packageVersion=!stripped_file:~%nextIndex%!
echo ^| !packageName! ^| !packageVersion! ^|
)
endlocal
REM ********* function *****************************
:strlen <resultVar> <stringVar>
(
setlocal EnableDelayedExpansion
(set^ tmp=!%~2!)
if defined tmp (
set "len=1"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!tmp:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "tmp=!tmp:~%%P!"
)
)
) ELSE (
set len=0
)
)
The output that I am getting is:
| | value1-value2-value3-2.45.13 |
| | value1-value2-value4-value5-1.17.04 |
The problem is that the 2 lines that stores the position where the text should be split do not evaluate in the set command (that splits the text).
You can see in the code that for prefixLength
, packageNameLength
& nextIndex
, their values are not being evaluated in the 2 set commands:
set packageName=!stripped_file:~%prefixLength%,%packageNameLength%!
set packageVersion=!stripped_file:~%nextIndex%!
Just to be sure, it outputs the correct values when I am echoing these variables (prefixLength
, packageNameLength
& nextIndex
).
It also works correctly when I am hardcoding the values directly into the set command.
But, when I put these variables into the set command, they don’t work.
Does anyone know how can I fix this?