4

I have a batch script that asks for a path and also asks for the type of files I want to search in the folders and subfolders in that path. Then it returns the path to those files in an output.txt file.

Here's my code:

@echo on
set LOGFILE=output.txt
set /P userInputPath=Enter the path you'd like to search?
set /p "FileType=Enter file type(s) here (ex: txt, pdf, docx): "

call :LOG > %LOGFILE%
exit
:LOG
for %%A in (%FileType%) do (dir /b /s %userInputPath%\*.%%A)
@pause

I want to avoid creating an output.txt file IF no files are found or IF the path entered is wrong. Can anyone please help me with this. Thank you!

Alain
  • 49
  • 5
  • Use the `IF` command with the `EXIST` option to check if the folder exists. Use the `FOR` command with the `/R` option to list the files. – Squashman Jan 23 '17 at 18:36
  • @Squashman How can I use those commands in my code? It's my first time writing a batch script, I am not too familiar with the syntax yet. Thank you – Alain Jan 23 '17 at 18:49

4 Answers4

2

you can add

for %%# in ("%LOGFILE%") do (
   if %%~z# equ 0 (
       del /s /q "%LOGFILE%"
   )
)

at the end.It checks if the size of the log file is 0 and if yes deletes it.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • I added it at the end. But when I run the file with a file type that doesn't exist, it still creates an output file that contains the path to the output file and the "Press any key to continue..." string. Any idea how I could avoid printing those? – Alain Jan 23 '17 at 18:48
  • 2
    @alain, because of the way you are executing it. Don't use the CALL and redirect to the log file. – Squashman Jan 23 '17 at 18:56
2

You may use the ability of for /F command of return an ExitCode of 1 when no data was processed, that is:

(for /F "delims=" %%A in ('dir /b /s "%userInputPath%\*.%FileType%"') do echo %%A) > %LOGFILE% || rem
if errorlevel 1 del %LOGFILE%

Note that this code is used instead of the subroutine call...

You may read a detailed explanation of how the ExitCode value is used in for /F command at this question; look for Exit Code management.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
1

If you use a FOR command to list the files it will never redirect output to the log file because the echo command will never execute if the FOR command does not iterate any file names.

@echo off
set "LOGFILE=output.txt"
del "%logfile%"
:LOOP
set /P "userInputPath=Enter the path you'd like to search;"
set /p "FileType=Enter file type(s) here (ex: txt, pdf, docx):"
IF NOT EXIST "%userInputPath%" (
    echo %userInputPath% does not exist
    GOTO LOOP
)

for /R "%userInputPath%" %%G in (%FileType%) do echo %%G>>%LOGFILE%
pause
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • This is really close to what I want. My only problem is when I run the script with a good path and a good file type, it doesn't print the path to the files of which I am trying to find the path of. Let's say I search for file type pdf, it returns the path to all folders and add pdf at the end. It looks like this: C:\Users\Username\Desktop\Test\pdf C:\Users\Username\Desktop\Test\Nicky Jam\pdf C:\Users\Username\Desktop\Test\Prince Royce\pdf – Alain Jan 23 '17 at 19:22
  • Nevermind. I got it thank you. Just curious, did you add the delete log file before the loop just to clear any existing log files or is there another reason? – Alain Jan 23 '17 at 19:38
  • @Alain, correct because I am appending to the log file. Each echo writes a new instance of the file so we need to append to the file. – Squashman Jan 23 '17 at 20:19
1

One more solution to find also files with hidden file attribute set which are ignored by command FOR:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "LOGFILE=output.txt"
set /P "userInputPath=Enter the path you'd like to search: "
set /P "FileType=Enter file type(s) here (ex: txt, pdf, docx): "

del "%LOGFILE%" 2>nul
for %%A in (%FileType%) do (
    for /F "delims=" %%B in ('dir /A-D /B /S "%userInputPath%\*.%%A" 2^>nul') do (
        >>"%LOGFILE%" echo %%B
    )
)
endlocal

This solution is slower as the other ones as the inner FOR reads all lines output by command DIR which are next output to handle STDERR redirected to the log file.

The error message(s) output by command DIR are redirected to device NUL to suppress them.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • set /?
  • setlocal /?

See also the Microsoft TechNet article Using command redirection operators.

Mofi
  • 46,139
  • 17
  • 80
  • 143