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

Re: findstr not working in for loop

#16 Post by SIMMS7400 » 28 Nov 2016 11:08

If I legit copy and paste it in my script it pastes in like such:

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%"



Then, it claims it can't find the file specified.

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

Re: findstr not working in for loop

#17 Post by Compo » 28 Nov 2016 11:31

On my posted code press [Select all] then [Ctrl][C] then open notepad then [Ctrl][P]

BTW, I've noticed that your code is different in different posts you'll have to decide if the code should end with "%SUBVAR_PROC_PATH%" or "%SUBVAR_PROC_PATH%Updated_Subvars.txt"
Last edited by Compo on 28 Nov 2016 11:43, edited 2 times in total.

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

Re: findstr not working in for loop

#18 Post by SIMMS7400 » 28 Nov 2016 11:38

Hi Compo -

Same issue, it says it cannot find the file specified.

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

Re: findstr not working in for loop

#19 Post by Compo » 28 Nov 2016 11:40

see above

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

Re: findstr not working in for loop

#20 Post by SIMMS7400 » 28 Nov 2016 11:42

Here's my script, still saying cannot find the file specified.

Code: Select all

::-- 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\Update_Subvars.txt
IF EXIST %SUBVAR_PROC_PATH% DEL %SUBVAR_PROC_PATH%

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%


(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%"

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

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

Re: findstr not working in for loop

#21 Post by Compo » 28 Nov 2016 11:48

Check my last messages, the post directly above my answer used %SUBVAR_PROC_PATH%, the other ones have used %SUBVAR_PROC_PATH%Updated_Subvars.txt.

My guess is that your post from which I answered the question was incorrect, so try changing the last string in my answer to "%SUBVAR_PROC_PATH%Updated_Subvars.txt"

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

Re: findstr not working in for loop

#22 Post by SIMMS7400 » 28 Nov 2016 12:02

Hi Compo -

Agreed, but that I then provided the name of the file in the variable set above everything. So it should have mattered.

Either way, I added what you said to your answer with the same results.

Cannot find the file specified.

Here is the latets code:

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%Updated_Subvars.txt"
PAUSE


No luck.

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

Re: findstr not working in for loop

#23 Post by Compo » 28 Nov 2016 12:36

The only way we can debug is to have every single referenced file and the entire code, not just snippets.

If I was to take all of your posts on all of your topics on this particular project, I still wouldn't have everything needed.

Much of what you've posted doesn't make sense to me because the code is either poor, long winded or unnecessary.

for example, what is this for?

Code: Select all

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

what's the idea behind this?

Code: Select all

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

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

Re: findstr not working in for loop

#24 Post by SIMMS7400 » 28 Nov 2016 12:46

Here is my entire script:

Code: Select all

::-- 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 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\Update_Subvars.txt
IF EXIST %SUBVAR_PROC_PATH% DEL %SUBVAR_PROC_PATH%

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

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

::-- Extract subvars from Essbase --::

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


::-- Find Subvars extracted above, and return value and spool to file in import format --::

(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%Updated_Subvars.txt"

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

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

goto AbnormalExit

:NormalExit
echo ********************************************************>>%logfile%
echo %~n0 - Completed Successfully                          >>%logfile%
echo ********************************************************>>%logfile%

echo ********************************************************>>%logfile%
echo Normal Exit - %~nx0                                    >>%logfile%
echo ********************************************************>>%logfile%
date /t                                                      >>%logfile%
time /t                                                      >>%logfile%

CALL :ARCHIVE
EXIT /B 0

:AbnormalExit
echo ********************************************************>>%errorfile%
echo %~n0 - Completed Unsuccessfully                        >>%errorfile%
echo ********************************************************>>%errorfile%
echo Please Check the log file for errors                    >>%errorfile%

echo ********************************************************>>%errorfile%
echo Abnormal Exit - %~nx0                                  >>%errorfile%
echo ********************************************************>>%errorfile%
date /t >>%errorfile%
time /t >>%errorfile%


CALL :ARCHIVE
EXIT /B 1

:ARCHIVE

::-- Archive Log Files Based on Date --::
 
FOR %%f IN (%MAINPATH%%LOGPATH%%PLOGPATH%*) DO (
   IF NOT EXIST "%MAINPATH%%LOGPATH%%PLOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\" (
      MKDIR "%MAINPATH%%LOGPATH%%PLOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
   )
   MOVE "%%f" "%MAINPATH%%LOGPATH%%PLOGPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
)   
 
::-- Archive Error Files Based on Date --::

FOR %%f IN (%MAINPATH%%ERRORPATH%%PERRORPATH%*) DO (
   IF NOT EXIST "%MAINPATH%%ERRORPATH%%PERRORPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%\" (
      MKDIR "%MAINPATH%%ERRORPATH%%PERRORPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
   )
   MOVE "%%f" "%MAINPATH%%ERRORPATH%%PERRORPATH%%date:~-4,4%_%date:~-10,2%%date:~-7,2%"
)

GOTO :EOF


Here is what the Subvar_Output.txt file looks like:

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



Since I archive the log and error files at the end, My intention is to make sure tose two directories are clean prior to executing again. I will also be adding another error catching portion above your code you provided.

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

Re: findstr not working in for loop

#25 Post by SIMMS7400 » 28 Nov 2016 18:28

Hi Folks -

This syntax is working as it should:

Code: Select all

FOR %%S IN (
   CurMo
   CurQtr
   FMVFXMth
   NexMo
   NexMo2
   NexQtr
   NexQtr2
   PemCM
   PemNM
   PreMo
   PreMo2
   PreQtr
   CurYr
   NexYr
   NexYr2
   NexYr3
   NexYr4
   NexYr5
   PemCY
   PemNY
   PreYr
   PreYr2
   PreYr3
) DO (

FOR /F "tokens=4" %%A IN ('find " %%S " ^<"%SUBVAR_OUTPUT%"') DO ECHO=%ESSB_APP%,%ESSB_DB%,%%S,%%A>> %SUBVAR_PROC_PATH%

)


Thank you!

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

Re: findstr not working in for loop

#26 Post by Squashman » 29 Nov 2016 07:46

Still do not understand why you are not doing this with a single FOR command. You are essentially matching the the third token and then outputting the 3rd and 4th token.

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

Re: findstr not working in for loop

#27 Post by SIMMS7400 » 29 Nov 2016 09:42

Hi Squash -

I tried that, using the /G: switch as you mentioned. It didn't quite work as expected as it didn't return the correct value. It would always return the last value in the file, regardless of the search string.

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

Re: findstr not working in for loop

#28 Post by Squashman » 29 Nov 2016 10:02

subvar_output.txt

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

search.txt

Code: Select all

CurMo
CurQtr
FMVFXMth
NexMo
NexMo2
NexQtr
NexQtr2
PemCM
PemNM
PreMo
PreMo2
PreQtr
CurYr
NexYr
NexYr2
NexYr3
NexYr4
NexYr5
PemCY
PemNY
PreYr
PreYr2
PreYr3

foo.bat

Code: Select all

@echo off
FOR /F "tokens=3,4 delims= " %%G IN ('findstr /G:search.txt subvar_output.txt') DO ECHO %%G,%%H
pause

Output

Code: Select all

CurYr,FY16
NexYr,FY17
NexYr2,FY18
NexYr3,FY19
NexYr4,FY20
NexYr5,FY21
PemCY,FY16
PemNY,FY17
PreYr,FY15
PreYr2,FY14
PreYr3,FY13
NexQtr2,Q4
NexQtr,Q4
CurQtr,Q3
FMVFXMth,Dec
PreQtr,Q2
CurMo,Oct
NexMo,Nov
NexMo2,Dec
PemCM,Oct
PemNM,Nov
PreMo,Sep
PreMo2,Aug
Press any key to continue . . .

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

Re: findstr not working in for loop

#29 Post by SIMMS7400 » 01 Dec 2016 15:17

Thank you, Squash!

What is the advantage of listing them in a file vs the set statement?

Post Reply