Searching and Separating Files Using . bat

Asked

Viewed 3,765 times

1

I have a daily/monthly process where, I have a . txt that has a list of about 10,000 file Ids (one ID per line)

(exemplo_content_file . txt)

{
12345
23456
34567
45678
56789
}

Each file ID (ex: 12345) of . txt corresponds to an . xml file (File_12345_0.xml) that is in a server folder (server02) along with 200,000 other . xml files that I’m not interested in.

I want to create a .bat that by the Ids I have in . txt, find and play in a folder only the . xml with the . txt Ids.

Roughly speaking, I think it would be logical.

rode a for to take the first ID of the first line of . txt and use it as a parameter to find the file. xml inside server02, if found, I want to move this file to a folder "PASTA_ARQUIVOS_DESEJADOS"




for (1=0 ; 1>Quantity_ids_txt ; i++)
{
Variable XXX = PICK UP FIRST ID (WHICH CORRESPONDS TO i LINE);

Se (PROCURAR .XML QUE O NOME POSSUA *XXX*.xml e encontrar){

    Mover Arquivo_<b>X</b> para C:\USER\PASTA_ARQUIVOS_DESEJADOS

}

Se não{

    registrar no arquivo LOG.txt a string "ARQUIVO XXX não encontrado"

}  




Please, could someone help me? I need to pass this logic to a . bat logic. I could do it in a c#, but in . bat do not give no. I thank you in advance!

1 answer

1


In this command below you can modify the variables to fit your environment.

Since I don’t know much about for in CMD has not yet been able to remove the error log from {.xml and of }.xml that will not be found.

In the for each line of the file will be saved in the variable %%A, so if in the file you have the ID 12345 and the name of the file to be copied is Arquivo_12345_0.xml, in the code below within the for where you have %pastaArquivos%%%A%tipo% you must modify to %pastaArquivos%Arquivo_%%A_0%tipo%.

@echo off

rem Comandos para salvar a data e a hora no log caso tenha interesse.
for /F "tokens=*" %%F in ('date /t') do set dateLog=%%F
set timeLog=%time%

rem Pasta para onde os arquivos devem ser copiados.
set minhaPasta=C:\MinhaPasta\

rem Arquivo txt que o processo vai ler com o nome dos arquivos.
set meuArquivo=C:\arquivos.txt

rem Caminho com o nome do log que deseja salvar.
set meuLog=C:\meuLog.log

rem Pasta onde estão os arquivos que devem ser copiados, tem que ter a barra no final "\".
set pastaArquivos=C:\MeusArquivos\

rem Extensão dos arquivos.
set tipo=.xml

rem Se a pasta que quer copiar os arquivos não existe esse comando vai criar ela
if not exist %minhaPasta% md %minhaPasta%

rem Caso o arquivo que tem os nomes dos arquivos não exista vai gravar o log informando.
if not exist %meuArquivo% echo Arquivo %meuArquivo% nao encontrado em %dateLog%%timeLog% >> %meuLog%

rem Inicio da verificação
for /F "tokens=*" %%A in (%meuArquivo%) do (
    if not exist %pastaArquivos%%%A%tipo% (
        echo Arquivo %pastaArquivos%%%A%tipo% nao encontrado em %dateLog%%timeLog% >> %meuLog%
    ) else (
        copy %pastaArquivos%%%A%tipo% %minhaPasta%
    )

)
rem Somente para ver o resultado da cópia dos arquivos, se não quiser remova.
pause
  • Laercio, I made a comment in my own question to be able to attach an image, so you will understand better. Could you help me with this question? Hugs and thanks for your attention

Browser other questions tagged

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