Windows Batch:获取文件路径而不从bat文件覆盖文件夹

问题描述 投票:0回答:1

我想在文件夹结构下递归列出文件并读出名称和路径。

但不是完整路径,我只想要bat和文件之间的路径。

例如:

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

召唤蝙蝠进来

C:\Users\testuser\Downloads\com

结果:

com\test\test.jar

我尝试在代码中替换/删除路径的一部分

C:\Users\testuser\Downloads\
,但它不起作用:

@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!"
)

如何才能得到我想要的路径?

提前致谢

windows file batch-file directory
1个回答
0
投票

我终于得到了将 jar 一般推送到 gitlab 注册表的预期结果:

@ECHO OFF
:: Path of the bat file
SET "StartPath=%~dp0"
SETLOCAL EnableDelayedExpansion
:: Find all files in current directory with the ending of jar
for /R %%f in (*.jar) do (
  :: Absolute Path of the file
  set "fullpath=%%f"    
  set "filename=%%~nxf"
  :: Removes the startpath from the absolute path
  set "fullpath=!fullpath:%StartPath%=!"
  :: Changes backward slashes to forward slashes
  set "fullpath=!fullpath:\=/!"
  echo "fullpath: !fullpath!"
  :: curl the jar to gitlab registry artifactory
  curl --header "PRIVATE-TOKEN:12345" --upload-file !filename! "https://example.com/api/v4/projects/12345/packages/maven/!fullpath!" 
)
© www.soinside.com 2019 - 2024. All rights reserved.