views:

29

answers:

2

I am trying to make a relative shortcut using a batchfile and a program to turn the batch in an exe program with an icon.

I need a 'shortcut' to open the next alfabetic parallel folder in an explorer window and one to open the previous. Ideally I would even like it to to close the explorer window used to doubleclick it.

I have so far:

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

the %folder% has the name of the folder (not path!) from where the batch is executed.

the line: dir /A:D /B gives you an output of multiple lines giving you all the parallel folders (because I go up a level using pushd ..). I really need to find a way to search for the %fodler% value and pick the line above, or below it.

I tried something using for /f but it is not that usefull when processing multiple lines instead of one single string.

anny ideas?

A: 

Even though it is not quite clear to me what exactly you want to achieve, the following snippet should do what you want. Simply add it to your script:

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%

Explanation:

The 3 variables previous, current and next are use like a shift register. In each loop iteration the current directory value is shifted by one place through the variables

At the end of the shifting the current variable is tested for the desired folder

If the loop ends before the condition is true, this means the last folder is the correct one, hence that trailing shifts.

Hope that helps...

Frank Bollack
Sorry I exidently added the previous comment in a answer, anyway, I added this to the next version:if "%next%."=="." goto :eof <br/>set nextpath=%cd%\%next% <br/>echo %nextpath% <br/>explorer.exe "%nextpath%" <br/>exit <br/>in stead of <br/> i loved to see a linefeed, but those are removed by the commentdoes exactly what is should, but how do I close the old window? this one just pops ontop of the one were the batch is excuted from
A: 

thanks man, this works great, but still a lot better when "delims=\" is added, since \ never exists in a folder name, otherwise the foldernames wil be broken ('my documents' becomes just: 'my') and the current folder will never be found.

I still need to find a way to open one of the found folders in an explorer window and preferebly a way to close the older one ( for exapmle if i have a folder a , b and c and put a previous.bat or previous.cmd in there, i would like to doubleclick it and find myself looking at a window displaying folder a and its contents, but I would prefere having closed the original window showing the contents of folder b.) Does anyone know how to do this?

I hope it is clear to you now.