Thursday, December 07, 2006

Using "ls" in Windows

As my professional life moves into a more UNIX direction I have found myself more and more often trying to execute UNIX commands in Windows. One such command is "ls" - to list the contents of a directory. I know that cygwin binaries can be compiled for Windows to get ls functionality, but I decided to solve the problem a different way. I've written a batch file that wraps the "dir" command in windows. To work properly, both "ls.bat" and "getstrlen.bat" from an earlier post on this blog, will need to be present in your PATH.

I don't have time to explain this code right now, but I will make an effort if anyone asks.

ls.bat:

@echo off

if not "%1"=="" goto gotargs
echo Executing: dir /b&dir /b
goto done

:gotargs
set ARGS1=%1
set CHECK=%ARGS1:~0,1%
if "%CHECK%"=="-" (set CURROPT=%ARGS1% & goto parsearg1)
if not "%9"=="" (echo Executing: dir /b "%1 %2 %3 %4 %5 %6 %7 %8 %9"&dir /b "%1 %2 %3 %4 %5 %6 %7 %8 %9"&goto done)
if not "%8"=="" (echo Executing: dir /b "%1 %2 %3 %4 %5 %6 %7 %8"&dir /b "%1 %2 %3 %4 %5 %6 %7 %8"&goto done)
if not "%7"=="" (echo Executing: dir /b "%1 %2 %3 %4 %5 %6 %7"&dir /b "%1 %2 %3 %4 %5 %6 %7"&goto done)
if not "%6"=="" (echo Executing: dir /b "%1 %2 %3 %4 %5 %6"&dir /b "%1 %2 %3 %4 %5 %6"&goto done)
if not "%5"=="" (echo Executing: dir /b "%1 %2 %3 %4 %5"&dir /b "%1 %2 %3 %4 %5"&goto done)
if not "%4"=="" (echo Executing: dir /b "%1 %2 %3 %4"&dir /b "%1 %2 %3 %4"&goto done)
if not "%3"=="" (echo Executing: dir /b "%1 %2 %3"&dir /b "%1 %2 %3"&goto done)
if not "%2"=="" (echo Executing: dir /b "%1 %2"&dir "%1 %2"&goto done)
echo Executing: dir /b %1&dir /b %1
goto done

:parsearg1
call getstrlen %CURROPT%
REM echo Got strlen: %strlen%
set ARGLEN=%strlen%
set count=%ARGLEN%
set i=1
set newargstr=
set CURROPT=%CURROPT: =%
set remain=%CURROPT%
goto parsearg2

:parsearg2
echo>%temp%\helper.bat set token=%%remain:~0,1%%
echo>>%temp%\helper.bat set CURROPT=%CURROPT%
echo>>%temp%\helper.bat set remain=%%CURROPT:~-%count%%%
call %temp%\helper.bat
del %temp%\helper.bat
if %i% GTR 2 goto resolvearg
:argresolved
set /a i="i+1"
set /a count="count-1"
if "%count%"=="-1" goto parsed
goto parsearg2

:resolvearg
if "%token%"=="l" goto arg_l
if "%token%"=="s" goto arg_s
if "%token%"=="a" goto arg_a
if "%token%"=="t" goto arg_t
if "%token%"=="r" goto arg_r
if "%token%"=="R" goto arg_CR
goto argresolved

:arg_l
if not "%newargstr%"=="" (set newargstr=%newargstr: /n=%)
if not "%newargstr%"=="" (set newargstr=%newargstr: /q=%)
set newargstr=%newargstr% /n /q
goto argresolved

:arg_s
if not "%newargstr%"=="" (set newargstr=%newargstr: /n=%)
if not "%newargstr%"=="" (set newargstr=%newargstr: /q=%)
set newargstr=%newargstr% /n /q
goto argresolved

:arg_a
if not "%newargstr%"=="" (set newargstr=%newargstr: /a=%)
set newargstr=%newargstr% /a
goto argresolved

:arg_t
if not "%newargstr%"=="" (set newargstr=%newargstr: /o:d=%)
if not "%newargstr%"=="" (set newargstr=%newargstr: /t:w=%)
set newargstr=%newargstr% /o:d /t:w
goto argresolved

:arg_r
if not "%newargstr%"=="" (set newargstr=%newargstr:d=-d%)
if not "%newargstr%"=="" (set newargstr=%newargstr: /t:w=%)
set newargstr=%newargstr% /t:w
goto argresolved

:arg_CR
if not "%newargstr%"=="" (set newargstr=%newargstr: /s=%)
set newargstr=%newargstr% /s
goto argresolved


:parsed
if not "%9"=="" (echo Executing: dir %newargstr% "%2 %3 %4 %5 %6 %7 %8 %9"&dir %newargstr% "%2 %3 %4 %5 %6 %7 %8 %9"&goto done)
if not "%8"=="" (echo Executing: dir %newargstr% "%2 %3 %4 %5 %6 %7 %8"&dir %newargstr% "%2 %3 %4 %5 %6 %7 %8"&goto done)
if not "%7"=="" (echo Executing: dir %newargstr% "%2 %3 %4 %5 %6 %7"&dir %newargstr% "%2 %3 %4 %5 %6 %7"&goto done)
if not "%6"=="" (echo Executing: dir %newargstr% "%2 %3 %4 %5 %6"&dir %newargstr% "%2 %3 %4 %5 %6"&goto done)
if not "%5"=="" (echo Executing: dir %newargstr% "%2 %3 %4 %5"&dir %newargstr% "%2 %3 %4 %5"&goto done)
if not "%4"=="" (echo Executing: dir %newargstr% "%2 %3 %4"&dir %newargstr% "%2 %3 %4"&goto done)
if not "%3"=="" (echo Executing: dir %newargstr% "%2 %3"&dir %newargstr% "%2 %3"&goto done)
if not "%2"=="" (echo Executing: dir %newargstr% %2&dir %newargstr% %2&goto done)
echo Executing: dir %newargstr%&dir %newargstr%
goto done


:done

No comments: