I have a bunch of scripts I'd like to escape all special characters as I'd like to essentially add them to a main script so that based on certain checks, redirect the various scripts to certain directories.
Does anyone have a script that run over *.cmds to escape the necessary characters so they can be redirect successfully? I was hoping there was a quicker way than manually.
For example, turn this script:
Code: Select all
@ECHO OFF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Script Name: SAMPLE.cmd --::
::-- --::
::-- Description: This is a Sample Batch Script --::
::-- --::
::-- --::
::-- Calls: --::
::-- Called By: --::
::-- --::
::-- Parameters: Call _env.cmd to get environment variables to determine --::
::-- login info, database, application, etc. --::
::-- --::
::-- Author: --::
::-- Date: --::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::-- Set Working Directory as Script Path --::
cd /d %~dp0
::-- Call Environment File --::
call _env.cmd
::-- Set Main Intrapath Variables --::
::-- Set Process in variables below i.e EPMA_Logs\
SET PLOGPATH=
SET PERRORPATH=
SET intrapath=%MAINPATH%%LOGPATH%%PLOGPATH%
SET errorintrapath=%MAINPATH%%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 )
echo ********************************************************>>%logfile%
echo %~n0 Starting at %TIME% >>%logfile%
echo ********************************************************>>%logfile%
echo ********************************************************>>%logfile%
echo Execute Command Line >>%logfile%
echo ********************************************************>>%logfile%
:: < COMMAND LINE GOES HERE >
SET myError2=%errorlevel%
IF %myError2%==0 goto NormalExit
echo ********************************************************>>%logfile%
echo Encountered Error in Execute Command Line >>%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
Into:
Code: Select all
ECHO=@ECHO OFF
ECHO=:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ECHO=::-- Script Name: SAMPLE.cmd --::
ECHO=::-- --::
ECHO=::-- Description: This is a Sample Batch 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: --::
ECHO=::-- Date: --::
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=::-- Set Process in variables below i.e EPMA_Logs\
ECHO=
ECHO=SET PLOGPATH=
ECHO=SET PERRORPATH=
ECHO=
ECHO=SET intrapath=%%MAINPATH%%%%LOGPATH%%%%PLOGPATH%%
ECHO=SET errorintrapath=%%MAINPATH%%%%ERRORPATH%%%%PERRORPATH%%
ECHO=
ECHO=IF NOT EXIST %%intrapath%% MKDIR %%intrapath%%
ECHO=IF NOT EXIST %%errorintrapath%% MKDIR %%errorintrapath%%
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=::-- Prepare Log ^& Error file directories --::
ECHO=
ECHO=FOR %%%%f IN ^( %%MAINPATH%%%%LOGPATH%%%%PLOGPATH%%* ^) DO ^( DEL %%%%f ^)
ECHO=FOR %%%%f IN ^( %%MAINPATH%%%%ERRORPATH%%%%PERRORPATH%%* ^) DO ^( DEL %%%%f ^)
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=CALL :ARCHIVE
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=
ECHO=CALL :ARCHIVE
ECHO=EXIT /B 1
ECHO=
ECHO=:ARCHIVE
ECHO=
ECHO=::-- Archive Log Files Based on Date --::
ECHO=
ECHO=FOR %%%%f IN ^(%%MAINPATH%%%%LOGPATH%%%%PLOGPATH%%*^) DO ^(
ECHO= IF NOT EXIST "%%MAINPATH%%%%LOGPATH%%%%PLOGPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%\" ^(
ECHO= MKDIR "%%MAINPATH%%%%LOGPATH%%%%PLOGPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%"
ECHO= ^)
ECHO= MOVE "%%%%f" "%%MAINPATH%%%%LOGPATH%%%%PLOGPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%"
ECHO=^)
ECHO=
ECHO=::-- Archive Error Files Based on Date --::
ECHO=
ECHO=FOR %%%%f IN ^(%%MAINPATH%%%%ERRORPATH%%%%PERRORPATH%%*^) DO ^(
ECHO= IF NOT EXIST "%%MAINPATH%%%%ERRORPATH%%%%PERRORPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%\" ^(
ECHO= MKDIR "%%MAINPATH%%%%ERRORPATH%%%%PERRORPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%"
ECHO= ^)
ECHO= MOVE "%%%%f" "%%MAINPATH%%%%ERRORPATH%%%%PERRORPATH%%%%date:~-4,4%%_%%date:~-10,2%%%%date:~-7,2%%"
ECHO=^)
ECHO=
ECHO=GOTO :EOF
Thanks!