Siddhant Rimal
Siddhant Rimal

Reputation: 976

escaping " symbol used in findstr within a FOR statement

Yeah, I've tried the most popular available solutions(1)(2)

They didn't help much; just restated what I already knew.

This works:

@echo on
set var=APPENDTEXT
for /f "delims=" %%a in ('dir *.* /b /a-d') do ren "%%a" "%%~na%var%%%~xa"
pause

but then I try to refine it a bit like so

@echo on
set var=APPENDTEXT
for /f "delims=" %%a in ('dir *.* /b /a-d | findstr /v /i "\.bat$" ') do ren "%%a" "%%~na%var%%%~xa"
pause

so that I do not end up renaming the batch file itself. But then everything got messed up.

I've tried several approaches for escaping, none working quite like I want them to.

Additional Information:
From what I gather, escaping " inside findstr is a problem when it is itself inside something else. I've tried escaping with "" and with /" and with ^" to no avail.

Am I doing
something wrong in these approaches?

  1. ('dir . /b /a-d | findstr /v /i "".bat$"" ')
  2. ('dir . /b /a-d | findstr /v /i \".bat$\" ')
  3. ('dir . /b /a-d | findstr /v /i ^".bat$^" ')

What is the correct way to escape it? *

What I want it to do ?

Simply put,

When I run this.bat file inside a folder, I want all the files inside it to be renamed with a APPENDTEXT (except the bat file itself)

Example:
a.dat --> aAPPENDTEXT.dat
pleasework.txt --> pleaseworkAPPENDTEXT.txt

Upvotes: 0

Views: 212

Answers (1)

Sam Denty
Sam Denty

Reputation: 4085

You have escaped the findstr statement correctly, but the pipe | symbol still needs to be escaped.

| findstr   →    ^| findstr

@echo on
set var=APPENDTEXT
for /f "delims=" %%a in ('dir *.* /b /a-d ^| findstr /v /i "\.bat$" ') do ren "%%a" "%%~na%var%%%~xa"
pause

Upvotes: 2

Related Questions