0

I am trying to get put my containing folder as a set of variable in my batch file. For example i Have

c:\Monday\AM

Monday will be save to variable A while AM will be save to variable B

How can I achieve that?

Once I know that I will create an if else statement base on the name saved on the variable (e.g Monday\AM = 1\2 Tuesday\AM = 2\2 Monday\PM = 1\1)

hope this better explain my question

8
  • 1
    it looks like you're asking for somthing you think you need, but probably don't. Try to explain the task, and provide your code so far. Commented Oct 25, 2016 at 13:58
  • A folder and B folder is always changing and there is a corresponding number base on the folder name (eg sample\folder = 1\2 sample2\folder = 2\2) Thats why i want to save those two folders name in a variable to create an if else (not loop sorry) . Honestly I dont have any code for this as I only know very basic in batch file/script? Commented Oct 25, 2016 at 14:09
  • How does the batch file know what folder structure to use if you do not already know it? Commented Oct 25, 2016 at 14:32
  • 1
    Please update your post with the real folder path and names an explain what you mean by corresponding base number on the folder name. Adding information to the comments are either not seen or too difficult to read. Commented Oct 25, 2016 at 14:43
  • 1
    Please put all information directly related to the question into your post by editing rather than spreading it over multiple comments! Commented Oct 25, 2016 at 16:07

2 Answers 2

4

This will get the directory the batch file resides in (child) and the folder above that (parent).

@echo off
for %%G in ("%~dp0\.") do set child=%%~nxG
for %%G in ("%~dp0\..") do set parent=%%~nxG
echo child=%child%
echo parent=%parent%
Sign up to request clarification or add additional context in comments.

Comments

0

I'm quite sure, this is a x-y problem (therefore the request "Try to explain the task"). But if you insist in doing it that way:

@echo off
pushd %~dp0
for %%X in (%cd%) do set _b=%%~nxX
pushd ..
for %%X in (%cd%) do set _a=%%~nxX
popd
popd
echo %_a%, %_b%

This switches into the folder where your batch-file resides (that's %~dp0) and gets the last two elements into variables. When finished, it restores to the original working folder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.