@rem csvsort.bat
@rem RJM Programming - January, 2015
@rem Defaults to three comma separated fields to sort and default csv3.bat and vsc3.bat as written
@rem Usage: csvsort[.bat] [[numFields] [rowsToSkip]] [inputCSVFilename] [fieldNumberOfSort] [/n] [/r]
@rem Note: If no inputSVFilename existing it will ask for interactive input
@rem Defaults: /n does numerical sort and /r reverses the sort
@rem Defaults: rowsToSkip=0 fieldNumberOfSort=1 (starting at 1) numFields=3 (if specified please also specify a rowsToSkip value)
@set of=
@set three=3
@set seo=
@if trick%1==trick goto nomore
@if exist %1 goto yesmore
@set oneis=
@set bothis=%1
@for /F "usebackq tokens=1,2 delims=." %%i in (`echo %1`) do @set oneis=%%j
@if trick%oneis%==trick goto goon
@goto nomore
:goon
@set seo=%1
@if exist %seo% set seo=
@if trick%seo%==trick goto bitmore
@shift
:bitmore
@if exist %1 goto yesmore
@set oneis=
@set bothis=%1
@for /F "usebackq tokens=1,2 delims=." %%i in (`echo %1`) do @set oneis=%%j
@if trick%oneis%==trick goto goon2
@goto nomore
:goon2
@set three=%seo%
@set seo=%1
@shift
@if exist %1 goto yesmore
@set bothis=%1
@for /F "usebackq tokens=1,2 delims=." %%i in (`echo %1`) do @set oneis=%%j
@goto nomore2
:yesmore
@if trick%seo%==trick0 set seo=
@if exist mytemp.xxx erase mytemp.xxx
@set aone=%1
:preworkit
@set atwo=%2
@set athree=%3
@set afour=%4
:workit
@if trick%atwo%==trick @csv%three% %seo% %aone% | sort | find /V "usebackq " >> mytemp.xxx
@if trick%atwo%==trick @goto pastbits
@if trick%athree%%afour%==trick/r/n @csv%three% %seo% %aone% %atwo% %afour% | sort three% | find /V "usebackq " >> mytemp.xxx
@if trick%afour%%athree%==trick/r/n @csv%three% %seo% %aone% %atwo% %athree% | sort %afour% | find /V "usebackq " >> mytemp.xxx
@if trick%athree%%afour%==trick/r @csv%three% %seo% %aone% %atwo% %afour% | sort %athree%%afour% | find /V "usebackq " >> mytemp.xxx
@if trick%athree%%afour%==trick/n @csv%three% %seo% %aone% %atwo% %athree%%afour% | sort | find /V "usebackq " >> mytemp.xxx
@if trick%athree%%afour%==trick @csv%three% %seo% %aone% %atwo% | sort | find /V "usebackq " >> mytemp.xxx
:pastbits
@vsc%three% mytemp.xxx %atwo% %of%
@goto theend
:nomore
@set three=3
:nomore2
@if exist mytemp.xxx erase mytemp.xxx
@if exist mytemp.yyy erase mytemp.yyy
@if exist mytemp.zzz erase mytemp.zzz
@echo One of first three parameters should have been a filename for the non-piped scenario ... but will allow you to type away here and end with ctrl-z to work the sorting in a piped way ... %2 %oneis% ...
@copy con mytemp.yyy | tee mytemp.zzz
@set aone=mytemp.yyy
@if trick%oneis%==trick goto postworkit
@if trick%atwo%==trick @set bothis=1 %bothis%
@set of=%bothis%
@goto preworkit
:postworkit
@if trick%of%==trick goto jb
@goto preworkit
:jb
@set atwo=%1
@set athree=%2
@set afour=%3
@rem echo One of first three parameters should have been a filename for the non-piped scenario ... but will allow you to type away here and end with ctrl-z to work the sorting in a piped way ... %oneis% %three% %seo% %aone% %atwo% %athree% %afour% ...
@goto workit
:theend
