0

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

Community
  • 1
  • 1
Siddhant Rimal
  • 975
  • 2
  • 11
  • 32
  • 2
    You need to escape the PIPE. – Squashman Feb 13 '17 at 17:08
  • 4
    You could also get rid of the findstr and just do a comparison to the file extension before the rename. `for /f "delims=" %%a in ('dir *.* /b /a-d') do IF /I NOT "%%~xa"==".bat" ren "%%a" "%%~na%var%%%~xa"` – Squashman Feb 13 '17 at 17:20
  • 1
    You could also get rid of the comparision by locking the batch file `<"%~f0" 2>nul (for %%a in (*) do ren "%%a" "%%~na%var%%%~xa")` – MC ND Feb 13 '17 at 17:39
  • 1
    In addition to the above, to satisfy your requirements why are you simply excluding the extension, use the **`^|FindStr/VIXC:"%~nx0"`**. – Compo Feb 13 '17 at 18:20
  • 1
    I suggest to use the command line `for /f "delims=" %%a in ('dir * /b /a-d') do if /i not "%~nx0" == "%%~nxa" ren "%%a" "%%~na%var%%%~xa"` to ignore the batch file running this command line. – Mofi Feb 13 '17 at 18:30
  • 1
    There is no need to escape any `"` characters, unless you want them to be part of the search string... – aschipfl Feb 13 '17 at 18:40
  • @Squashman : interesting solution. How would I go on about improving the condition to exclude all files that already have APPENDTEXT in the end of their filenames. what should I change here? `for /f "delims=" %%a in ('dir *.* /b /a-d ^| findstr /v /i "\.bat$"') ren "%%a" "%%~na%var%%%~xa"` – Siddhant Rimal Feb 15 '17 at 10:27

1 Answers1

2

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
Sam Denty
  • 3,693
  • 3
  • 30
  • 43
  • Did you test this? – Jean-François Fabre Feb 13 '17 at 17:14
  • How would I go on about improving the condition to exclude all files that already have APPENDTEXT in the end of their filenames. what should I change here? `for /f "delims=" %%a in ('dir *.* /b /a-d ^| findstr /v /i "\.bat$"') do ren "%%a" "%%~na%var%%%~xa"` I've tried adding a findstr condition to check for "APPENDTEXT" in %%a right after `do` but it failed. Can you help me there? – Siddhant Rimal Feb 15 '17 at 12:45
  • @SiddhantRimal `for /f "delims=" %%a in ('dir *.* /b /a-d ^| findstr /v /i "\.bat$"|findstr /V /C:"APPENDTEXT"') do ren "%%a" "%%~na%var%%%~xa"` – Sam Denty Feb 28 '17 at 21:02
  • @SamDenty I've [already got an answer from another question](http://stackoverflow.com/q/42458717/5040900). I was having problems with piping yet again. Thanks for your input. Much appreciated. – Siddhant Rimal Mar 01 '17 at 02:58