Batch File - going back two steps in a directory path

老子叫甜甜 提交于 2019-12-29 07:21:32

问题


I am creating a batch file i am on a path

C:\Validation\docs\chm

I want to move back to the

C:\Validation part 

which is in %DialogPath% This was entered by the user but when i write

CD /D %DialogPath%

An error occurs that tells this path does not exists


回答1:


The direct answer to your question would be

cd ..\..

But cd /D C:\Validation also works.

The problem is more likely with the variable than then command.




回答2:


Until you give more details as to the script in question, we can only guess to what the problem may be.

However, since you are changing the current directory only for a limited time you should be using the pushd and popd commands.

Example: (Run this .bat script to see how pushd and popd work!)

:: Hide Commands
@echo off

:: Display Current Working Directory
echo Current Directory = %CD%

:: Create folders for demonstration purposes only
rd /Q "%Temp%\Test" 2>nul & mkdir "%Temp%\Test" & mkdir "%Temp%\Test\Subfolder"

:: Change the Working Directory
pushd "%Temp%"

:: Display Current Working Directory
echo Current Directory = %CD%

pushd "%Temp%\Test\Subfolder"

:: Display Current Working Directory
echo Current Directory = %CD%

:: Revert back to the previous Working Directory
popd

:: Display Current Working Directory
echo Current Directory = %CD%

:: Revert back to the previous Working Directory
popd

:: Display Current Working Directory
echo Current Directory = %CD%

pause

For help type pushd /? or popd /? into the command prompt.




回答3:


You can move up one path with cd ... If you do that twice, you will land in the C:\Validation directory.

In your example it would look like this:

C:\Validation\docs\chm> cd ..

C:\Validation\docs> cd ..

C:\Validation>


来源:https://stackoverflow.com/questions/14070252/batch-file-going-back-two-steps-in-a-directory-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!