Gave your idea a try thinking the same would hold true for an exclamation.
SET STR="%~1"
ECHO This is my parameter: !STR:~1,-1!.
But if your data has an exclamation in it, it will get rid of the exclamation in the data. The only way to echo an exclamation is to turn off expansion. But I can't do that because I have to use it to do other things in my batch file.
I have a batch file that combines all files into one file but at the same time it appends the original filename it came from to the end of each line.
This is the main portion of my batch file
@echo off & setlocal enableextensions enabledelayedexpansion
Set _Opsheet=appendlog.log
set _Tcount=0
FOR /F "tokens=*" %%A IN ('dir /b /a-d *.TXT') DO (
set _count=0
set _Tfile=%%~nA .
set _Filename=!_Tfile:~0,15!
echo "Working on file: %%~nA"
FOR /F "Delims=" %%B IN (%%A) DO (
set /a _count+=1
set _tmpstr="%%B"
echo !_tmpstr:~1,-1!!_Filename! >> FILES_COMBINED.tmp
)
echo.!_Filename! count is !_count! >>%_Opsheet%
set /a _Tcount=!_Tcount!+!_count!
)
echo.Input Count is !_Tcount! >>%_Opsheet%
Exclamation marks CAN also be output with enabled delayed expansion.
The problem in your case is not the output, it's the assignment!
set "STR=%~1"
First the %1 expands to ex. "Hello!"
But then the parser tries to expand the single exclamation mark, this results in removing of the exclamation mark.
So it is enough to enable the delayed expansion after the SET-statement.
SET "STR=%~1"
setlocal EnableDelayedExpansion
ECHO This is my parameter: !STR:~1,-1!.
Exclamation
SET STR="%~1"
ECHO This is my parameter: !STR:~1,-1!.
But if your data has an exclamation in it, it will get rid of the exclamation in the data. The only way to echo an exclamation is to turn off expansion. But I can't do that because I have to use it to do other things in my batch file.
I have a batch file that combines all files into one file but at the same time it appends the original filename it came from to the end of each line.
This is the main portion of my batch file
@echo off & setlocal enableextensions enabledelayedexpansion
Set _Opsheet=appendlog.log
set _Tcount=0
FOR /F "tokens=*" %%A IN ('dir /b /a-d *.TXT') DO (
set _count=0
set _Tfile=%%~nA .
set _Filename=!_Tfile:~0,15!
echo "Working on file: %%~nA"
FOR /F "Delims=" %%B IN (%%A) DO (
set /a _count+=1
set _tmpstr="%%B"
echo !_tmpstr:~1,-1!!_Filename! >> FILES_COMBINED.tmp
)
echo.!_Filename! count is !_count! >>%_Opsheet%
set /a _Tcount=!_Tcount!+!_count!
)
echo.Input Count is !_Tcount! >>%_Opsheet%
Re: Exclamation
ECHO.%_tmpstr:~1,-1%%_Filename% >> FILES_COMBINED.tmp
ENDLOCAL
Output of exclamation marks
The problem in your case is not the output, it's the assignment!
set "STR=%~1"
First the %1 expands to ex. "Hello!"
But then the parser tries to expand the single exclamation mark, this results in removing of the exclamation mark.
So it is enough to enable the delayed expansion after the SET-statement.
SET "STR=%~1"
setlocal EnableDelayedExpansion
ECHO This is my parameter: !STR:~1,-1!.