在批处理文件中将文件夹设置为变量

时间:2021-06-09 23:21:25

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

星期一将保存到变量A,而AM将保存到变量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)

一旦我知道我将根据保存在变量上的名称创建一个if else语句(例如Monday \ AM = 1 \ 2 Tuesday \ AM = 2 \ 2 Monday \ PM = 1 \ 1)

hope this better explain my question

希望这能更好地解释我的问题

2 个解决方案

#1


3  

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%

#2


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:

我很确定,这是一个x-y问题(因此请求“尝试解释任务”)。但如果你坚持这样做:

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

这会切换到批处理文件所在的文件夹(即%~dp0),并将最后两个元素转换为变量。完成后,它将恢复到原始工作文件夹。

#1


3  

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%

#2


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:

我很确定,这是一个x-y问题(因此请求“尝试解释任务”)。但如果你坚持这样做:

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

这会切换到批处理文件所在的文件夹(即%~dp0),并将最后两个元素转换为变量。完成后,它将恢复到原始工作文件夹。