How to redirect string with variables as is and not as the literal value?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

How to redirect string with variables as is and not as the literal value?

#1 Post by SIMMS7400 » 04 Oct 2016 17:21

Hi -

I'm performing a find and replace on a file based on a variable value established earlier in the script.

Here is my script:

Code: Select all

setLocal enableDelayedExpansion

set filename="%CD%\_env_Setup.cmd"

set OT1=ABC
set OT2=DEF
set OT3=GHI

IF %FLG%==1 SET RT1=
IF %FLG%==1 SET RT2=SET MAINPATH=%DRIVE%Hyperion_Batch\
IF %FLG%==1 SET RT3=SET MAINFOLDER=%DRIVE%Hyperion_Batch\

IF %FLG%==2 SET RT1=for %%a in (%~f0\..\..\..\..\.) do SET APP=%%~nxa
IF %FLG%==2 SET RT2=SET MAINPATH=%DRIVE%Automation_Batch\%APP%\Hyperion_Batch\
IF %FLG%==2 SET RT3=SET MAINFOLDER=%DRIVE%Automation_Batch\%APP%\Hyperion_Batch\


for /f "tokens=*" %%a in ('type %filename%') do (
    set "line=%%a"
    if defined line (
        call set "line=%%line:%OT1%=%RT1%%%"
        call set "line=%%line:%OT2%=%RT2%%%"
      call set "line=%%line:%OT3%=%RT3%%%"
        echo !line!>>"%CD%\_env.cmd"
    ) else (
        echo.
    )
)
PAUSE


Let's say I'm spooling the contents where %FLG%==2

_env.cmd should look like such for the line specified above:

REM --------------------------------
REM SET PATH VARIABLES
REM --------------------------------

for %%a in (%~f0\..\..\..\..\.) do SET APP=%%~nxa
SET MAINPATH=%CD:~0,3%Automation_Batch\%APP%\Hyperion_Batch\
SET MAINFOLDER=%CD:~0,3%Automation_Batch\%APP%\Hyperion_Batch\

SET ARCHIVEPATH=Archives\
SET DATAPATH=Data\
SET ERRORPATH=Errors\
SET FILEPATH=Files\
SET EXPORTPATH=Exports\
SET IMPORTPATH=Imports\
SET BACKUPPATH=Backups\
SET LOCKPATH=Locks\
SET LOGPATH=Logs\
SET SCRIPTPATH=Scripts\NTScripts\
SET MAXLSCRIPTPATH=Scripts\MaxL\
SET UTILPATH=Scripts\Utilities\
SET SQLPATH=Scripts\SQL\


I do not want it to redirect what the variable value actually is. Just the strings AS IS.

Is that possible?

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

Re: How to redirect string with variables as is and not as the literal value?

#2 Post by Squashman » 04 Oct 2016 18:42

I am confused.
The flag variable is never set.
The line variable will always be defined so why are you bothering to check if it is?

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

Re: How to redirect string with variables as is and not as the literal value?

#3 Post by SIMMS7400 » 05 Oct 2016 01:41

Squash -

Here is my script. The purpose of this script is to to establish a POV folder structure for automation at net new client engagements. This 'POV' has been agreed on by multiple colleagues.

Depending on what folder structure option is established, the environment file (_env.cmd) needs to be dynamic to capture the MAINPATH variable value.

So I was hoping to redirect those strings to the _env.cmd on the fly.

Code: Select all

ECHO    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ECHO    \\                                     \\
ECHO    \\              Establish              \\
ECHO    \\        POV Folder Structure         \\
ECHO    \\                                     \\
ECHO    \\            EPM Practice             \\
ECHO    \\                                     \\
ECHO    \\      0        0  0      0    0      \\
ECHO    \\    \/!\/     (=!!=)   \/!\/\/!\/    \\
ECHO    \\     / \      /\  /\    / \  / \     \\
ECHO    \\                                     \\
ECHO    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
TIMEOUT /T 3 >NUL

CLS
ECHO ***************************************************************
ECHO Please specificy the drive where automation will be established
ECHO ***************************************************************
ECHO.
ECHO [1] C
ECHO [2] D
ECHO [3] E
ECHO [4] F
ECHO [5] G
ECHO [6] H
ECHO [7] I
ECHO [8] J
ECHO.
CHOICE /N /C 12345678 /M "Option: "
IF ERRORLEVEL 8 SET DRIVEL=J
IF ERRORLEVEL 7 SET DRIVEL=I
IF ERRORLEVEL 6 SET DRIVEL=H
IF ERRORLEVEL 5 SET DRIVEL=G
IF ERRORLEVEL 4 SET DRIVEL=F
IF ERRORLEVEL 3 SET DRIVEL=E
IF ERRORLEVEL 2 SET DRIVEL=D
IF ERRORLEVEL 1 SET DRIVEL=C

SET DRIVE=%DRIVEL%:\
SET MAINPATH=%DRIVE%Hyperion_Batch\

CLS
ECHO ***********************************************************
ECHO Will there be more than 1 application requiring automation?
ECHO ***********************************************************
ECHO.
ECHO [1] Yes
ECHO [2] No
ECHO.
CHOICE /N /C 12 /M "Option: "
IF ERRORLEVEL 2 GOTO 1

CLS
ECHO *****************************************
ECHO How many applications require automation?
ECHO *****************************************
ECHO.
CHOICE /N /C 12345678 /M "Option: "

IF ERRORLEVEL 8  SET APP_NUM=APP_1,APP_2,APP_3,APP_4,APP_5,APP_6,APP_7,APP_8 & SET AN=8
IF ERRORLEVEL 7  SET APP_NUM=APP_1,APP_2,APP_3,APP_4,APP_5,APP_6,APP_7 & SET AN=7
IF ERRORLEVEL 6  SET APP_NUM=APP_1,APP_2,APP_3,APP_4,APP_5,APP_6 & SET AN=6
IF ERRORLEVEL 5  SET APP_NUM=APP_1,APP_2,APP_3,APP_4,APP_5 & SET AN=5
IF ERRORLEVEL 4  SET APP_NUM=APP_1,APP_2,APP_3,APP_4 & SET AN=4
IF ERRORLEVEL 3  SET APP_NUM=APP_1,APP_2,APP_3 & SET AN=3
IF ERRORLEVEL 2  SET APP_NUM=APP_1,APP_2 & SET AN=2
IF ERRORLEVEL 1  SET APP_NUM=APP_1 & SET AN=1

CLS
ECHO ************************************************************************
ECHO Best practice is to dedicate an automation directory to each application
ECHO.
ECHO This segregates processes keeping directories clean and easily navigable
ECHO ------------------------------------------------------------------------
ECHO.
ECHO For instance, if you had 3 applications, the directories would like such^:
ECHO.
ECHO  C:\Automation_Batch\APP_1\Hyperion_Batch\etc...
ECHO  C:\Automation_Batch\APP_2\Hyperion_Batch\etc...
ECHO  C:\Automation_Batch\APP_2\Hyperion_Batch\etc...
ECHO.
ECHO Otherwise, all automation would reside in a single directory like such^:
ECHO.
ECHO  C:\Hyperion_Batch\etc...
ECHO.
ECHO ************************************************************************
ECHO.
ECHO [1] Establish a single automation directory
ECHO.
ECHO [2] Establish application specific directories
ECHO.
CHOICE /N /C 12 /M "Option: "
ECHO.
IF ERRORLEVEL 2 GOTO 2

CLS
:1
ECHO ************************************************
ECHO Establishing the POV Folder Structure...
ECHO ************************************************
SET "FLG=1"

IF NOT EXIST %MAINPATH%Archives MKDIR %MAINPATH%Archives
IF NOT EXIST %MAINPATH%Data MKDIR %MAINPATH%Data
IF NOT EXIST %MAINPATH%Errors MKDIR %MAINPATH%Errors
IF NOT EXIST %MAINPATH%Files\Imports MKDIR %MAINPATH%Files\Imports
IF NOT EXIST %MAINPATH%Files\Exports MKDIR %MAINPATH%Files\Exports
IF NOT EXIST %MAINPATH%Files\Backups MKDIR %MAINPATH%Files\Backups
IF NOT EXIST %MAINPATH%Locks MKDIR %MAINPATH%Locks
IF NOT EXIST %MAINPATH%Logs MKDIR %MAINPATH%Logs
IF NOT EXIST %MAINPATH%Scripts\MaxL MKDIR %MAINPATH%Scripts\MaxL
IF NOT EXIST %MAINPATH%Scripts\Utilities\PsTools MKDIR %MAINPATH%Scripts\Utilities\PsTools
IF NOT EXIST %MAINPATH%Scripts\SQL MKDIR %MAINPATH%Scripts\SQL
IF NOT EXIST %MAINPATH%Scripts\Batch MKDIR %MAINPATH%Scripts\Batch

GOTO FNDRPLC

:2
ECHO ************************************************
ECHO Establishing the POV Folder Structure...
ECHO ************************************************
SET MAINPATH=
SET MAINPATH=%DRIVE%Automation_Batch\
SET "FLG=2"

FOR /D %%f IN (%APP_NUM%) do (

   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Archives MKDIR %MAINPATH%%%f\Hyperion_Batch\Archives
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Data MKDIR %MAINPATH%%%f\Hyperion_Batch\Data
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Errors MKDIR %MAINPATH%%%f\Hyperion_Batch\Errors
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Files\Imports MKDIR %MAINPATH%%%f\Hyperion_Batch\Files\Imports
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Files\Exports MKDIR %MAINPATH%%%f\Hyperion_Batch\Files\Exports
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Files\Backups MKDIR %MAINPATH%%%f\Hyperion_Batch\Files\Backups
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Locks MKDIR %MAINPATH%%%f\Hyperion_Batch\Locks
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Logs MKDIR %MAINPATH%%%f\Hyperion_Batch\Logs
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Scripts\MaxL MKDIR %MAINPATH%%%f\Hyperion_Batch\Scripts\MaxL
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Scripts\Utilities\PsTools MKDIR %MAINPATH%%%f\Hyperion_Batch\Scripts\Utilities\PsTools
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Scripts\SQL MKDIR %MAINPATH%%%f\Hyperion_Batch\Scripts\SQL
   IF NOT EXIST %MAINPATH%%%f\Hyperion_Batch\Scripts\Batch MKDIR %MAINPATH%%%f\Hyperion_Batch\Scripts\Batch
   
)

CLS
color CF
ECHO ***************************************************************************
ECHO %AN% application specific automation directories have been established
ECHO.
ECHO The directories are as follows:
echo.
FOR /D %%f IN (%APP_NUM%) do ( echo %MAINPATH%%%f )
ECHO.
ECHO Change APP_1-%AN% names accordingly
ECHO ***************************************************************************
PAUSE
color 0f
CLS

:FNDRPLC

setLocal enableDelayedExpansion

set filename="%CD%\_env_Setup.cmd"

set OT1=ABC
set OT2=DEF
set OT3=GHI

IF %FLG%==1 SET RT1=
IF %FLG%==1 SET RT2=SET MAINPATH=%DRIVE%Hyperion_Batch\
IF %FLG%==1 SET RT3=SET MAINFOLDER=%DRIVE%Hyperion_Batch\

IF %FLG%==2 SET RT1=for %%a in (%~f0\..\..\..\..\.) do SET APP=%%~nxa
IF %FLG%==2 SET RT2=SET MAINPATH=%DRIVE%Automation_Batch\%APP%\Hyperion_Batch\
IF %FLG%==2 SET RT3=SET MAINFOLDER=%DRIVE%Automation_Batch\%APP%\Hyperion_Batch\


for /f "tokens=*" %%a in ('type %filename%') do (
    set "line=%%a"
    if defined line (
        call set "line=%%line:%OT1%=%RT1%%%"
        call set "line=%%line:%OT2%=%RT2%%%"
      call set "line=%%line:%OT3%=%RT3%%%"
        echo !line!>>"%CD%\_env.cmd"
    ) else (
        echo.
    )
)
PAUSE

IF %FLG%==2 GOTO C2

SET MAINPATH=%DRIVE%Hyperion_Batch\
ECHO F | XCOPY /F /Y _env.cmd %MAINPATH%Scripts\Batch\_env.cmd
ECHO F | XCOPY /F /Y SAMPLE.cmd %MAINPATH%Scripts\Batch\SAMPLE.cmd

DEL "%CD%\_env_Setup.cmd"
DEL "%CD%\_env.cmd"
DEL "%CD%\SAMPLE.cmd"
PAUSE

GOTO WARN

:C2
FOR /D %%f IN (%APP_NUM%) do (

   SET MAINPATH=%DRIVE%Automation_Batch\
   ECHO F | XCOPY /F /Y _env.cmd %MAINPATH%%%f\Hyperion_Batch\Scripts\Batch\_env.cmd
   ECHO F | XCOPY /F /Y SAMPLE.cmd %MAINPATH%%%f\Hyperion_Batch\Scripts\Batch\SAMPLE.cmd
   DEL "%CD%\_env_Setup.cmd"
   DEL "%CD%\_env.cmd"
   DEL "%CD%\SAMPLE.cmd"
   
)
PAUSE

:WARN
SET W=WARN.txt
IF EXIST "%W%" DEL "%W%"

ECHO *********************************************************************>>%W%
ECHO The POV folder structure has been established successfully!                                             >>%W%
ECHO *********************************************************************>>%W% 
ECHO.                                                                       
ECHO As of Spring 2016,  blah blah blah
ECHO Adding content here
ECHO Still in progress                                                     
ECHO.                                                      
ECHO Thank you,                                                
ECHO EPM Practice                                       
ECHO.                                                      
ECHO *********************************************************************  >>%W%
ECHO ***************** All Scripts Self-Destructing... *******************                     >>%W%
ECHO *********************************************************************  >>%W%
TIMEOUT /T 2>NUL

IF EXIST "%W%" start "" notepad.exe "%W%"

CLS
ECHO ******************************************************************
ECHO The Peloton POV folder structure has been established successfully
ECHO ******************************************************************
TIMEOUT /T 2 >NUL
CLS
ECHO    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ECHO    \\                                     \\
ECHO    \\                POV                  \\
ECHO    \\    Folder Structure Established     \\
ECHO    \\                                     \\
ECHO    \\            EPM Practice             \\
ECHO    \\                                     \\
ECHO    \\             0      0                \\
ECHO    \\           \/!\/  \/!\/              \\
ECHO    \\            / \    / \               \\
ECHO    \\                                     \\
ECHO    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
TIMEOUT /T 4 >NUL
DEL "%W%"
start /b "" cmd /c del "%~f0"&exit /b


The file that is read into the find and replace section, looks like this for the (3) lines I want to find & replace:

@ECHO OFF
REM ******************************************************************************************
REM
REM Name: env.cmd (Environment File)
REM
REM Description: This environment file is set to hold all variables for all Hyperion
REM scripts requiring user id, pwd, server, db, app, and path variables.
REM
REM Author:
REM Date:
REM
REM ******************************************************************************************

REM --------------------------------
REM SET PATH VARIABLES
REM --------------------------------

ABC
DEF
GHI

SET ARCHIVEPATH=Archives\
SET DATAPATH=Data\
SET ERRORPATH=Errors\
SET FILEPATH=Files\
SET EXPORTPATH=Exports\
SET IMPORTPATH=Imports\
SET BACKUPPATH=Backups\
SET LOCKPATH=Locks\
SET LOGPATH=Logs\
SET SCRIPTPATH=Scripts\NTScripts\
SET MAXLSCRIPTPATH=Scripts\MaxL\
SET UTILPATH=Scripts\Utilities\
SET SQLPATH=Scripts\SQL\


So, depending on which folder structure is set up, it replaces the above strings with the appropriate syntax to be able to capture the drive letter and directory's down to Hyperion_Batch as that is the value for the MAINPATH variable.

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: How to redirect string with variables as is and not as the literal value?

#4 Post by jeb » 05 Oct 2016 02:32

If I understand your question, then you have a problem that your replaces works, but it creates not the expected lines.

Currently you get for your original lines

Code: Select all

abc
def
ghi


This outputs (more or less)

Code: Select all

for %
SET MAINPATH=
SET MAINFOLDER=


But you want to see this
[code]for abc in (c:\Temp\test3\rep1.bat\..\..\..\..\.) do SET APP=abc
ET MAINPATH=Automation_Batch\\Hyperion_Batch\
ET MAINFOLDER=Automation_Batch\\Hyperion_Batch\[/code]

First you have to escape the percent signs by doubling them
[code]IF %FLG%==2 SET "RT1=for %%%%a in (%%~f0\..\..\..\..\.) do SET APP=%%%%~nxa"
IF %FLG%==2 SET "RT2=SET MAINPATH=%%DRIVE%%Automation_Batch\%%APP%%\Hyperion_Batch\"
IF %FLG%==2 SET "RT3=SET MAINFOLDER=%%DRIVE%%Automation_Batch\%%APP%%\Hyperion_Batch\"
[/code]

And you need to change your code for the replace itself.

[code]SET "RT1=for %%%%a in (%%~f0\..\..\..\..\.) do SET APP=%%%%~nxa"
SET "RT2=SET MAINPATH=%%DRIVE%%Automation_Batch\%%APP%%\Hyperion_Batch\"
SET "RT3=SET MAINFOLDER=%%DRIVE%%Automation_Batch\%%APP%%\Hyperion_Batch\"

for /F "delims=" %%1  in ("!RT1!") do (
for /F "delims=" %%2  in ("!RT2!") do (
for /F "delims=" %%3  in ("!RT3!") do (
    for /f "tokens=*" %%a in ('type a.txt') do (
        set "line=%%a"
        if defined line (
          set "line=!line:%OT1%=%%1!"
          set "line=!line:%OT2%=%%2!"
          set "line=!line:%OT3%=%%3!"
            echo(!line!
        ) else (
            echo(
        )
    )
)))


But in your case I would recommend to use better tools, like JRepl.bat

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

Re: How to redirect string with variables as is and not as the literal value?

#5 Post by SIMMS7400 » 05 Oct 2016 02:55

Jeb thank you!

I will test this out shortly.

Note: If %FLG%== 1 the string ABC should just be replaced by a blank line.

I will report back thanks!

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

Re: How to redirect string with variables as is and not as the literal value?

#6 Post by Compo » 05 Oct 2016 06:24

Why are you carrying a dummy script, _env_Setup.cmd then rewriting it during a find and replace as _env.cmd, then copying that to a new location and deleting both _env_Setup.cmd and _env.cmd leaving just that final copy?

Just write the one file directly from the running script to the \Scripts\Batch location with it's dynamically created paths!

Here's a quick rewrite, so you don't need the _env*cmd files.

Code: Select all

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION

ECHO=   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ECHO=   \\                                     \\
ECHO=   \\              Establish              \\
ECHO=   \\        POV Folder Structure         \\
ECHO=   \\                                     \\
ECHO=   \\            EPM Practice             \\
ECHO=   \\                                     \\
ECHO=   \\      0        0  0      0    0      \\
ECHO=   \\    \/!\/     (=!!=)   \/!\/\/!\/    \\
ECHO=   \\     / \      /\  /\    / \  / \     \\
ECHO=   \\                                     \\
ECHO=   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
TIMEOUT /T 3 /NOBREAK>NUL

CLS
ECHO=***************************************************************
ECHO=Please specificy the drive where automation will be established
ECHO=***************************************************************
ECHO=
SET "i=0"
FOR /F "DELIMS=:\ " %%A IN ('MOUNTVOL^|FIND ":\"') DO (SET/A i+=1
   CALL SET "OPT=%%OPT%%%%A"
   CALL SET "$%%i%%=%%A")
CHOICE /C "%OPT%" /M "Option: "
CALL SET "DRIVE=%%$%ERRORLEVEL%%%:"

CLS
ECHO=***********************************************************
ECHO=Will there be more than 1 application requiring automation?
ECHO=***********************************************************
ECHO=
CHOICE /M "Option: "
IF ERRORLEVEL 2 (SET "FLG=1"
   GOTO :1)

CLS
ECHO=*****************************************
ECHO=How many applications require automation?
ECHO=*****************************************
ECHO=
CHOICE /C 23456789 /M "Option: "
SET/A "AN=%ERRORLEVEL%+1"
FOR /L %%# IN (1,1,%AN%) DO CALL SET "APP_NUM=%%APP_NUM%% APP_%%#"

CLS
ECHO=************************************************************************
ECHO=Best practice is to dedicate an automation directory to each application
ECHO=
ECHO=This segregates processes keeping directories clean and easily navigable
ECHO=------------------------------------------------------------------------
ECHO=
ECHO=For instance, if you had 3 applications, the directories would look like:
ECHO=
ECHO= %DRIVE%\Automation_Batch\APP_1\Hyperion_Batch\etc...
ECHO= %DRIVE%\Automation_Batch\APP_2\Hyperion_Batch\etc...
ECHO= %DRIVE%\Automation_Batch\APP_3\Hyperion_Batch\etc...
ECHO=
ECHO=Otherwise, all automation would reside in a single directory like:
ECHO=
ECHO= %DRIVE%\Hyperion_Batch\etc...
ECHO=
ECHO=************************************************************************
ECHO=
ECHO=[1] Establish single directory automation
ECHO=
ECHO=[2] Establish multiple application specific directories
ECHO=
CHOICE /C 12 /M "Option: "
ECHO=
SET "FLG=%ERRORLEVEL%"

:1
CLS
ECHO=************************************************
ECHO=Establishing the POV Folder Structure...
ECHO=************************************************

IF %FLG% EQU 1 (CALL :CFS "%DRIVE%") ELSE (
   FOR %%# IN (%APP_NUM%) DO CALL :CFS "%DRIVE%\Automation_Batch\%%#")
IF %FLG% EQU 1 GOTO :WARN
COLOR CF

CLS
ECHO=***************************************************************************
ECHO=%AN% application specific automation directories have been established
ECHO=
ECHO=The directories are as follows:
ECHO=
FOR %%# IN (%APP_NUM%) DO ECHO=%DRIVE%\Automation_Batch\%%#\Hyperion_Batch
ECHO=
ECHO=Rename APP_1-%AN% according to your specific application names
ECHO ***************************************************************************
TIMEOUT -1
GOTO :WARN

:CFS
SET "MAINPATH=%~1\Hyperion_Batch"
IF NOT EXIST "%MAINPATH%\Archives\" MD "%MAINPATH%\Archives"
IF NOT EXIST "%MAINPATH%\Data\" MD "%MAINPATH%\Data"
IF NOT EXIST "%MAINPATH%\Errors\" MD "%MAINPATH%\Errors"
IF NOT EXIST "%MAINPATH%\Files\Imports\" MD "%MAINPATH%\Files\Imports"
IF NOT EXIST "%MAINPATH%\Files\Exports\" MD "%MAINPATH%\Files\Exports"
IF NOT EXIST "%MAINPATH%\Files\Backups\" MD "%MAINPATH%\Files\Backups"
IF NOT EXIST "%MAINPATH%\Locks\" MD "%MAINPATH%\Locks"
IF NOT EXIST "%MAINPATH%\Logs\" MD "%MAINPATH%\Logs"
IF NOT EXIST "%MAINPATH%\Scripts\MaxL\" MD "%MAINPATH%\Scripts\MaxL"
IF NOT EXIST "%MAINPATH%\Scripts\Utilities\PsTools\" MD "%MAINPATH%\Scripts\Utilities\PsTools"
IF NOT EXIST "%MAINPATH%\Scripts\SQL\" MD "%MAINPATH%\Scripts\SQL"
IF NOT EXIST "%MAINPATH%\Scripts\Batch\" MD "%MAINPATH%\Scripts\Batch"
>"%MAINPATH%\Scripts\Batch\_env.cmd" (
   ECHO=@ECHO OFF
   ECHO=REM ********************************************************************************
   ECHO=REM
   ECHO=REM Name: env.cmd (Environment File^)
   ECHO=REM
   ECHO=REM Description: This environment file is set to hold all variables for all Hyperion
   ECHO=REM scripts requiring user id, pwd, server, db, app, and path variables.
   ECHO=REM
   ECHO=REM Author:
   ECHO=REM Date:
   ECHO=REM
   ECHO=REM ********************************************************************************
   ECHO=
   ECHO=REM --------------------------------
   ECHO=REM SET PATH VARIABLES
   ECHO=REM --------------------------------)
IF %FLG% EQU 2 (
   >>"%MAINPATH%\Scripts\Batch\_env.cmd" (
      ECHO=FOR %%%%# IN (%%~dp0..\..\..^) DO (SET "APP=%%%%~nx#"
      ECHO=   SET "MAINPATH=%%%%~dpnx#"^))) ELSE (
      >>"%MAINPATH%\Scripts\Batch\_env.cmd" (ECHO=SET "MAINPATH=%MAINPATH%"))
>>"%MAINPATH%\Scripts\Batch\_env.cmd" (
   ECHO=SET "MAINFOLDER=%%MAINPATH%%\Hyperion_Batch"
   ECHO=SET "ARCHIVEPATH=%%MAINFOLDER%%\Archives"
   ECHO=SET "DATAPATH=%%MAINFOLDER%%\Data"
   ECHO=SET "ERRORPATH=%%MAINFOLDER%%\Errors"
   ECHO=SET "FILEPATH=%%MAINFOLDER%%\Files"
   ECHO=SET "EXPORTPATH=%%FILEPATH%%\Exports"
   ECHO=SET "IMPORTPATH=%%FILEPATH%%\Imports"
   ECHO=SET "BACKUPPATH=%%FILEPATH%%\Backups"
   ECHO=SET "LOCKPATH=%%MAINFOLDER%%\Locks"
   ECHO=SET "LOGPATH=%%MAINFOLDER%%\Logs"
   ECHO=SET "SCRIPTPATH=%%MAINFOLDER%%\Scripts\NTScripts"
   ECHO=SET "MAXLSCRIPTPATH=%%MAINFOLDER%%\Scripts\MaxL"
   ECHO=SET "UTILPATH=%%MAINFOLDER%%\Scripts\Utilities"
   ECHO=SET "SQLPATH=%%MAINFOLDER%%\Scripts\SQL")
COPY SAMPLE.cmd "%MAINPATH%\Scripts\Batch"
GOTO :EOF

:WARN
DEL "%CD%\SAMPLE.cmd"
SET "W=WARN.txt"
IF EXIST "%W%" DEL "%W%"
COLOR 0F

CLS
ECHO=*********************************************************************>>%W%
ECHO=The POV folder structure has been established successfully!          >>%W%
ECHO=*********************************************************************>>%W% 
ECHO=                                                                       
ECHO=As of Spring 2016,  blah blah blah
ECHO=Adding content here
ECHO=Still in progress                                                     
ECHO=                                                     
ECHO=Thank you,                                               
ECHO=EPM Practice                                       
ECHO=                                                     
ECHO=*********************************************************************>>%W%
ECHO=***************** All Scripts Self-Destructing... *******************>>%W%
ECHO=*********************************************************************>>%W%
TIMEOUT /T 2 /NOBREAK>NUL

START "" /W notepad.exe "%W%"
DEL "%W%"

CLS
ECHO ******************************************************************
ECHO The Peloton POV folder structure has been established successfully
ECHO ******************************************************************
TIMEOUT /T 2 /NOBREAK>NUL

CLS
ECHO=   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
ECHO=   \\                                     \\
ECHO=   \\                POV                  \\
ECHO=   \\    Folder Structure Established     \\
ECHO=   \\                                     \\
ECHO=   \\            EPM Practice             \\
ECHO=   \\                                     \\
ECHO=   \\             0      0                \\
ECHO=   \\           \/!\/  \/!\/              \\
ECHO=   \\            / \    / \               \\
ECHO=   \\                                     \\
ECHO=   \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
TIMEOUT /T 4 /NOBREAK>NUL

(GOTO) 2>NUL&DEL "%~f0"

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

Re: How to redirect string with variables as is and not as the literal value?

#7 Post by SIMMS7400 » 06 Oct 2016 00:35

Compo -

Beautiful! Works great! I added to your code to finish building my process. Neat clean and efficient. Much appreciated!

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

Re: How to redirect string with variables as is and not as the literal value?

#8 Post by SIMMS7400 » 09 Oct 2016 06:19

Hi -

I decided I want to build the SAMPLE.cmd file on the fly too in order to cut down on unnecessary "extra" files.

I'm having trouble redirecting the following contents to SAMPLE.cmd. I've tried to use the same function as well as new but it keeps failing:

Code: Select all

ECHO=@ECHO OFF
ECHO=:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO=::-- Script Name: SAMPLE.cmd
ECHO=::--
ECHO=::-- Description: This is a SAMPLE script
ECHO=::               
ECHO=::
ECHO=::-- Calls:       
ECHO=::-- Called By:
ECHO=::
ECHO=::-- Parameters:  Call _env.cmd to get environment variables to determine
ECHO=::                login info, database, application, etc.
ECHO=::       
ECHO=::-- Author:      Joe Shmo
ECHO=::-- Date:          10/9/16
ECHO=:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO=
ECHO=::-- Set Working Directory as Script Path --::
ECHO=
ECHO=cd /d %%~dp0
ECHO=
ECHO=::-- Call Environment File --::
ECHO=
ECHO=call _env.cmd
ECHO=
ECHO=::-- Set Main Intrapath Variables --::
ECHO=
ECHO=SET intrapath=%%MAINPATH%%%%LOGPATH%%
ECHO=SET errorintrapath=%%MAINPATH%%%%ERRORPATH%%
ECHO=
ECHO=::-- Prepare Main Log and Error Files  --::
ECHO=
ECHO=FOR /f "tokens=1-2 delims=/:" %%%%a in ("%%TIME%%") do (set timestamp=%%%%a%%%%b)
ECHO=FOR /f "tokens=* delims= " %%%%c in ("%%timestamp%%") do (set timestamp=%%%%c)
ECHO=
ECHO=SET logfile=%%intrapath%%%%date:~-4,4%%%%date:~-10,2%%%%date:~-7,2%%_%%timestamp%%_%%~n0.log
ECHO=SET errorfile=%%errorintrapath%%%%date:~-4,4%%%%date:~-10,2%%%%date:~-7,2%%_%%timestamp%%_%%~n0.log
ECHO=
ECHO=if exist %%logfile%% del %%logfile%%
ECHO=if exist %%errorfile%% del %%errorfile%%
ECHO=
ECHO=echo ********************************************************>>%%logfile%%
ECHO=echo %%~n0 Starting at %%TIME%%                                 >>%%logfile%%
ECHO=echo ********************************************************>>%%logfile%%
ECHO=
ECHO=echo ********************************************************>>%%logfile%%
ECHO=echo Execute Command Line                                    >>%%logfile%%
ECHO=echo ********************************************************>>%%logfile%%
ECHO=
ECHO=^<  COMMAND LINE GOES HERE ^>
ECHO=
ECHO=SET myError2=%%errorlevel%%
ECHO=IF %%myError2%%==0 goto NormalExit
ECHO=
ECHO=echo ********************************************************>>%%logfile%%
ECHO=echo Encountered Error in Execute Command Line               >>%%logfile%%
ECHO=echo ********************************************************>>%%logfile%%
ECHO=
ECHO=goto AbnormalExit
ECHO=
ECHO=:NormalExit
ECHO=echo ********************************************************>>%%logfile%%
ECHO=echo %%~n0 - Completed Successfully                           >>%%logfile%%
ECHO=echo ********************************************************>>%%logfile%%
ECHO=
ECHO=echo ********************************************************>>%%logfile%%
ECHO=echo Normal Exit - %%~nx0                                     >>%%logfile%%
ECHO=echo ********************************************************>>%%logfile%%
ECHO=date /t                                                      >>%%logfile%%
ECHO=time /t                                                      >>%%logfile%%
ECHO=
ECHO=::-- Archive Log Files Based on Date --::
ECHO=       
ECHO=FOR %%%%f IN (%%MAINPATH%%%%LOGPATH%%*) DO (
ECHO=IF NOT EXIST "%%MAINPATH%%%%LOGPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%" MKDIR "%%MAINPATH%%%%LOGPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%"
ECHO=MOVE %%%%f "%%MAINPATH%%%%LOGPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%"
ECHO=)
ECHO=
ECHO=EXIT /B 0
ECHO=
ECHO=:AbnormalExit
ECHO=echo ********************************************************>>%%errorfile%%
ECHO=echo  %%~n0 - Completed Unsuccessfully                        >>%%errorfile%%
ECHO=echo ********************************************************>>%%errorfile%%
ECHO=echo Please Check the log file for errors                    >>%%errorfile%%
ECHO=
ECHO=echo ********************************************************>>%%errorfile%%
ECHO=echo Abnormal Exit - %%~nx0                                   >>%%errorfile%%
ECHO=echo ********************************************************>>%%errorfile%%
ECHO=date /t                                                      >>%%errorfile%%
ECHO=time /t                                                      >>%%errorfile%%
ECHO=
ECHO=::-- Archive Error Files Based on Date --::
ECHO=
ECHO=FOR %%%%f IN (%%MAINPATH%%%%ERRORPATH%%*) DO (
ECHO=IF NOT EXIST "%%MAINPATH%%%%ERRORPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%" MKDIR "%%MAINPATH%%%%ERRORPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%"
ECHO=MOVE %%%%f "%%MAINPATH%%%%ERRORPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%"
ECHO=)
ECHO=
ECHO=EXIT 1


Any tricks to this?

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

Re: How to redirect string with variables as is and not as the literal value?

#9 Post by Compo » 09 Oct 2016 07:07

Fixed but not fully rewrote, just run it, look at the output and decide if there is anything you need to change. Hope it helps you.

Code: Select all

>SAMPLE.cmd (
   ECHO=@ECHO OFF
   ECHO=:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   ECHO=::-- Script Name: SAMPLE.cmd                                                 ::
   ECHO=::--                                                                         ::
   ECHO=::-- Description: This is a SAMPLE script                                    ::
   ECHO=::                                                                           ::
   ECHO=::                                                                           ::
   ECHO=::-- Calls:                                                                  ::
   ECHO=::-- Called By:                                                              ::
   ECHO=::                                                                           ::
   ECHO=::-- Parameters: Call _env.cmd to get environment variables to determine     ::
   ECHO=::   login info, database, application, etc.                                 ::
   ECHO=::                                                                           ::
   ECHO=::-- Author: Joe Shmo                                                        ::
   ECHO=::-- Date: 10/9/16                                                           ::
   ECHO=:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   ECHO=
   ECHO=::-- Set Working Directory as Script Path --::
   ECHO=
   ECHO=CD /D "%%~dp0"
   ECHO=
   ECHO=::-- Call Environment File --::
   ECHO=
   ECHO=CALL _env.cmd
   ECHO=
   ECHO=::-- Set Main Intrapath Variables --::
   ECHO=
   ECHO=SET "intrapath=%%MAINPATH%%%%LOGPATH%%"
   ECHO=SET "errorintrapath=%%MAINPATH%%%%ERRORPATH%%"
   ECHO=
   ECHO=::-- Prepare Main Log and Error Files --::
   ECHO=
   ECHO=SET "mydate=%%DATE:~-4,4%%_%%DATE:~-10,2%%%%DATE:~-7,2%%"
   ECHO=FOR /F "TOKENS=1-2 DELIMS=/:" %%%%A IN ("%%TIME%%"^) DO SET "timestamp=%%%%A%%%%B"
   ECHO=::FOR /F "TOKENS=* DELIMS= " %%%%C IN ("%%timestamp%%"^) DO SET "timestamp=%%%%C"
   ECHO=
   ECHO=SET "logfile=%%intrapath%%%%mydate:_=%%_%%timestamp%%_%%~n0.log"
   ECHO=SET "errorfile=%%errorintrapath%%%%mydate:_=%%_%%timestamp%%_%%~n0.log"
   ECHO=
   ECHO=IF EXIST "%%logfile%%" DEL "%%logfile%%"
   ECHO=IF EXIST "%%errorfile%%" DEL "%%errorfile%%"
   ECHO=
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=ECHO=%%~n0 Starting at %%TIME%%^>^>"%%logfile%%"
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=ECHO=^>^>"%%logfile%%"
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=ECHO=Execute Command Line^>^>"%%logfile%%"
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=
   ECHO=:: ^< COMMAND LINE GOES HERE ^>
   ECHO=
   ECHO=SET "myError2=%%ERRORLEVEL%%"
   ECHO=IF "%%myError2%%"=="0" GOTO :NormalExit
   ECHO=
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=ECHO=Encountered Error in Execute Command Line^>^>"%%logfile%%"
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=
   ECHO=GOTO :AbnormalExit
   ECHO=
   ECHO=:NormalExit
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=ECHO=%%~n0 - Completed Successfully^>^>"%%logfile%%"
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=ECHO=^>^>"%%logfile%%"
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=ECHO=Normal Exit - %%~nx0^>^>"%%logfile%%"
   ECHO=ECHO=*********************************************************^>^>"%%logfile%%"
   ECHO=DATE /T^>^>"%%logfile%%"
   ECHO=TIME /T^>^>"%%logfile%%"
   ECHO=
   ECHO=::-- Archive Log Files Based on Date --::
   ECHO=
   ECHO=FOR %%%%A IN (%%MAINPATH%%%%LOGPATH%%*^) DO (
   ECHO=   IF NOT EXIST "%%MAINPATH%%%%LOGPATH%%%%mydate%%\" (
   ECHO=      MD "%%MAINPATH%%%%LOGPATH%%%%mydate%%"
   ECHO=   ^)
   ECHO=   MOVE "%%%%A" "%%MAINPATH%%%%LOGPATH%%%%mydate%%"
   ECHO=^)
   ECHO=
   ECHO=EXIT /B 0
   ECHO=
   ECHO=:AbnormalExit
   ECHO=ECHO=********************************************************^>^>"%%errorfile%%"
   ECHO=ECHO=%%~n0 - Completed Unsuccessfully^>^>"%%errorfile%%"
   ECHO=ECHO=********************************************************^>^>"%%errorfile%%"
   ECHO=ECHO=Please Check the log file for errors^>^>"%%errorfile%%"
   ECHO=
   ECHO=ECHO=********************************************************^>^>"%%errorfile%%"
   ECHO=ECHO=Abnormal Exit - %%~nx0^>^>"%%errorfile%%"
   ECHO=ECHO=********************************************************^>^>"%%errorfile%%"
   ECHO=DATE /T^>^>"%%errorfile%%"
   ECHO=TIME /T^>^>"%%errorfile%%"
   ECHO=
   ECHO=::-- Archive Error Files Based on Date --::
   ECHO=
   ECHO=FOR %%%%A IN (%%MAINPATH%%%%ERRORPATH%%*^) DO (
   ECHO=   IF NOT EXIST "%%MAINPATH%%%%ERRORPATH%%%%mydate%%\" (
   ECHO=      MD "%%MAINPATH%%%%ERRORPATH%%%%mydate%%"
   ECHO=   ^)
   ECHO=   MOVE "%%%%A" "%%MAINPATH%%%%ERRORPATH%%%%mydate%%"
   ECHO=^)
   ECHO=
   ECHO=EXIT /B 1
)

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

Re: How to redirect string with variables as is and not as the literal value?

#10 Post by SIMMS7400 » 10 Oct 2016 18:22

Works great, Compo! You're the man! Thank you!

Post Reply