批处理文件|父目录

时间:2012-08-28 13:17:22

标签: batch-file directory cmd parent

地狱,我是批处理文件的新手 我有一个批处理文件,如:

@ECHO OFF
if not exist To_delete mkdir To_delete
if not exist resources mkdir resources
copy "C:\icon.png" "resources"

cd /d To_delete
echo The current folder is : "%CD%"
echo The icon.png is located at "CODE_I_NEED_IS_HERE"

The result should be:
The current folder is : C:\Users\Leona\Desktop\To_delete\
The icon.png is located at C:\Users\Leona\Desktop\Resource\

注意:我不想在该文件夹中通过CD获取icon.png的路径,我想要的是在当前文件夹为“To_delete”时检索资源文​​件夹中的文件。

谢谢。

4 个答案:

答案 0 :(得分:3)

@ECHO OFF
if not exist To_delete mkdir To_delete
if not exist resources mkdir resources
copy "C:\Documents and Settings\mlastg\Desktop\logo.png" "resources"

cd /d To_delete
echo The current folder is : "%CD%" 
dir /s/b/A:-D "%CD%\..\resources"

上一个命令将为您提供resources文件夹中所有文件的路径。现在你可以详细说明你的答案了

C:\Documents and Settings\mlastg\Desktop>batch.bat
        1 file(s) copied.
The current folder is : "C:\Documents and Settings\mlastg\Desktop\To_delete"
C:\Documents and Settings\mlastg\Desktop\resources\logo.png

答案 1 :(得分:1)

更改为您可以使用的父目录

cd ..

所以如果你在“删除”中,你的命令需要看起来像这样

cd ..\Resource

进入资源文件夹

答案 2 :(得分:1)

您可以使用FOR循环和文件修饰符~f来解决它 另请参阅FOR /?

for %%a in ("%CD%\..\resources") do set resourceDir=%%~fa
echo The icon.png is located at "%resourceDir%"

答案 3 :(得分:0)

您是否尝试过../(或..\ for Windows)? 例如:echo The icon.png is located at "..\Resource\"