How to check file name from prefix

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gio123bg
Posts: 2
Joined: 10 Feb 2014 12:00

How to check file name from prefix

#1 Post by gio123bg » 11 Feb 2014 04:04

Hi All,
I need to rename a file if it exists with a prefix (i.e. "MORE****.txt"). More exactly in my code

@echo on
FOR /F %%i in ('dir %DIRINPUT%MORE*.txt /B') do (
IF %ERRORLEVEL% EQU 0 (
echo file found
set FILENAME=%%i
) else (
echo ERROR: file not found
)
)

I want to avoid the system error "file not found" and display my message "ERROR: file not found" without exit from my batch file.
I tried to use this code

set FILE01=MORE*.txt
IF EXIST %DIRINPUT%%FILE01% (
echo file found
) else (
echo ERROR: file not found
)

but I receive an error if the file doesn' exist.

Any help will be well appreciated.

Thanks in advance for your kind support.

Regards,

Giovanni

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to check file name from prefix

#2 Post by foxidrive » 11 Feb 2014 04:19

This will echo the filename only if it exists.

Code: Select all

@echo on
FOR /F %%i in ('dir %DIRINPUT%MORE*.txt /B /a-d 2^>nul ') do echo "%%i"

gio123bg
Posts: 2
Joined: 10 Feb 2014 12:00

Re: How to check file name from prefix

#3 Post by gio123bg » 11 Feb 2014 08:43

Good!! Thank you very much indeed for your suggestion and for the quick reply.

Post Reply