I'd like to get the parent directory of a file from within a .bat
file. So, given a variable set to "C:\MyDir\MyFile.txt"
, I'd like to get "C:\MyDir"
. In other words, the equivalent of dirname()
functionality in a typical UNIX environment. Is this possible?
相关问题
- Inheritance impossible in Windows Runtime Componen
- how to get running process information in java?
- Is TWebBrowser dependant on IE version?
- How can I have a python script safely exit itself?
- I want to trace logs using a Macro multi parameter
相关文章
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- Warning : HTML 1300 Navigation occured?
- Bundling the Windows Mono runtime with an applicat
- Windows 8.1 How to fix this obsolete code?
- Compile and build with single command line Java (L
- CosmosDB emulator can't start since port is al
- How to print to stdout from Python script with .py
If for whatever reason you can't use FOR (no Command Extensions etc) you might be able to get away with the ..\ hack:
The problem with the for loop is that it leaves the trailing \ at the end of the string. This causes problems if you want to get the dirname multiple times. Perhaps you need to get the name of the directory that is the grandparent of the directory containing the file instead of just the parent directory. Simply using the for loop technique a second time will remove the \, and will not get the grandparent directory.
That is you cannot simply do the following.
This will set dirname to "c:\1\2\3", not "c:\1\2".
The following function solves that problem by also removing the trailing \.
It is called as follows.
This will set
%dirname%
to the drive and directory of the file name stored in%filename%
.Careful with filenames containing spaces, though. Either they have to be set with surrounding quotes:
or you have to put the quotes around the argument in the
for
loop:Either method will work, both at the same time won't :-)