windowsfilebatch-filedirectory

Windows Batch: Get file Path without overlaying folders from bat file


I want to list recursively files under a folder structure and read out the name and the path.

But not the fullpath, I want only the path between the bat and the file.

For Example:

C:\Users\testuser\Downloads\com\test\test.jar

Call the Bat in

C:\Users\testuser\Downloads\com

Result:

com\test\test.jar

I tried in my code to replace/remove the part of the path C:\Users\testuser\Downloads\ but it doesn't work:

@ECHO OFF
SET "StartPath=%cd%"
setlocal enabledelayedexpansion
for /R %%f in (*.jar) do (
  set fullpath=%%f
  echo "fullpath: !fullpath!"
  echo "name: %%~nxf"
  set jarpath=%!fullpath!:!StartPath!=%
  echo "jarpath: !jarpath!"
)

How can i get the path I want?

Thanks in advance


Solution

  • Despite your own answer, this provides the code for the specific problem you laid out in your question.

    For Example:

    C:\Users\testuser\Downloads\com\test\test.jar
    

    Call the Bat in

    C:\Users\testuser\Downloads\com
    

    Result:

    com\test\test.jar
    
    @Echo Off
    SetLocal EnableExtensions DisableDelayedExpansion
    
    For %%G In ("%~dp0.") Do For /R "%~dp0." %%H In ("*.jar") Do (Set "FullPath=%%H"
        SetLocal EnableDelayedExpansion
        Echo "fullpath: !FullPath!"& Echo "name: %%~nxH"
        For %%I In ("!FullPath:%%~fG=%%~nxG!") Do EndLocal & Echo "jarpath: %%~I"
    ) 
    
    Pause
    
    EndLocal
    Exit /B