If Statement Echoing Out Even Though @echo off and With >nul and2>nul

I will start by saying that I am very inexperienced with Batch, and therefore am reliant on chatGPT to figure out some syntax, but it seems that chatGPT cannot solve the problem I’m having.

As I am attempting to follow a textbook on C++, there are example programs. I wish to run these programs, but I constantly need to compile. Visual Studio doesn’t provide an easy way to compile a program on a whim like you might with Eclipse and a Java file (I cannot simply create a new file and then run that said file because Visual Studio 2022 is expecting a single main function out of all .cpp files in the project, meaning I have to constantly use cl /EHsc in the command prompt). I have therefore attempted to create a batch script to compile programs for me using Visual Studio’s compiler.

What I have created thus far is this:

@echo off

:: Set up the Visual Studio environment
call "C:Program Files (x86)Microsoft Visual Studio2019BuildToolsVCAuxiliaryBuildvcvarsall.bat" x64

:: Prompt for the source directory
:askSourceDirAgain
set /p source_dir="Enter source directory: "
if not exist %source_dir% (
    echo Directory not found, please try again.
    goto askSourceDirAgain
)
echo Source directory found

:askSourceFileAgain
set /p "source_file=Enter cpp file name: "

:: append .cpp if not provided
echo %source_file% | findstr /i ".cpp$" >nul
if errorlevel 1 (
    set "source_file=%source_file%.cpp"
)
pause

:: verify file exists
if not exist "%source_dir%%source_file%" 1>nul 2>nul  (
    echo File %source_file% not found in directory, please try again.
    goto askSourceFileAgain
)

:: compile the program
:recompileProgram
set output_dir="%source_dir%Compiled"
if not exist "%output_dir%" mkdir "%output_dir%Compiled"
cl /EHsc "%source_dir%%source_file%" /Fe"%output_dir%%~n0.exe"

:: ask to run the compiled program
set /p runProgram="Run the compiled program? (y/n)"
if /i "%runProgram%"=="y" (
    start cmd /k "%output_dir%%~n1.exe"
)

:: ask the user if they want to recompile, change directory, or exit
:chooseNextStep
echo "Choose an the next option"
echo "1. Recompile"
echo "2. Choose another program to compile"
echo "3. Choose another directory and compile another program"
echo "4. Exit"
set /p choice="Choice: "

if "%choice%"=="1" (
    goto recompileProgram
) else if "%choice%"=="2" (
    goto askSourceFileAgain
) else if "%choice%"=="3" (
    goto askSourceDirAgain
) else if "%choice%"=="4" (
    exit /b 0
) else (
    echo "Invalid response, please try again."
    goto chooseNextStep
)

The problem I’m having is that if not exist "%source_dir%%source_file%" 1>nul 2>nul is printing out text when it should not be, even though (as far as I understand) I am sending outputs to a nul and echo is off.

The output I receive, when purposely providing a file name that does not exist, in the terminal is:

Enter cpp file name: wdaw
Press any key to continue . . .
'--' is not recognized as an internal or external command,
operable program or batch file.
File wdaw.cpp not found in directory, please try again.
Enter cpp file name:

When the output I expect is:

Enter cpp file name: wdaw
Press any key to continue . . .
File wdaw.cpp not found in directory, please try again.
Enter cpp file name:

The pause is in there for debugging.

I’m very confident there are other problems in this script, since like I said I am rather inexperienced in Batch and am trying to utilize chatGPT, so feel free to point those out as well.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật