String Split issue in for loop... while process folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mskavim
Posts: 9
Joined: 25 Jan 2015 14:51

String Split issue in for loop... while process folders

#1 Post by mskavim » 25 Jan 2015 15:22

Hello Everybody,

I am problem splinting the string in a loop. I have a subroutine (:STRING_SPLIT ) that splits the string by delimiter and return the split value. But the returned value is empty.
So for the debug purpose I called ":STRING_SPLIT" in another wrapper subroutine ":SPLIT_WRAP" to check if the string split value is retuned and yes I am getting the value I want.
But I am not getting this string split value in the main.

My Requirement:-
-----------------------
I have a folder structure like

+PHASE1 ( Directory)
++PREFERENCES ( Sub - Directory)
+++SITE ( Sub Sub - Directory)
+++GROUP ( Sub Sub - Directory)
++++FIlE1.xml
++++FIlE2.xml
++++RUN.bat

Code:-
-----------------

Code: Select all

:: Set TITLE name...
SET JOB_TITLE=Executing:{ %ROOT_FOLDER% }
TITLE %JOB_TITLE%

ECHO #=================== START:: %ROOT_FOLDER% ========================#
FOR /f "tokens=*" %%D IN ('DIR /B /AD /ON') DO (
    ECHO #---"%%D"---#
    CD "%%D"
   FOR /f "tokens=*" %%F IN ('DIR /B *') DO (
      IF /I "%%D"=="Preferences" (
         REM ECHO Importing file "%%F"...
         IF /I "%%F"=="group" (
            REM ECHO DO NOTHING..
            CALL :LOAD_SUB "group" %%D
         ) ELSE IF /I "%%F"=="role" ( 
            REM ECHO DO NOTHING..
            CALL :LOAD_SUB "role" %%D
            ECHO.
         ) ELSE (
            ECHO Importing file "%%F"...            
         )         
      )
   )
    CD ..
)
ECHO #=================== END:: %ROOT_FOLDER% ==========================#
cmd

:endext
endlocal
set EXITVALUE=%ERRORLEVEL%


:LOAD_SUB
::SETLOCAL
ECHO.
::SET CURRENT_DIR=%~1
REM ECHO I am processing %~1 preferences.
ECHO    #---"%~2":"%~1"---#
CD "%~1"
FOR /f "tokens=*" %%P IN ('DIR /B *') DO (
   :: IF EXIST *.xml( ECHO       Importing file "%%P"... )
   ECHO       Processing file/directory "%%P"...
   :: ECHO.CURRENT_DIR   :   %CURRENT_DIR%
    IF /I "%~2"=="Preferences" (
   
      IF /I "%~1"=="role" (
      
         CALL:STRING_SPLIT "%%P" "." "FileName" "FileExtn"   
         ECHO.FileName   : %FileName%  REM $$$ ERROR : EMPTY VALUE
         ECHO.FileExtn   : %FileExtn%  REM $$$ ERROR : EMPTY VALUE
         IF /I "%FileExtn%"=="XML" (
         
            :: CALL IMPORT COMMAND         
            :: CALL :IMPORT_PREFERENCES "%%P"   
         )
      )
   )
)
CD..
::ENDLOCAL
GOTO:EOF


:STRING_SPLIT
:: CALL:STRING_SPLIT %data% "." "FileName" "FileExtn"
SETLOCAL ENABLEDELAYEDEXPANSION
 SET MY_STRING=%~1
 SET DELIMITER_CHAR=%~2
 ::ECHO.MY_STRING   :   %MY_STRING%
 ::ECHO.DELIMITER_CHAR   : %DELIMITER_CHAR%
FOR /f "tokens=1* delims=%DELIMITER_CHAR%" %%x IN ("%MY_STRING%") DO SET FileName=%%x&SET FileExtn=%%y
::ECHO.FileName : %FileName%
::ECHO.FileExtn : %FileExtn%
ENDLOCAL & SET %~3=%FileName% & SET %~4=%FileExtn%
::exit /b
GOTO:EOF

:SPLIT_WRAP
SETLOCAL
SET tSTRING=%~1
SET DEL_CHAR=%~2
ECHO.tSTRING : %tSTRING%
ECHO.DEL_CHAR : %DEL_CHAR%
      CALL :STRING_SPLIT %tSTRING% %DEL_CHAR% "FileName" "FileExtn"
      ECHO.FileName   : %FileName%
      ECHO.FileExtn   : %FileExtn%
ENDLOCAL & (SET %~3=%FileName%) & ( SET %~4=%FileExtn% )
GOTO:EOF



Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: String Split issue in for loop... while process folders

#2 Post by Squashman » 25 Jan 2015 16:30

Probably help if you used DelayedExpansion with your variables where required. That is just the first thing I noticed wrong. I haven't looked over your code completely.

mskavim
Posts: 9
Joined: 25 Jan 2015 14:51

Re: String Split issue in for loop... while process folders

#3 Post by mskavim » 25 Jan 2015 17:23

I tried this ....but still I see the same issue.....

Code: Select all

:LOAD_SUB
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO.
SET CURRENT_DIR=%~1
REM ECHO I am processing %~1 preferences.
ECHO    #---"%~2":"%~1"---#
CD "%~1"
FOR /f "tokens=*" %%P IN ('DIR /B *') DO (

   :: IF EXIST *.xml( ECHO       Importing file "%%P"... )
   ECHO       Processing file/directory "%%P"...
   :: ECHO.CURRENT_DIR   :   %CURRENT_DIR%      
   IF /I "%~1"=="RevisionRule" (
      Setlocal EnableDelayedExpansion
      CALL :STRING_SPLIT "%%P" "." "FileName" "FileExtn"
      ECHO.FileName   : %FileName%
      ECHO.FileExtn   : %FileExtn%
      
      IF /I "%FileExtn%"=="XML" (
         CALL :IMPORT_REV_RULE "%%P"
      )
      endlocal
   )
)
CD..
ENDLOCAL
GOTO:EOF


Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: String Split issue in for loop... while process folders

#4 Post by Squashman » 25 Jan 2015 17:34

I have no idea why you are enabling delayed expansion twice within that set of code.

But when you are in a code block you have to reference your variables with exclamation points.

If you read the help for the SET command you should see the following example.

Code: Select all

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "!VAR!" == "after" @echo If you see this, it worked
    )

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: String Split issue in for loop... while process folders

#5 Post by penpen » 25 Jan 2015 18:07

You should not use "cmd" in your OP in order to pause/stop execution, so you should remove this line.
You should also insert a "goto :eof" or "exit /B %EXITVALUE%" (or similar) above ":LOAD_SUB", to avoid processing :LOAD_SUB without calling it.

penpen

mskavim
Posts: 9
Joined: 25 Jan 2015 14:51

Re: String Split issue in for loop... while process folders

#6 Post by mskavim » 25 Jan 2015 18:24

@Squahman - I am a rookie in batch scripting.... so my apologies for any stupid mistakes :) ...!VAR! works thanks a lot!.

Post Reply