Find files on local drives using Batch

2

Good day dear,

My knowledge of scripting is very limited, so I would like the help of the community in the following question.

We have some excel files for specific names on users' computers. (suppose: file1.xls, file2.xls and file3.xls) I want to run a batch to search for these specific files.

I would like a txt log with the computer name to be created in a folder on the network by pointing to the result (the folders containing those files).

Some considerations:

The batch will be executed locally using a deploy through automatos software, so the user running the batch will be the system;

-I do not want the user to know that this search is being executed;

- If the files do not exist on the user machine, there is no need for logging, simply by stopping the batch.

As I said, I do not have much advanced script knowledge, what I've discovered so far is that I can use DIR to fetch the file and print it on the screen as below:

dir setup.txt /s

Of course, in addition to being super simple, the code does not define paths and nor does it search other local drives besides C. It is worth mentioning that users have mapped network folders and I do not want to search inside them. >

My question is: how to translate my need into code?

Thank you immediately to anyone who is ready to help. Any ideas will be welcome and tested accordingly.

Thank you and have a great day everyone

* Update:

I found this code here that is already something. It does not print the search results in the log txt with the path of the files found, but it already tells me if the files exist or not:

dir /s /a-d setup.txt >nul && (echo found it) || (echo not found)

Who can help me by improving it, I thank you right away.

    
asked by anonymous 02.03.2016 / 15:49

2 answers

0

I ended up finding an idea and developing the solution later.

I share here to help those who have the same need as I do in the future.

@echo off

rem Muda o local de execução do script para a pasta desejada. 
pushd C:\

rem Faz a busca em todos os arquivos da pasta e da subpasta desejados. Para buscar mais de um arquivo adicione a opção /s novamente seguida do nome do arquivo.
rem Caso não saiba a extensão ou o nome todo do arquivo use * antes ou depois do nome, em parte ou em todo o nome da extensão também.   
rem No exemplo abaixo, caso encontre os arquivos especificados, ele pula para a linha printresult. Caso não encontre vá para o notfound.
dir /s /a-d "setup.txt" /s "setup2.txt" >nul && (goto printresult) || (goto notfound)

:printresult
rem Ao encontrar os arquivos o script produz um log em txt na pasta apontada, usando o nome da máquina como nome do arquivo em frente à palavra FOUND.
dir /s /a-d setup.txt /s setup2.txt >>"\servidor\pastadelogs\FOUND_%computername%.txt

rem A opção abaixo retorna o prompt para o local inicial
popd
exit


:notfound
rem Caso não encontre os arquivos o script gera um log usando o nome da máquina como nome do arquivo em frente à palavra NOTFOUND. 
echo "Este PC não possui os arquivos" >>"\servidor\pastadelogs\NOTFOUND_%computername%.txt
popd
exit
    
03.03.2016 / 14:01
0

If I understood what looks for :

@echo off && setlocal enableextensions enabledelayedexpansion 

set /a _conta=1 - 1

set _arqui=%temp%\_setup_file_.log

set _ocorr=ocorrencias

(

if exist "!_arqui!" del /q /f "!_arqui!"

<nul type nul >"!_arqui!"

)  2>nul >nul 

echo. & echo/Buscando Arquivos...

(

for /f "tokens=*" %%i in ('^<nul dir /o-d /on /b /s *.exe') do echo/%%~i>>"!_arqui!"

for %%a in ("!_arqui!") do for /f "tokens=3 delims=:" %%b in ('find /v /c "" "%%a"') do call set /a _conta=%%b

if !_conta! equ 1 call set _ocorr=ocorrencia

) 2>nul >nul 

(

type !_arqui! | findstr /r /c:"[a-Z]" 2>nul >nul ) && ( call :_result ) || ( call :_error ) 

) 2>nul >nul 

goto :eof

:_result

echo. & echo/R E S U L T A D O S: Busca retornou !_conta! !_ocorr! para busca :Setup.txt:^^!& echo.

set /p "_yEp_or_nOp=Abrir o log Sim(s) | Nao(n): "

if /i "!_yEp_or_nOp!"=="s" call start "" /b Notepad.exe "!_arqui!"

copy "!_arqui!" "%~dp0\" /y 2>nul >nul 

exit /b

:_error

echo. && echo/ E R R O R: Busca sem !_ocorr! para busca no :Setup.txt:^^!

exit /b
    
08.12.2018 / 19:49