Find files on local drives using Batch

Asked

Viewed 178 times

4

Good morning dear friends,

my knowledge of script is very limited and so I would like to count on the help of the community in the following question.

We have some excel files of 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 then like a txt log with the computer name to be created in a network folder pointing to the result (the folders containing those files).

Some considerations:

The batch will run locally using a deploy via software automates, so the user running the batch will be the system;

-I don’t want the user to know that this search is running;

-If the files do not exist on the user’s machine, there is no need for the log, simply the batch being stopped.

As I said, I don’t have much advanced knowledge in script, what I’ve discovered so far is that I can use DIR to fetch the file and print on screen as below:

dir setup.txt /s

But of course, in addition to being super simple, the code does not define paths and nor does it search for other local drives besides C. It is also worth saying that users have mapped network folders and I do not want to search within these.

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

I immediately thank those who are willing to help. Any idea will be welcome and tested accordingly.

Thank you and a great day to all

*Update:

I found this code here that’s something. It does not print the search results in the txt log with the path of the found files, but 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 improving immediately thank you.

  • only in the Root of the unit, or subfolders, of subfolders, etc., also?

  • @David Subfolders too. If possible on all non-networked drives or cv/dvd drive. If this is not possible, on the whole c drive.

2 answers

0

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

I share here to help those who have the same need as me 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

0


inserir a descrição da imagem aqui

If I got it what quest:

@echo off && setlocal enabledelayedexpansion 

set /a "_cnt=0" & set "_arqui="%temp%\_setup_file_.log"" & set "_ocorr=ocorrencias" 
echo/ & set "_busca=setup.txt" & del /q /f !_arqui! 2>nul >nul & type nul >!_arqui!

echo/Buscando Arquivos... & type nul >!_arqui! & title <nul & title Q116162
2>nul >nul (
for /f "tokens=*delims= " %%i in ('where /r "." !_busca!')do echo/%%~i>>!_arqui!
for %%a in (!_arqui!) do for /f "tokens=3delims=:" %%b in ('find /v /c "" "%%a"')do set /a _cnt=%%b)
(type !_arqui!|findstr . >nul||goto :^[ ) & if !_cnt! leq 1 set "_ocorr=!_ocorr:~0,-1!" 

:^)
choice /m "Abrir o arquivo log? [ Sim(s) | Nao(n) ] : " & if !errorlevel! == 1 ( 
call start "" /b Notepad.exe !_arqui! & copy /y !_arqui! "%~dp0\" /y 2>nul >nul 
echo/ & echo/RESULTADOS: Busca retornou !_cnt! !_ocorr! para sua busca [!_busca!] & exit /b) else (
:^[
if !_cnt! equ 0 echo. & echo/ERROR: Busca retorna sem !_ocorr:~,-1! para [!_busca!] & exit /b)

Browser other questions tagged

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