Leona
Leona

Reputation: 377

Batch File | Parent Directory

Hell all, I'm a newbie of Batch File I have a batch File like:

@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\

Attention: I don't want to get the path of icon.png by CD in that folder, what i want is to retrieve the files inside resources folder when the current folder is "To_delete".

Thank you.

Upvotes: 2

Views: 13797

Answers (4)

SGalea
SGalea

Reputation: 722

@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"

Last command will give you the path of all the files in the resources folder. now you can elaborate to get your answer

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

Upvotes: 3

jeb
jeb

Reputation: 82418

You could solve it with a FOR loop and the file modifier ~f.
Look also at FOR /?

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

Upvotes: 1

calsign
calsign

Reputation: 341

Have you tried ../ (or ..\ for Windows)? For example: echo The icon.png is located at "..\Resource\"

Upvotes: 0

lorenz albert
lorenz albert

Reputation: 1459

to change to the parent diretory you can use

cd ..

so if you are in "To_delete" your command needs to look like this

cd ..\Resource

to get into the Resource folder

Upvotes: 1

Related Questions