user4204014
user4204014

Reputation:

Prepend and append text to every line of a file in Windows batch

I've looked at numerous amounts of questions, all suggesting the same code and I have no idea why it doesn't work for me. I'm trying to prepend and append text to every line of a .txt file, using a .bat command. My code currently is

@echo off
for /f "tokens=* delims= " %%a in (patchList.txt) do (
echo copy %%a>>output.txt
)

But upon running it nothing happens at all. Even if I try to output to the screen by getting rid of the output parameter, nothing happens. Any suggestions?

Upvotes: 0

Views: 1238

Answers (1)

JosefZ
JosefZ

Reputation: 30103

Your patchList.txt is created/saved in Unicode encoding.

If type patchList.txt gives readable output, then the file contains Byte Order Mark and you could adjust your script as follows:

@echo off
for /f "tokens=*" %%a in ('type patchList.txt') do (
    >>output.txt echo copy %%a
)

If type patchList.txt gives weird output with text interspaced by blanks or white squares or other replacement characters then the file does not contain BOM; then Powershell could help:

@echo off
for /f "usebackq tokens=*" %%a in (`
        powershell -Command Get-Content '%CD%\patchList.txt' -Encoding Unicode
                                 `) do (
    >>output.txt echo copy %%a
)

Or, better, switch to powershell at all!
If powershell is not acceptable for some reason then we could utilize find command which can read Unicode files: try

< patchList.txt find "."

Please note that "." (full stop) above is not a regex-like pattern nor a wildcard so there might be difficult to determine all-lines matching character.

Upvotes: 2

Related Questions