开发者

How to extract folder path from a string in a batch script

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 followin开发者_Go百科g 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?


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


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

:parentfolder
echo %~dp1
goto :eof
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜