For command not working with Findstr usage

Asked

Viewed 402 times

3

I’m trying to execute command for at command prompt to delete all my files that have text "< cd_comiss>P< /cd_comiss>", assigning them to a variable %%e, but he returns me that:

%and was unexpected at this time

Could someone help me?

Obs.: I’m navigating to the folder I want to then run the command:

for /f %%e in ('findstr "<cd_comiss>P</cd_comiss>" *.*') do del %%e
  • It worked! Thanks for your help

2 answers

2


For the form you want to execute, simply change the form you are using the variable, instead of using %%e utilize %e:

for /f %e in ('findstr "<cd_comiss>P</cd_comiss>" *.*') do del %e

Just to clarify, variable usage is different when you want to run directly by Prompt or run a batch file.

When running directly through the prompt use: %e
For each batch file, use: %%e

As the documentation guides, in free translation:

{% Variable | %% variable}: Required. Represents a parameter replaceable. Use the %variable to run from the command. Use %% variable to execute the command inside a batch file. Variables differentiate between lower case and lower case should be represented with an alpha value such as %A, %B or %C.

View documentation here.

0

- Code bat Suicida: Avoid this!!


at bat:

- for /f %%e in ('findstr "<cd_comiss>P</cd_comiss>" *.*')do del %%e

at the prompt:

- for /f %e in ('findstr "<cd_comiss>P</cd_comiss>" *.*')do del %e


inserir a descrição da imagem aqui

  • Deletes all files with string (and without it), even the Bat, did not use filters, any of the characters are worth, and yet, is allowing the action in all files "*.*"...

  • Obs.: Of course, it would only erase if the code worked, but fortunately it doesn’t, since the command findstr and the for, need characters of escape us: \, /, ^, <, >,< |, &, etc.,.... - Applies also the comma ","

Apaga todos os arquivos com a string, menos o próprio Bat, use filtro:
   |findstr /vilc:"%~0"
Ou, Apaga todos os arquivos com a string, menos o próprio Bat, use filtro:
   |findstr /vilc:".cmd"


Already with the use of exhaust, from there gets more controlled/filtered this kind of processing, and it will even work, but ends with deleting/listing until a GIF file, file that doesn’t even have the string (complete) searched in its content, but will be deleted because some character of the string will hit! In this command/code, findstr is being used to treat all files (*.*), and how the findstr tends to treat everything as Regex (except with the use of /L, /c:, \, I, etc.. ), so any character of the file that hits ends up being valid...:


- Try not to use unfiltered us commands: del, deltree, rmdir, move, ren, rename, etc...


  • Use at all times with a filter more safe/accurate, go through a echo before:
  • Implement a filtering system to catch a extension specifies: - Example: "*.xml"

for /f %%e in ('findstr "^<cd_comiss^>P^<\/cd_comiss^>" *.xml^|findstr /vilc:"*\.cmd"')do echo/del %%e

Using a bat for find and delete files .xml and only those containing the string: ("<cd_comiss>P</cd_comiss>"):

  • Obs.: The string is only in the . xml pair files: 002, 004, 006, 008, 010!

inserir a descrição da imagem aqui


@echo off && setlocal enableextensions enabledelayedexpansion & echo/

echo/ & title <nul & title Q213311 & mode 100,10 & set "_str="\^<\ cd\_comiss\^>P\^<\ \/cd\_comiss\^>"" & echo/ 
for /f %%c in ('copy /Z "%~dpf0" nul')do set "_cr=%%c" & for /f %%a in ('"prompt $H&for %%b in (0) do rem"')do set "_bs=%%a"

for /f %%f in ('where /r . "*.*"^|findstr /vilc:"%~0"')do for /f ^tokens^=^*delims^=^  %%E in ('type %%f^|findstr /ilm !_str!')do (
     ping -n 2 -4 127.0 >nul & echo/ !_cr! & del /q /f "%%~f" >nul && <nul set /p"^=!_bs!!_cr! ^|Arquivo:^| "%%~f" ^| Apagado^!!"
    )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.