Reputation: 15
@echo OFF
@find /c /i "james" "C:\Users\ersojt\Desktop\Sample\*.eml" > NUL
if %ERRORLEVEL% EQU 0 (
move "C:\Users\ersojt\Desktop\Sample\*.eml" "C:\Users\ersojt\Desktop\Sample2"
) else (
@echo Failure
)
PAUSE
I am trying to use a Batch file to search for a specific file containing an input; then moving this file to another directory.
Can anyone help me?
Upvotes: 1
Views: 725
Reputation: 70923
@echo OFF
setlocal enableextensions disabledelayedexpansion
set "source=C:\Users\ersojt\Desktop\Sample\*.eml"
set "target=C:\Users\ersojt\Desktop\Sample2"
set "searchString=james"
set "found="
for /f "delims=" %%a in ('
findstr /m /i /l /c:"%searchString%" "%source%" 2^>nul
') do (
if not defined found set "found=1"
echo move "%%a" "%target%"
)
if not defined found (
echo Failure
)
pause
This will use a findstr
command to search for files containing the indicated search string. /m
switch is used to only retrieve the matching file names.
The findstr
is executed by a for /f
command that will retrieve its output and, for each line in the output, execute the code in the do
clause, with the line stored in the for
replaceable parameter (%%a
in this sample).
Move operations are only echoed to console. If the output is correct remove the echo
that prefixes the move
command.
Upvotes: 2