-1

I have an array of filenames and I wish to print the file's name of each one:

For example: if the array contains the file: C:\Windows\Directx.log Then, I would like to print Directx.log

I know that if I iterate on a text file, I can do it like this:

for %%F in ("C:\Documents and Settings\Usuario\Escritorio\hello\test.txt") do echo %%~nxF

But again, it's an array and not a text file.

Here's my code:

set filesCount=0
for /f "eol=: delims=" %%F in ('dir /b /s %SequencesDir%\*.%StandardExtension%') do (
    set "filesArray!filesCount!=%%F"
    set /a filesCount+=1  
)

set /a secondCnt=0

:LoopArray
if not %secondCnt%==%filesCount% (
    set CurrentFile=!filesArray%secondCnt%!
    echo !CurrentFile!
    for /f "delims=" %%A in ('echo(%CurrentFile:\=^&echo(%') do set ExactFile=%%~nxA
echo %ExactFile%
set /a secondCnt+=1
goto LoopArray
)

Any ideas?

Idanis
  • 1,918
  • 6
  • 38
  • 69
  • I suggest you to use the standard array notation and use subscript 1 for the first element: `... do ( set /a filesCount+=1 & set "filesArray[!filesCount!]=%%F" )`. This form is clearer and aids to avoid misunderstandings. See: http://stackoverflow.com/questions/10544646/dir-output-into-bat-array/10569981#10569981 – Aacini Jan 16 '13 at 22:46

1 Answers1

0

The fact that you have an array of values is not significant. You already know how to loop through the members, so for a particular iteration, you want to convert a full file path into just the name and extension. The simple way to do that is to either use a FOR loop with a variable modifier %%~nxA or a subroutine CALL with a parameter modifier %~nx1.

Note: It is much more efficient to use a FOR /L loop to iterate your members instead of a GOTO loop.

FOR Loop solution (my preference)

for /l %%N in (0 1 %filesCount%) do (
  set "currentFile=!filesArray%%N!"
  for %%F in ("!currentFile!") do set "ExactFileName=%%~nxF"
  echo Full file path = !currentFile!
  echo File name = !ExactFileName!
)


CALL subroutine solution (slower)

for /l %%N in (0 1 %filesCount%) do (
  set "currentFile=!filesArray%%N!"
  call :getName "!currentFile!"
  echo Full file path = !currentFile!
  echo File name = !ExactFileName!
)
exit /b

:getName
set "ExactFileName=%~nx1"
exit /b
dbenham
  • 127,446
  • 28
  • 251
  • 390