Question : Batch file that does a file search and then if found executes a specific command on the file

Hi,

I need to find a way to do a batch file that will look for a filename (entire name or partial) and if it finds a file runs a specific command on the filename in question.

Ex :

Lets say we have:

AAACCCDDD.JPG
AAAZZZDDD.JPG
EEESSSFFF.JPG


And I want to run the batch file in question with the %1 value representing the file I need to find.

Ex :   test.bat AAA


I would like the batch file to either find a file named AAA or containing AAA and run the following command for it :

mspaint.exe full file name

The other thing is that if multiple files are found containing the search string (like in the example above) a list should be displayed and a message asking to be more precise.

Can this be done via a batch file?

Answer : Batch file that does a file search and then if found executes a specific command on the file

Here you go.

~bp
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
@echo off
set BaseDir=c:\pics
set Hits=0
for /f %%A in ('dir /a-d /b /s "%BaseDir%\*%~1*.jpg"') do set /a Hits+=1
if %Hits% GTR 1 (
  echo *** More than one matching file found ***
  for /f %%A in ('dir /a-d /b /s "%BaseDir%\*%~1*.jpg"') do echo %%~fA
  exit /b
)
for /f %%A in ('dir /a-d /b /s "%BaseDir%\*%~1*.jpg"') do (
  mspaint "%%~fA"
)
Random Solutions  
 
programming4us programming4us