lielo
lielo

Reputation: 33

batch: get last folder name from path from txt

i need take paths from txt and get last folder name and then use it.

setlocal EnableDelayedExpansion

set InputFile=somar.txt
for /f "tokens=* delims=" %%x in ('Type "%InputFile%"') do (
set path=%%x
:shift
for /f "tokens=1* delims=\/" %%i in ( "!path!" ) do (
    set folder=%%i
    set path=%%j
    )
if not [!path!] == [] goto :shift

echo folder: !folder!
)
endlocal

problem is that it works only for first line in txt. where is the problem?

Upvotes: 3

Views: 1199

Answers (3)

Kim Ki Won
Kim Ki Won

Reputation: 1855

InputFile content must contains filename or folder.
ex)

D:\Test1  <= folder
D:\Test2\file.txt  <= file

D:\Test3\01. folder <= folder but recognize file. that contain extension.

My code is:

SETLOCAL EnableDelayedExpansion

SET lastFolder=
SET InputFile=somar.txt

FOR /F %%F IN (%InputFile%) DO (
    CALL :__GetFolderName "%%F"
    @ECHO lastFolder: !lastFolder!
)

GOTO :EOF


:: ******************** Inner  Batch
:__GetFolderName
IF "%~x1"=="" SET lastFolder=%~n1 & GOTO :EOF

SET dp=%~dp1

CALL :__GetFolderName "%dp:~0,-1%"

GOTO :EOF
:: ********************

ENDLOCAL

Result is:

lastFolder: Test1
lastFolder: Test2
lastFolder: Test3

Upvotes: 0

ElektroStudios
ElektroStudios

Reputation: 20494

For variable filenames:

@Echo OFF

:: By Elektro H@cker

set "File=File.ext"
Call :Get_paths "%InputFile%"

Pause&exit

:Get_paths
Set "AbsolutePath=%~dp1"
set "AbsolutePath=%AbsolutePath:\= %"
FOR %%# in (%AbsolutePath%) do (
    Set "LastFolder=%%#"
    Echo Folder: %%#
)
Echo Last Folder: %LastFolder%
GOTO:EOF

Output:

Folder: C:
Folder: Users
Folder: Administrador
Folder: Desktop

Last Folder: Desktop

For files:

@Echo OFF

:: By Elektro H@cker

set "File=test.txt"

For /F "Tokens=* usebackq" %%# in ("%FILE%") DO (   
    Set "AbsolutePath=%%~dp#"
    Call set "AbsolutePath=%%AbsolutePath:\= %%"
    CMD /Q /C "@FOR %%@ in (%%AbsolutePath%%) do (Echo Folder: %%@)"
)

Pause&Exit

Upvotes: 0

dbenham
dbenham

Reputation: 130919

You have a number of problems:

1) Your FOR loop is broken the moment you issue a GOTO. You will not get any more FOR loop iterations after GOTO. You could fix this by moving your GOTO loop into a subroutine and then CALL the subroutine from within your DO loop.

2) The PATH environment variable has a reserved meaning for Windows. You should never use that variable name for your own purposes, even if it is localized as you have it. It isn't worth tempting fate. Simply use a different variable name.

3) Perhaps not an issue with your data, but ! is a valid character for a file or folder name. Your expansion of FOR variables will corrupt names containing ! if delayed expansion is enabled. This can be fixed by toggling delayed expansion on and off as needed.

You also have a minor inefficiency - There is no need to use TYPE with your FOR loop. You can simply let FOR read the file directly. (unless the file is unicode)

You could take all the recommendations above, but there is a much simpler solution :-)

EDIT - change made to handle a path that ends with \

@echo off
set "InputFile=somar.txt"
for /f "usebackq eol=: delims=" %%A in ("%inputFile%") do (
  for %%F in ("%%A\.") do echo folder: %%~nxF
)

The ~nx FOR variable modifiers directly give you the name and extension of the end of the path. Type HELP FOR from the command line to read about all the modifiers that are available to FOR variables.

Upvotes: 2

Related Questions