Create bat to move files returned by findstr

Asked

Viewed 1,204 times

4

Good afternoon to all!

I have come across a daily problem where, I have several . xml to open and check if these have tags:

< desc_produto >Riscos< /desc_Produto >"

or

< tp_mov>P01< /tp_mov>

To do this, I was performing this procedure manually, but I decided to make a . bat that can make my life easier. I found the findstr command that performs this procedure (Windows 7). So far I am only running at the prompt the following command:

findstr /i /s "< desc_produto>Riscos< /desc_Produto>" *. *

However, I would like to improve the . bat, where, when executing it, the system will check on the content of all . xml folder to find the files that have this text tag. And when finding these files, create a Risks folder and move them inside it.

Could someone please help me with this need?

  • Did the answer help you solve the problem? Is it possible to accept it or need some complementary information?

  • Oops! It helped me a lot, that’s exactly what I needed. I just don’t know how to accept it and close the question! I’m new at stackoverflow

  • You can give 1 point for the answer, on the up arrow and further down the arrow, has a "right" sign, where you accept the answer.

  • @Bueno Good afternoon, I found it a little out of the ordinary your xml tags are with spaces in the statement, they are just like that?

3 answers

0

I did not understand very well and wanted to do the same thing sending from the aquivo with the Palacra "banana" to another directory.

  • This answer would look better as Comentário.

0

inserir a descrição da imagem aqui

Response considering questioning in the comment for destination folder filtering where there are occurrences of multiple strings or not, using the findstr....

It is possible to use findstr and act in accordance with the exit operadores:

• Comments on operators & | && and || and their uses according to the demeanor of their commands:


execute & execute & execute 
execute | recebe _saída_do_comando_anterior 
execute | recebe _saída_do_comando_anterior & execute
executou_sem_erro && então_execute_também 
executou_com_erro || execute_também_porque_deu_erro 
executou_com_erro || executou_com_erro || executou_com_erro 
executou_sem_erro && executou_sem_erro && executou_sem_erro 
executou_sem_erro && então_execute || execute_esse_no_primeiro_deu_erro


Then just follow the logic:

acho o 1º ? sim e achou também o 2º ? sim (então) faz X ! (string && string && faz X)  
acho o 1º ? sim e achou também o 2º ? não (então) faz Y ! (string && string || faz Y)  
acho o 1º ? não e achou também o 2º ? não (então) faz Z ! (string || string || faz Z)  

Para:: 

achou !_str_1! ? sim e achou também a !_str_2! ? sim (então) mover p\ Risco !
achou !_str_1! ? sim e achou também a !_str_2! ? não (então) mover p\ Arisco !
achou !_str_1! ? não e achou também a !_str_2! ? não (então) Não mover !

Example in the code:


type "%~1"|!_sys!findstr /i "!_str_1:^=!" >nul) && (!_sys!findstr /i "!_str_2:^=!" "%~1"

@echo off & setlocal enableextensions enabledelayedexpansion & mode 102,5 & title <nul 

title Q200198 & set "_sys=%__appdir__%" & 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 %%l in (1 1 90)do set "_spc=!_spc! "

2>nul >nul (mkdir .\Riscos & mkdir .\Arisco) & set "_dir_1=Arisco" & set "_dir_2=Riscos" & echo/ & echo/ 
set "_str_1=\^<\ tp_mov\^>P01\^<\ \/tp_mov\^>" && set "_str_2=\^<\ desc_produto\^>Riscos\^<\ \/desc_produto\^>"

for /f %%f in ('!_sys!where /r "%cd%" "*.xml" ^|findstr /vi "!_dir_1! !_dir_2!"')do call :^) "%%~f" "%cd%\!_dir_1!" "%cd%\!_dir_2!"
echo/!_cr!&& goto :^?

:^)
set "_dest=.." && set _msg=<nul & 2>nul ((
(type "%~1"|!_sys!findstr /i "!_str_1:^=!" >nul) && (!_sys!findstr /i "!_str_2:^=!" "%~1"  >nul) && (
move /y "%~1" "%~2" >nul & set "_dest=Riscos")))||((type "%~1"|!_sys!findstr /i "!_str_2:^=!" >nul) && (
move /y "%~1" "%~3" >nul & set "_dest=Arisco")) >nul 
echo/!_cr! & if "!_dest!" == ".." (set "_msg= Arquivo Analisado "%~nx1" -^> Tags nao encontradas !_spc!"
     ) else (set "_msg=  Arquivo Analisado: "%~nx1" -^> Movido para a pasta !_dest! !_cr!")
      <nul set /p"^=!_bs!!_cr!!_msg:~0,100!!_cr!" & ping -n 2 -4 127.0 >nul & call :^] "!_msg:~0,100!"
      exit /b 

:^]
for /f %%h in ('cmd /u/c set /p "^=%_msg:"= %"^<nul^|find /v /c "^|"')do (
     for /l %%H in (1 1 !_i!)do <nul set /p"^=!_bs!") && exit /b 

:^?

  • @Bueno After eating more time, I will still put the respective comment in the codes, any doubt or "adjustment" necessary, comment... thanks...I liked your question...

0


Utilize for to browse your search result with findstr.

Create a batch findtags.bat with the following content:

@echo off
rem Garante que as variáveis de ambiente tenham escopo local
    setlocal
rem Cria diretório não retornando erro se já existe
    if not exist Riscos\. md Riscos
rem Busca em subdiretórios retornando nome dos arquivos encontrados
    for /f %%a in ('findstr /I /S /M "< desc_produto>Riscos< /desc_Produto>" *.*') do (
        echo Movendo %%a
        move %%a Riscos\
    )
    endlocal
  • Thank you very much! You helped me so much, that’s exactly what I needed! Now, following the same logic, is it possible to include two items in a check? example: If in the same xml you have the tags < desc_product>Risks< /desc_product> and also have the tag < tp_mov>P01< /tp_mov>, then move to folder XXXX How can I do this?

  • Yes, you use regex in findstr with /R for this search. Or you can do the second search in the returned file like: findstr "Blabla" %a

Browser other questions tagged

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