如何从批处理脚本中的字符串中提取文件夹路径

发布于 2024-12-08 07:47:36 字数 654 浏览 0 评论 0原文

我一直在致力于构建一个打包实用程序,它基本上获取两个 SVN 修订版之间所有添加/修改的文件,然后将它们复制到本地并压缩它们。到目前为止,我已经能够成功提取两个修订版之间的所有更改文件。

为了更进一步,我使用 xcopy 在某个目录中递归地创建文件夹。

假设当我使用 svn diff 命令检查两个修订版时以下文件已更改

/temp1/temp2/temp3/temfile.txt
/temp1/temp21/temp31/tempfile.txt
/temp1/temp2/ (folder created)
/temp1/temp2/temp3 (folder created)

为了使 XCopy 工作,我正在执行

xcopy local/svn/copy/path d:/{folderpath} 

需要从上述更改列表中提取文件夹路径的操作

xcopy "C:/LocalSVN/temp1/temp2/temp3/temfile.txt" "d:/temp1/temp2/temp3/"

,例如我需要在我的批处理文件中仅提取文件夹路径和删除文件名。在批处理文件中执行此操作的最佳方法是什么?

有没有不同的方法来实现我想要做的事情?

I have been working on building a packaging utility which basically gets all added/modified files between two SVN revisions, then copies them locally and zips them.So far, I have been able to successfully extract all changed files between two revisions.

To go further, I am using xcopy to recursively create folders at a certain directory.

Assuming that the following files have changed when I check two revisions using svn diff command

/temp1/temp2/temp3/temfile.txt
/temp1/temp21/temp31/tempfile.txt
/temp1/temp2/ (folder created)
/temp1/temp2/temp3 (folder created)

For XCopy to work, I am doing

xcopy local/svn/copy/path d:/{folderpath} 

where folderpath needs to be extracted from the above changed list e.g.

xcopy "C:/LocalSVN/temp1/temp2/temp3/temfile.txt" "d:/temp1/temp2/temp3/"

I need to in my batch file, extract only the folder path and remove the file name.What is the best way to do it in a batch file?

Is there a different way to achieve what I am trying to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2024-12-15 07:47:36

它与如何在批处理文件中获取部分目录路径中几乎相同,

关键是使用 % ~dp 功能,并且由于这只适用于参数(不适用于变量),因此您可以使用 FOR 循环或子例程将变量移至参数中。

@echo off
set "testString=/temp1/temp2/temp3/temfile.txt"

call :GetPath returnVal "%testString%"
echo %returnVal%
exit /b

:GetPath
set "%1=%~dp2"
exit /b

It's nearly the same as in How to get a Part of the Directory path in a batch file

The key is to use the %~dp functionality, and as this only works with parameters (not variables), you can use a FOR-Loop or a subroutine to move your variable into a parameter.

@echo off
set "testString=/temp1/temp2/temp3/temfile.txt"

call :GetPath returnVal "%testString%"
echo %returnVal%
exit /b

:GetPath
set "%1=%~dp2"
exit /b
咋地 2024-12-15 07:47:36
@echo off
setlocal
SET SUBDIR=%~dp0
call :parentfolder %SUBDIR:~0,-1% 
endlocal
goto :eof

:parentfolder
echo %~dp1
goto :eof
@echo off
setlocal
SET SUBDIR=%~dp0
call :parentfolder %SUBDIR:~0,-1% 
endlocal
goto :eof

:parentfolder
echo %~dp1
goto :eof
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文