简体   繁体   中英

Batch file dir command

ok, sry if the question will get little clumsy, but i dont know how else to give you information for you to be able to help me. I need to write a batch file to be able to do several tasks. Here is the whole code i've written:

@echo off  
:pasirinkimas  
cls  
echo Spauskite 1, jei norite pakeisti ekrano ir teksto spalva  
echo Spauskite 2, jei norite isvesti i ekrana sistemos kintamuosius  
echo Spauskite 3, jei norite ieskoti failu  
echo Spauskite 4, jei norite baigti darba  

set /p pasirinkimas= Iveskite psirinkima:   

if %pasirinkimas% == 1 goto Spalvos  
if %pasirinkimas% == 2 goto Kintamieji  
if %pasirinkimas% == 3 goto Paieska  
if %pasirinkimas% == 4 goto pabaiga  
goto pasirinkimas  

:Spalvos  
echo 0 = Black        A = Light Green  
echo 1 = Blue         B = Light Aqua  
echo 2 = Green        C = Light Red  
echo 3 = Aqua         D = Light Purple  
echo 4 = Red          E = Light Yellow  
echo 5 = Purple       F = Bright White  
echo 6 = Yellow  
echo 7 = White  
echo 8 = Gray  
echo 9 = Light Blue  

set /p fonas= Pasirinkite fono spalva:   
set /p tekstas= Pasirinkite teksto spalva:  
color %fonas%%tekstas%  
goto pasirinkimas  

:Kintamieji  
set  
:loop  
echo Ar norite testi darba (t/n)?:  
set /p darbas=  
if %darbas% == t goto pasirinkimas  
if %darbas% == n goto pabaiga  
goto loop  

:paieska  
echo iveskite ieskomo failo pavadinima:  
set /p ieskau=  
dir %ieskau%  
pause>null
goto pasirinkimas  

pause>null  

:pabaiga  
exit  

So i need help with third function. Searching for files. Though i have @echo off at the very beggining, when im trying to look for a file, the screen displays unneeded text no matter if the file was found or not:

Volume in drive C has no label.  
Volume Serial Number is DEB6-826F  
Directory of "displays directory the batch file is in"  

What i need is:
1. if batch finds the file im searching for that it would display the file size (which it does + first 2 unwanted lines).
2. When it doesnt find the file im looking for i need the batch file to display the message of my choosing.
How do i achieve this?

as far as my limited starter knowledge goes, @echo off should have dealt with volume messages. tried changing dir %ieskau% line with dir /b /s %ieskau% line but that displays only file directory. I need that + file size.

(i hope im clear enough on what i need. Tried to be as clear as possible and give all the information possibly needed)

edited

if exist "%ieskau%" (
    dir /a-d "%ieskau%" 2>nul | findstr /r /c:"^ .*:\\.*" /c:"^  [ ]*[1-9]"
) else (
    echo File not found
)

If the indicated file exist, retrieve information, else show error.

The dir output is filtered by findstr using two regular expressions ( /r ): the first one will match the line with the folder name (the only that starts with a space and contains the string :\\ ) and the second one will match the line with the file size (starts with two or more spaces followed by a digit between 1 and 9)

you could brobably do something like;

Cd C:\
if not exist "*%ieskau%*" goto none
dir /s /b "*%ieskau%*" >C:\myfile.txt
for /f "delims=" %%i in (text.txt) do set c=%%i
echo these are your files %c%
exit /b
:none
echo no file found.
exit /b

But you will have to let it load for about 10 seconds (while its searching) also /b is optional, it just makes it look better...

hope that helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM