1

我正在尝试使用批处理文件和程序创建一个相对快捷方式,以将批处理转换为带有图标的 exe 程序。

我需要一个“快捷方式”来在资源管理器窗口中打开下一个字母并行文件夹,并需要一个来打开上一个。理想情况下,我什至希望它关闭用于双击它的资源管理器窗口。

我到目前为止:

@echo off
echo %cd%
for %%a in ("%cd%") do set folder=%%~na
pushd ..
echo %folder%
echo .
dir /A:D /B
pause

%folder% 具有执行批处理的文件夹的名称(不是路径!)。

该行: dir /A:D /B 为您提供多行的输出,为您提供所有并行文件夹(因为我使用 pushd .. 升级了一个级别)。我真的需要找到一种方法来搜索 %fodler% 值并选择上面或下面的行。

我尝试了一些使用 for /f 的方法,但在处理多行而不是单个字符串时它不是很有用。

有什么想法吗?

4

1 回答 1

0

尽管我不太清楚您到底想要实现什么,但以下代码段应该可以满足您的需求。只需将其添加到您的脚本中:

setlocal enabledelayedexpansion

for /f %%a in ('dir /b /ad /on') do (

   @rem shift the current value through the variables
   set previous=!current!
   set current=!next!
   set next=%%a

   @rem check if the "current" value is the right one
   if "!current!"=="%folder%" goto :found
)

@rem if we get here the loop has finished without %current% having the expected value
@rem but we need to check if it was the last folder in the directory
if "%next%"=="%folder%" (
    set previous=%current%
    set current=%next%
    set next=
    goto :found
)

endlocal

@rem exit here if no match is found (should never happen)
goto :eof

@rem variables should have valid values
:found
echo %previous%
echo %current%
echo %next%

解释:

这 3 个变量previous和使用起来就像一个移位寄存器currentnext在每次循环迭代中,当前目录值通过变量移动一个位置

在移位结束时,current针对所需文件夹测试变量

如果循环在条件为真之前结束,这意味着最后一个文件夹是正确的,因此尾随移位。

希望有帮助...

于 2010-08-06T21:22:53.690 回答