findstr not working in for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

findstr not working in for loop

#1 Post by SIMMS7400 » 27 Nov 2016 16:54

Hi Folks -

How come the findstr piece of code does not work when inside the FOR loop?

Code: Select all

SET SUBVAR_OUTPUT=%intrapath%Subvar_Output.txt

FOR %%S IN (
CurQtr
FMVFXMth
NextMo
NextMo2
NextQtr
NextQtr2
PemCM
PemNM
PreMo
PreMo2
PreQtr
) DO (
for /f "tokens=4" %%a in ('findstr/r "^[\ \   ]*%%S" "%SUBVAR_OUTPUT%" ') do set "HYP_VAR=%%a"
ECHO %HYP_VAR%
PAUSE
)


If I take it out, it works as expected. However the reason I need it inside the for loop is I'm using all strings above it to search.

Thanks!

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

Re: findstr not working in for loop

#2 Post by Squashman » 27 Nov 2016 17:43

Are you debugging your script properly. Are there any errors? What is your expected output?

What are you doing with this character class. It makes no sense to have a slash, space, slash, space, space, space.

Code: Select all

[\ \   ]

You would only need a slash and a space.

Could we see an example of what the file looks like that you are searching.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: findstr not working in for loop

#3 Post by Compo » 27 Nov 2016 18:53

In addition to Squashman's comment you are also trying to ECHO %HYP_VAR% inside a FOR block without DelayedExpansion.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: findstr not working in for loop

#4 Post by SIMMS7400 » 28 Nov 2016 07:59

Hi Folks -

Thanks fo rhte replies. Silly my, I didn't use DelayedExpansions, therefore it wasn't being echoed properly with the conventional methods to call a variable.

Setting the appropriate headings and encasing the variable in ! symbols did the trick.

However, I'm seeing a new issues. When I search the file for the specified string and then spool results to another files in a format used for upload, it's repeating (2) variables multiple times. Any idea why?

Here is the file I'm searching:

Code: Select all

MAXL> login admin System11admin on DIPRDHYPESS03W;

 OK/INFO - 1051034 - Logging in user [admin@Native Directory].
 OK/INFO - 1241001 - Logged in to Essbase.

MAXL> display variable on database "DIFIN"."DIFIN";

 application         database            variable            value             
+-------------------+-------------------+-------------------+-------------------
 DIFIN               DIFIN               PtrRM               Jan               
 DIFIN               DIFIN               ThisYear            FY06               
 DIFIN               DIFIN               BudYr               FY16               
 DIFIN               DIFIN               CurYr               FY16               
 DIFIN               DIFIN               NexYr               FY17               
 DIFIN               DIFIN               NexYr2              FY18               
 DIFIN               DIFIN               NexYr3              FY19               
 DIFIN               DIFIN               NexYr4              FY20               
 DIFIN               DIFIN               NexYr5              FY21               
 DIFIN               DIFIN               PemBY               FY15               
 DIFIN               DIFIN               PemCY               FY16               
 DIFIN               DIFIN               PemNY               FY17               
 DIFIN               DIFIN               PreYr               FY15               
 DIFIN               DIFIN               PreYr2              FY14               
 DIFIN               DIFIN               PreYr3              FY13               
 DIFIN               DIFIN               PtrCY               FY16               
 DIFIN               DIFIN               PtrNY               FY17               
 DIFIN               DIFIN               PtrNY2              FY18               
 DIFIN               DIFIN               PtrNY3              FY19               
 DIFIN               DIFIN               PtrNY4              FY20               
 DIFIN               DIFIN               PtrNY5              FY21               
 DIFIN               DIFIN               PtrPY               FY15               
 DIFIN               DIFIN               NexQtr2             Q4                 
 DIFIN               DIFIN               NexQtr              Q4                 
 DIFIN               DIFIN               CurQtr              Q3                 
 DIFIN               DIFIN               FMVFXMth            Dec               
 DIFIN               DIFIN               PreQtr              Q2                 
 DIFIN               DIFIN               PtrCM               Sep               
 DIFIN               DIFIN               PtrNM2              Nov               
 DIFIN               DIFIN               PtrNM               Oct               
 DIFIN               DIFIN               CurMo               Oct               
 DIFIN               DIFIN               NexMo               Nov               
 DIFIN               DIFIN               NexMo2              Dec               
 DIFIN               DIFIN               PemCM               Oct               
 DIFIN               DIFIN               PemNM               Nov               
 DIFIN               DIFIN               PreMo               Sep               
 DIFIN               DIFIN               PreMo2              Aug               

 WARNING - 1241024 - Possible string truncation in column 3.
 WARNING - 1241028 - Output column defined with warnings.
 WARNING - 1241024 - Possible string truncation in column 4.
 WARNING - 1241028 - Output column defined with warnings.
 OK/INFO - 1241044 - Records returned: [37].

MAXL> logout;

      User admin is logged out



Here is my script:

Code: Select all

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Script Name: Update_Subvars.cmd                                       --::
::--                                                                       --::
::-- Description: This scripts advances and updates Essbase Subvars        --::
::--                                                                       --::
::--                                                                       --::
::-- Calls:                                                                --::
::-- Called By:                                                            --::
::--                                                                       --::
::-- Parameters: Call _env.cmd to get environment variables to determine   --::
::--             login info, database, application, etc.                   --::
::--                                                                       --::
::-- Author:                                                               --::
::-- Date:       11/28/16                                                  --::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::-- Set Working Directory as Script Path --::

cd /d %~dp0

::-- Call Environment File --::

CALL \\DIPRDHYPESS03W\F$\Hyperion_Batch\Scripts\Batch\_env.cmd

::-- Set Main Intrapath Variables --::
::-- Set Process in variables below i.e EPMA_Logs\

SET PLOGPATH=Update_Subvars_Logs\
SET PERRORPATH=Update_Subvars_Errors\

SET intrapath=%ESS_SERVER%\%MAINPATH_RMT%%LOGPATH%%PLOGPATH%
SET errorintrapath=%ESS_SERVER%\%MAINPATH_RMT%%ERRORPATH%%PERRORPATH%

IF NOT EXIST %intrapath% MKDIR %intrapath%
IF NOT EXIST %errorintrapath% MKDIR %errorintrapath%

::-- Prepare Main Log and Error Files --::

FOR /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set timestamp=%%a%%b)
FOR /f "tokens=* delims= " %%c in ("%timestamp%") do (set timestamp=%%c)

SET logfile=%intrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log
SET maxllogfile=%intrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0_MaxL.log
SET errorfile=%errorintrapath%%date:~-4,4%%date:~-10,2%%date:~-7,2%_%timestamp%_%~n0.log

::-- Prepare Log & Error file directories --::

FOR %%f IN ( %MAINPATH%%LOGPATH%%PLOGPATH%* )     DO ( DEL %%f )
FOR %%f IN ( %MAINPATH%%ERRORPATH%%PERRORPATH%* ) DO ( DEL %%f )

::-- Set Subvar Process Variables --::

SET FINDSTR=C:\Windows\System32\findstr.exe
SET SUBVAR_OUTPUT=%intrapath%Subvar_Output.txt

SET SUBVAR_PROC_PATH=%MAINPATH%%FILEPATH%Subvar_Maintenance\
FOR %%f IN ( %SUBVAR_PROC_PATH%* ) DO ( DEL %%f )

SET ARCHIVE_SUBVAR_PATH=%MAINPATH%%FILEPATH%Subvar_Maintenance\Updated_Subvars\%date:~-4,4%_%date:~-10,2%%date:~-7,2%\

echo ********************************************************>>%logfile%
echo %~n0 Starting at %TIME%                              >>%logfile%
echo ********************************************************>>%logfile%

echo ********************************************************>>%logfile%
echo Extract Valid Substition Variables for Month End        >>%logfile%
echo ********************************************************>>%logfile%

CALL %STARTESSMSHPATH%ESSMSH.EXE %ESS_SERVER%\%MAINPATH_RMT%%MAXLPATH%Extract_Subvarsnew.mxl %ESSB_USER% %ESSB_PSWD% %ESSB_SRVR% %ESSB_APP% %ESSB_DB% %SUBVAR_OUTPUT%
PAUSE

FOR %%S IN (

CurMo
CurQtr
FMVFXMth
NextMo
NextMo2
NextQtr
NextQtr2
PemCM
PemNM
PreMo
PreMo2
PreQtr
CurYr
NextYr
NextYr2
NextYr3
NextYr4
NextYr5
PemCY
PemNY
PreYr
PreYr2
PreYr3

) DO (

for /f "tokens=4" %%A in ('%FINDSTR% /C:%%S "%SUBVAR_OUTPUT%" ') DO SET "SN=%%S" & SET "SV=%%A"

ECHO %ESSB_APP%,%ESSB_DB%,!SN!,!SV! >> %SUBVAR_PROC_PATH%Updated_Subvars.txt
)
pause
SET myError1=%errorlevel%
IF %myError1%==0 goto Update_Subvars

echo **********************************************************************>>%logfile%
echo Encountered Error in Extract Valid Substition Variables for Month End >>%logfile%
echo **********************************************************************>>%logfile%

goto AbnormalExit

:Update_Subvars


And then here is the generated output with multiple variables being repeated:

Code: Select all

DIFIN,DIFIN,CurMo,Oct 
DIFIN,DIFIN,CurQtr,Q3
DIFIN,DIFIN,FMVFXMth,Dec
DIFIN,DIFIN,FMVFXMth,Dec
DIFIN,DIFIN,FMVFXMth,Dec
DIFIN,DIFIN,FMVFXMth,Dec
DIFIN,DIFIN,FMVFXMth,Dec
DIFIN,DIFIN,PemCM,Oct
DIFIN,DIFIN,PemNM,Nov
DIFIN,DIFIN,PreMo,Aug
DIFIN,DIFIN,PreMo2,Aug
DIFIN,DIFIN,PreQtr,Q2
DIFIN,DIFIN,CurYr,FY16
DIFIN,DIFIN,CurYr,FY16
DIFIN,DIFIN,CurYr,FY16
DIFIN,DIFIN,CurYr,FY16
DIFIN,DIFIN,CurYr,FY16
DIFIN,DIFIN,CurYr,FY16
DIFIN,DIFIN,PemCY,FY16
DIFIN,DIFIN,PemNY,FY17
DIFIN,DIFIN,PreYr,FY13
DIFIN,DIFIN,PreYr2,FY14
DIFIN,DIFIN,PreYr3,FY13


Thanks, guys!

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

Re: findstr not working in for loop

#5 Post by Squashman » 28 Nov 2016 08:13

Because your search strings will match multiple times because you have search strings that have the same characters.

NextYr wil match NextYr and NextYr2.


You are obfuscating your data incorrectly. Just so you know.
NexYr NextYr

Put all your search strings in a file and use the file as your search string with the FINDSTR command. Otherwise you are looping through your file X amount of times. Every instance in your FOR command it is then using findstr to search the entire file again.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: findstr not working in for loop

#6 Post by SIMMS7400 » 28 Nov 2016 08:47

Hi Squash -

Thank you for the clarification.

I adjusted my code as such but still getting the same issues:

Code: Select all

FOR /F %%S IN ( SUBVAR_LIST.txt ) DO (

for /f "tokens=4" %%A in ('findstr %%S "%SUBVAR_OUTPUT%"') DO SET "SN=%%S" & SET "SV=%%A"
ECHO %ESSB_APP%,%ESSB_DB%,!SN!,!SV! >> %SUBVAR_PROC_PATH%Updated_Subvars.txt
)

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

Re: findstr not working in for loop

#7 Post by Squashman » 28 Nov 2016 08:49

SIMMS7400 wrote:Hi Squash -

Thank you for the clarification.

I adjusted my code as such but still getting the same issues:


Read this comment from me again.

Squashman wrote:Put all your search strings in a file and use the file as your search string with the FINDSTR command.


Then open up a cmd prompt and type and read the help for the FINDSTR command.

Code: Select all

findstr /?

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: findstr not working in for loop

#8 Post by Compo » 28 Nov 2016 09:12

you could have just left it as it was and changed your search pattern to:

Code: Select all

for /f "tokens=4" %%A in ('find " %%S " ^<"%SUBVAR_OUTPUT%"') …

or perhaps used findstr with either "\<%%S\> " or "\ %%S\ " or even a literal search similar to the find example above, " %%S ".

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: findstr not working in for loop

#9 Post by SIMMS7400 » 28 Nov 2016 09:37

Hi Sqush -

Ah yes, got it!

For some reason, it's onlyreturning a value of "Aug" for all variables. Here is my code:

Code: Select all

echo ********************************************************>>%logfile%
echo Extract Valid Substition Variables for Month End        >>%logfile%
echo ********************************************************>>%logfile%

CALL %STARTESSMSHPATH%ESSMSH.EXE %ESS_SERVER%\%MAINPATH_RMT%%MAXLPATH%Extract_Subvarsnew.mxl %ESSB_USER% %ESSB_PSWD% %ESSB_SRVR% %ESSB_APP% %ESSB_DB% %SUBVAR_OUTPUT%

PUSHD %SUBVAR_LIST%
FOR /f %%S IN ( Subvar_List.txt ) DO (
for /f "tokens=4" %%A in ('%FINDSTR% /G:Subvar_List.txt "%SUBVAR_OUTPUT%"') DO SET "SN=%%S" & SET "SV=%%A"

ECHO %ESSB_APP%,%ESSB_DB%,!SN!,!SV! >> %SUBVAR_PROC_PATH%
)
POPD

SET myError1=%errorlevel%
IF %myError1%==0 goto Update_Subvars

echo **********************************************************************>>%logfile%
echo Encountered Error in Extract Valid Substition Variables for Month End >>%logfile%
echo **********************************************************************>>%logfile%

goto AbnormalExit

:Update_Subvars


Aug is the last line of the portion of the files that gets searched.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: findstr not working in for loop

#10 Post by Compo » 28 Nov 2016 10:05

You don't even need to set those variables:

Code: Select all

(FOR %%S IN (
   CurMo
   CurQtr
   FMVFXMth
   NextMo
   NextMo2
   NextQtr
   NextQtr2
   PemCM
   PemNM
   PreMo
   PreMo2
   PreQtr
   CurYr
   NextYr
   NextYr2
   NextYr3
   NextYr4
   NextYr5
   PemCY
   PemNY
   PreYr
   PreYr2
   PreYr3
) DO (FOR /F "TOKENS=4" %%A IN ('"%FINDSTR%" "\<%%S\>" "%SUBVAR_OUTPUT%"'
   ) DO ECHO=%ESSB_APP%,%ESSB_DB%,%%S,%%A))>>"%SUBVAR_PROC_PATH%"
Last edited by Compo on 28 Nov 2016 10:45, edited 1 time in total.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: findstr not working in for loop

#11 Post by SIMMS7400 » 28 Nov 2016 10:40

Thank you!

Unfortunately, that syntax is bombing on me and its not giving me any error codes. It's not spooling the file to %SUBVAR_PROC_PATH%:


Code: Select all

FOR %%S IN (
CurMo
CurQtr
FMVFXMth
NextMo
NextMo2
NextQtr
NextQtr2
PemCM
PemNM
PreMo
PreMo2
PreQtr
CurYr
NextYr
NextYr2
NextYr3
NextYr4
NextYr5
PemCY
PemNY
PreYr
PreYr2
PreYr3

) DO (
FOR /F "TOKENS=4" %%A IN ('"%FINDSTR%" "\<%%S\>" "%SUBVAR_OUTPUT%"') DO ECHO=%ESSB_APP%,%ESSB_DB%,%%S,%%A"))>>"%SUBVAR_PROC_PATH%"


I'll continue to debug.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: findstr not working in for loop

#12 Post by Compo » 28 Nov 2016 10:45

There was a typo, now fixed!

…also, please note that what you've posted above is not what I posted, you have changed it's format such that it cannot work!

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: findstr not working in for loop

#13 Post by SIMMS7400 » 28 Nov 2016 10:55

Hi Compo -

Thank you! Unfortunately, still same issue as before.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: findstr not working in for loop

#14 Post by SIMMS7400 » 28 Nov 2016 11:01

It says ) was unexpected at this time.

Code: Select all

FOR %%S IN (
   CurMo
   CurQtr
   FMVFXMth
   NextMo
   NextMo2
   NextQtr
   NextQtr2
   PemCM
   PemNM
   PreMo
   PreMo2
   PreQtr
   CurYr
   NextYr
   NextYr2
   NextYr3
   NextYr4
   NextYr5
   PemCY
   PemNY
   PreYr
   PreYr2
   PreYr3

) DO (FOR /F "TOKENS=4" %%A IN ('"%FINDSTR%" "\<%%S\>" "%SUBVAR_OUTPUT%"'   
   ) DO ECHO=%ESSB_APP%,%ESSB_DB%,%%S,%%A))>>"%SUBVAR_PROC_PATH%"


Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: findstr not working in for loop

#15 Post by Compo » 28 Nov 2016 11:05

…because, as I said in my last message, you have changed my code leave it alone and try it.

as a clue note the now missing first character

Post Reply