Batch for grabbing second to last delim

November 1, 2010 at 11:49:41
Specs: Windows XP
@echo off
setlocal enabledelayedexpansion
goto game
:game
pushd "C:\Documents and Settings\esintrn3\Desktop\sidewalk test"
for %%j in (*) do (
set str=%%j
set nota=!!
set /a test=-1
for /f "tokens=1,2,3,4,5,6,7,8 delims=_ " %%a in ("!str!") do set numb=%%a&set street1=%%b&set street2=%%c&set street3=%%d&set street4=%%e&set street5=%%f&set street6=%%g&set street7=%%h
goto test7
:test7
if !street7!==!nota! (goto test6) else (set /a test=%test%+1)
:test6
if !street6!==!nota! (goto test5) else (set /a test=%test%+1)
:test5
if !street5!==!nota! (goto test4) else (set /a test=%test%+1)
:test4
if !street4!==!nota! (goto test3) else (set /a test=%test%+1)
:test3
if !street3!==!nota! (goto test2) else (set /a test=%test%+1)
:test2
if !street2!==!nota! (goto test1) else (set /a test=%test%+1)
:test1
if !street1!==!nota! (goto test0) else (set /a test=%test%+1)
:test0
if !test!==0 (set street="unknown")
if !test!==1 (set street=!street1!)
if !test!==2 (set street=!street1! !street2!)
if !test!==3 (set street=!street1! !street2! !street3!)
if !test!==4 (set street=!street1! !street2! !street3! !street4!)
if !test!==5 (set street=!street1! !street2! !street3! !street4! !street5!)
if !test!==6 (set street=!street1! !street2! !street3! !street4! !street5! !street6!)
if !test!==7 (set street=!street1! !street2! !street3! !street4! !street5! !street6! !street7!)
if exist !street!\ (
move "!str!" "!street!"
pushd "C:\Documents and Settings\esintrn3\Desktop\sidewalk test\!street!"
) else (
mkdir "C:\Documents and Settings\esintrn3\Desktop\sidewalk test\!street!"
echo %%j created !street!: folder
move "!str!" "!street!"
pushd "C:\Documents and Settings\esintrn3\Desktop\sidewalk test\!street!"
)
if exist !numb!\ (
move "!str!" "!numb!"
goto game
) else (
mkdir "C:\Documents and Settings\esintrn3\Desktop\sidewalk test\!street!\!numb!"
echo %%j created !numb!: folder
move "!str!" "!numb!"
goto game
)
)
popd
pause
exit

If i get a street like 1500 Street Name_01 I need to find the street name folder or create a new one if it doesn't exist. Then it needs to find the number 1500 and either move the file to that folder or create a new one.
The program works but the test seems way too long. Is there any way I can shorten it? Being able to grab the second to last existing token would help with this program and another.

Thanks
P.S. This is my first batch program i've ever written.


See More: Batch for grabbing second to last delim

Report •


#1
November 1, 2010 at 13:16:50
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%i in (myfile) do (
call :sub1 %%i
echo.!T!
)
goto :eof

:sub1

:loop
  if '%3' equ '' (
    set T=%1
    ) else (
    shift
    goto :loop
  )
goto :eof


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
November 1, 2010 at 13:29:51
Now I have no time to shrink your script, but I notice you code GOTOs referencing labels defined *inside* the FOR loop.

This is not allowed since the execution of those statements disrupts the nominal control flow. As a general rule yuo can't define labels inside a sequence of statements embraced by left and right parenthesis.

In batch scripting sometime you have to strictly apply the rules of structured (gotoless) programming.


Report •

#3
November 2, 2010 at 05:17:47
Grabbing the second to last token should be very useful, thanks.

I did notice that when I did a goto command it exited the loop, but the program was working after a quick command call back to :game so I left it alone.

Thanks for the responses.


Report •

Related Solutions


Ask Question