Page 1 of 1

Redirect to file escaping all special characters?

Posted: 25 Feb 2017 18:27
by SIMMS7400
Hi Folks -

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!

Re: Redirect to file escaping all special characters?

Posted: 25 Feb 2017 19:16
by aGerman
The resulting command lines will work for execution in an environment where delayed expansion is disabled.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
for %%i in (*.bat *.cmd) do (
  set "batfile=%%~fi"
  set "escaped=%%~dpni_escaped.txt"
  call :escape
)
pause
exit /b

:escape
setlocal EnableDelayedExpansion
<"!batfile!" >"!escaped!" (
  for /f %%i in ('type "!batfile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "ln=" &set /p "ln="
    if not defined ln (
      echo echo(
    ) else (
      set "ln=!ln:^=^^!"
      set "ln=!ln:<=^<!"
      set "ln=!ln:>=^>!"
      set "ln=!ln:|=^|!"
      set "ln=!ln:&=^&!"
      set "ln=!ln:)=^)!"
      echo echo(!ln:%%=%%%%!
    )
  )
)
endlocal
exit /b


Steffen

Re: Redirect to file escaping all special characters?

Posted: 26 Feb 2017 06:13
by SIMMS7400
Thank you so much! This works perfectly!

Re: Redirect to file escaping all special characters?

Posted: 25 Mar 2017 16:01
by RedEyedRocker
aGerman wrote:The resulting command lines will work for execution in an environment where delayed expansion is disabled.

Code: Select all

@echo off &setlocal DisableDelayedExpansion
for %%i in (*.bat *.cmd) do (
  set "batfile=%%~fi"
  set "escaped=%%~dpni_escaped.txt"
  call :escape
)
pause
exit /b

:escape
setlocal EnableDelayedExpansion
<"!batfile!" >"!escaped!" (
  for /f %%i in ('type "!batfile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
    set "ln=" &set /p "ln="
    if not defined ln (
      echo echo(
    ) else (
      set "ln=!ln:^=^^!"
      set "ln=!ln:<=^<!"
      set "ln=!ln:>=^>!"
      set "ln=!ln:|=^|!"
      set "ln=!ln:&=^&!"
      set "ln=!ln:)=^)!"
      echo echo(!ln:%%=%%%%!
    )
  )
)
endlocal
exit /b


Steffen


THANKS SO MUCH! THIS JUST MADE MY LIFE EASIER!!! YOU'RE AWESOME AND PROFESSIONAL!!!
KEEP UP THE GREAT WORK! AT LEAST THIS IS BETTER AND USEFUL THAN WHAT TRUMP DOES XD

Re: Redirect to file escaping all special characters?

Posted: 26 Mar 2017 07:09
by SIMMS7400
Chill, RER.

Re: Redirect to file escaping all special characters?

Posted: 26 Mar 2017 11:26
by RedEyedRocker
SIMMS7400 wrote:Chill, RER.


Do you think I'm pissed off to tell to me to calm down? That was typed in full caps because I was very glad and it was really helpful for me.