Echo writes trailing space in batch file - batch-file

With this batch file, I get a trailing space on every line in the output txt file:
#echo off
setlocal enabledelayedexpansion
for /f "delims==" %%A in (trimlist.txt) do set string=%%A & echo !string:,=.!>>trimlist-new.txt
How do I go about removing the trailing spaces? I want to avoid creating a new batch file to do so if possible.

The space between %%A and = is being included in your string. To avoid it, you can either have a multi-line for loop, or simply put quotes around your set statement.
#echo off
setlocal enabledelayedexpansion
for /f "delims==" %%A in (trimlist.txt) do set "string=%%A" & echo !string:,=.!>>trimlist-new.txt

Related

Batch script to extract part of a string

I need a batch script that will read in another batch script (batch2) and:
look for the string "configout:" OR "configin:"
If it encounters one of these two strings, extract what's after it until the string ".xml"
copy paste it in a new text file.
and do this for each line of batch2.
For exemple:
If this is my first line in the batch script
/configin:%faxml%fm_sellin_in.xml /configout:%faxml%transco_fm_sellin_out%col_transco%.xml /inputfile:
I should have this in my text file:
%faxml%fa_sellin_in.xml
%faxml%transco_fm_sellin_out%col_transco%.xml
I have seen a good code in Here:
for /f "tokens=1-2 delims=~" %%b in ("yourfile.txt") do (
echo %%b >> newfile.txt
echo removed %%a)
but i don't know how to adapt it to my specific case.
Why not replace all the /configin and /configout with newlines? -
(Replace string with a new line in Batch)
For example
setlocal EnableDelayedExpansion
set "str=/configin:%%faxml%%fm_sellin_in.xml /configout:%%faxml%%transco_fm_sellin_out%%col_transco%%.xml /inputfile:"
set str=!str:/configin^:=^
!
set str=!str:/configout^:=^
!
Now, !str! would contain
fm_sellin_in.xml
transco_fm_sellin_out.xml /inputfile:
Then, you could use the for loop to extract the strings
for /f "tokens=1,2 delims=. " %%a in ("!str!") do ()
this for loop iterates through each line and splits each line with the and . characters.
So %%a is your file name and %%b is the extension.
then
if [%%b]==[xml] (echo %%a.%%b>>mytextfile.txt)
We will do this for all the lines of batch2.
And the finished code is
setlocal EnableDelayedExpansion
for /f "delims=" %%c in (batch2.txt) do (
set str=%%c
set str=!str:/configin^:=^
!
set str=!str:/configout^:=^
!
for /f "tokens=1,2 delims=. " %%a in ("!str!") do (if [%%b]==[xml] (echo %%a.%%b>>mytextfile.txt))
)

Batch script -> find a sentence and replace it by a new one

I'm working on a DOS script in order to search a line and replace it into files from a specific folder. Here is what i have already done :
#echo off
setlocal enableextensions disabledelayedexpansion
set "search=#Interceptors({ RuntimeExceptionInterceptor.class }^)"
set "replace=#Interceptors({ RuntimeExceptionInterceptor.class, ReportInterceptor.class, CorrelationIdInterceptor }^)"
set "textFile=C:\Utilisateurs\a669884\Documents\test.txt"
for %%a in (*.txt) do (
echo "%%a"
for /f "Delims=" %%i in (%%a ^& break ^> %%a) do (
set "line=%%i "
setlocal enabledelayedexpansion
>>%%a echo(!line:%search%=%replace%!
endlocal
)
)
Problem is that my first line is not replace by the new one, the new line is added under it like that :
#Interceptors({ RuntimeExceptionInterceptor.class })
#Interceptors({ RuntimeExceptionInterceptor.class, ReportInterceptor.class, CorrelationIdInterceptor })
Do you know why my first line isn't replaced? Thanks a lot
Seb
Not sure what setting textfile has to do with the routine - it's not used.
Your original code produced a complaint that & was not found and created an empty file for me.
Changing the for to
for /f "Delims=" %%i in ('type "%%a" ^& break ^> "%%a"') do (
seemed to make the substitution, given the one line (partial?) of sourcefile you've provided, plus a few dummy lines.
execute the type... with the filenames enclosed in quotes to ensure correct operation with separators, replacing the file; substitute as specified.

Batch to delete the first line of a text file without creating a new file

Is there any solution to this question? I see a lot of questions about deleting first line of a text file but all of them need create a new text file. I need this because my text file is constantly updating with new lines (by a second batch file) so if the script creates a new text file it can accidently delete some new lines with the old text file.
Can i delete first line of a text file without creating a new one? If not, why?
Using only batch.
Blank lines shouldn't be preserved (If possible).
Especial characters like ! can be deleted.
IMO this question makes no sense. There is no way to avoid to preserve the new file contents in a place different than the original file; this is true even in any advanced programming language, that would require to read from second line to end of file and copy each line to the beginning of the file. However, at end of the process it would be necessary to truncate the file in order to eliminate the last bytes in the file (with the number of bytes that the first line had).
A Batch file certainly can not perform this type of process, so the lines of the file (from second one up to the end of file) must necessarily be stored in a place different than the original file. One of the answers store the lines in memory variables, but this method is inefficient specially if the file is large.
So, if the question is: "what tricks can be used in order to not use a new file to eliminate the first line in a data file?", then this is a more efficient method:
#echo off
for /F "skip=1 delims=" %%a in ('type input.txt ^& del input.txt') do >> input.txt echo %%a
If you want to preserve empty lines, use this method instead:
#echo off
for /F "skip=1 tokens=1* delims=:" %%a in ('findstr /N "^" input.txt ^& del input.txt') do >> input.txt echo(%%b
You should note that when the for /F command start execution it blocks the file for an exclusive access, so any attempt to modify the file while the for /F is reading it would be avoided with "access denied" error.
If you don't care about preserving blank lines, !, (, ), or ^, you can run the input file through a for loop, storing each line in a separate variable, then merging the variables with a newline character at the end of each one.
#echo off
setlocal enabledelayedexpansion
cls
set counter=0
for /f "delims=" %%A in (input.txt) do (
set line[!counter!]=%%A
set /a counter+=1
)
set /a counter-=2
set LF=^
for /L %%A in (1,1,!counter!) do set sheet=!sheet!!line[%%A]!!LF!
set /a counter+=1
for /L %%A in (!counter!,1,!counter!) do set sheet=!sheet!!line[%%A]!
echo !sheet!>input.txt
However, if you want to preserve blank lines and special characters, there are a few tricks you can throw in, but the overall idea is the same.
#echo off
setlocal enabledelayedexpansion
cls
:: findstr /n puts line numbers at the start of each line, which will allow us to preserve blank lines
for /f "tokens=1 delims=:" %%A in ('findstr /n "^" input.txt') do set line_counter=%%A
::set /p preserves special characters
<input.txt (
for /L %%A in (1,1,!line_counter!) do set /p line[%%A]=
)
set /a line_counter-=1
set LF=^
:: Do NOT delete the two blank lines above this line.
for /L %%A in (2,1,!line_counter!) do set sheet=!sheet!!line[%%A]!!LF!
set /a line_counter+=1
for /L %%A in (!line_counter!,1,!line_counter!) do set sheet=!sheet!!line[%%A]!
echo !sheet!>input.txt
In general a new file is needed because the requested operation requires that you READ and WRITE. So with only 1 file you would be modifying the file that you are reading! If you are simply concerned about the resultant file being a different file, that can be resolved by deleting the original and renaming the new file when done. The PUSHD and POPD are optional... use if applicable. ** Untested **
pushd "YourFolderName"
set "FileName=YourInFileName.txt"
set "TempFile=SomeTempFileName.txt"
if exist "%TempFile% del /f /q "%TempFile%"
more "%FileName%" +1 > "%TempFile%"
del /f /q "%FileName%"
ren "%TempFile%" "%FileName%"
popd

Trouble in Batch using quotes in a variable

Relatively new to batch scripts here and I have been searching everywhere for the answer only to not find anything.
Here is what I have for a batch script so far..
#echo off
set addtext="text to add includes spaces"
for /f "delims=" %%l in (file.txt) do (
echo %%l %addtext% >> tmpfile.txt
)
I'm looking to add a line of text to every line in the file but my problem comes in with the double quotes. I don't want the quotes to display with the text.
I only have the quotes there because there are spaces in the string of text that I'm looking to add to every line.
#echo off
setlocal enableextensions disabledelayedexpansion
set "addtext=text to add includes spaces"
for /f "delims=" %%l in (file.txt) do (
>> tmpfile.txt echo %%l %addtext%
)
This should work. Just not include the quotes in the value of the variable, but use them to wrap the assignment.
In cases where the string could contain more problematic characters, this is a safer version
#echo off
setlocal enableextensions disabledelayedexpansion
set "addtext=text to add includes spaces, > redirections & more problems !"
(for %%a in ("%addtext%") do for /f "delims=" %%l in (file.txt) do (
echo %%l %%~a
)) >> tmpfile.txt
Quotes are not included in the value, but wraps the assignation
To prevent problem accessing the variable, it is wrapped in quotes, stored in a for replaceable parameter (%%a) and when requested echoed without the quotes (%%~a)
Just to get better performance (should be used also in the first code) instead of open/write/close the output file for each line (redirection for each echo), redirection is handled for the full for command.

have a batch file read a text file from dropbox and execute it as variables in the batch file

I am trying to get a batch to file read a text file from dropbox and execute it as variables in the batch file.
this is what i am trying, but it does not work, please help!
SetLocal EnableDelayedExpansion
set content=
for /F "delims=" %%i in (DROPBOX-LINK-HERE) do set content=!
content! %%i
%content%
EndLocal
I'm not sure what you mean by DROPBOX-LINK-HERE, but I am using an ordinary text file for content.
You must separate each line with & or else enclose the content in parentheses and separate each line with <linefeed>. The linefeed solution is more complicated, but has fewer limitations on the content.
Any ! characters in the content will be corrupted during expansion of a FOR variable if delayed expansion is enabled. But delayed expansion is needed to preserve unquoted special characters. So delayed expansion needs to be creatively toggled on and off.
Here is code that I think does what you want.
#echo off
setlocal disableDelayedExpansion
::Define a carriage return variable
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
::Create a newline variable
set LF=^
::The above 2 blank lines are critical - do not remove
::Both CR and LF should be expanded using delayed expansion only.
::Load the content into a variable.
::We want to separate lines with linefeed, but FOR /F won't preserve linefeeds.
::So use carriage return as a place holder for now.
set "content=("
setlocal enableDelayedExpansion
for /f %%C in ("!CR! ") do (
endlocal
for /f "delims=" %%A in (test.txt) do (
setlocal enableDelayedExpansion
for /f "delims=" %%B in ("!content!") do (
endlocal
set "content=%%B%%C%%A"
)
)
)
::Now replace carriage returns with newline and append terminating )
setlocal enableDelayedExpansion
for /f %%C in ("!CR! ") do for %%N in ("!LF!") do set "content=!content:%%C=%%~N!%%~N)"
::Execute the content
endlocal&%content%
The code works, but there are limitations to the type of code that can be executed from a variable.
Variables cannot be expanded by using normal expansion unless you use CALL. For example, a line like echo %var% will not work, but call echo %var% will work. Another option is to use delayed expansion. SETLOCAL EnableDelayedExpansion and ENDLOCAL can be included in the content as needed.
You cannot CALL or GOTO a :LABEL within the content.
That's all I can remember at the moment, but there may be (probably are) other restrictions.
I have one question though:
If the content is already in a text file, then why not simply give the text file a .BAT extension and execute it?

Resources