1

here is the batch script :

@echo off
cls
setlocal EnableDelayedExpansion
title  %~nx0 %1
color 0f
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat"
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat"
echo.
set linker_file="C:\my_compiler_libs.txt"
if %~x1==.cpp  cl /EHsc @%linker_file% %1 
if %~x1==.c    cl /EHsc @%linker_file% %1 
if %~x1==.sln  msbuild %1 /m  /p:configuration=debug /p:platform=win32   
echo.
if exist "%~n1.obj" del "%~n1.obj"
echo.
echo     1: compile again
echo     2: run in cmd 
echo     3: run
echo     4: exit 
echo.
set /p answer=
if %answer%==1 call %~f0 %1
if %answer%==2 cmd /k "%~n1.exe"
if %answer%==3 "%~n1.exe"
if %answer%==4 exit /b

when i choose 1 to compile again it works and my_compiler.bat call itself as needed but only tree times if choose 1 the forth time it crashes with error the input line is too long any help please

Neo Mosaid
  • 367
  • 3
  • 17
  • Have you inspected what the line `call %~f0 %1` looks like? What are the values of `%~f0` and `%1`? – GolezTrol Dec 20 '14 at 15:01
  • %~f0 is the full path to the running batch file and %1 is the command line passed to this batch file in my case a .cpp file to be compiled – Neo Mosaid Dec 20 '14 at 15:03
  • Maybe not a duplicate, but [this question](http://stackoverflow.com/questions/16821784/input-line-is-too-long-error-in-bat-file) seems to be about the same error message. Maybe it's helpful. – GolezTrol Dec 20 '14 at 15:05
  • Batch strings can only hold up to 8192 characters. Are you hitting the limit? – SomethingDark Dec 20 '14 at 15:18
  • thanks @GolezTrol adding this to the script fixes the problem `if not defined DevEnvDir ( call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat" call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat" )` – Neo Mosaid Dec 20 '14 at 15:21
  • 1
    I'd just remove the `call` from the `if %answer%==1 call %~f0 %1` line. – Magoo Dec 20 '14 at 15:38
  • it is not a batch (cmd ) problem related, there is no problem with `call %~f0 %1` the problem is with the definition of the environment variables defined in `vsvars32.bat` – Neo Mosaid Dec 20 '14 at 19:15

1 Answers1

2

Either vsvars32.bat or VsDevCmd.bat (or both) are appending to a variable (most likely PATH) without checking to see if the needed value already exists. Eventually the command line that attempts to append to the variable exceeds the maximum command line length of 8191 byytes.

You have solved the problem by using:

if not defined DevEnvDir (
  call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat"
  call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat"
)

There are other ways to solve the problem.

1) Use ENDLOCAL to discard the environment changes that have been made before restarting. As noted by Magoo, there is no need to return to the parent batch, so there is no need for CALL.

if %answer%==1 (
  endlocal
  %~f0 %1
)

2) Put a label below the problem CALLs and use GOTO

@echo off
cls
setlocal EnableDelayedExpansion
title  %~nx0 %1
color 0f
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat"
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat"
echo.
set linker_file="C:\my_compiler_libs.txt"

:compile
cls
if %~x1==.cpp  cl /EHsc @%linker_file% %1 
if %~x1==.c    cl /EHsc @%linker_file% %1 
if %~x1==.sln  msbuild %1 /m  /p:configuration=debug /p:platform=win32   
echo.
if exist "%~n1.obj" del "%~n1.obj"
echo.
echo     1: compile again
echo     2: run in cmd 
echo     3: run
echo     4: exit 
echo.
set /p answer=
if %answer%==1 goto compile
if %answer%==2 cmd /k "%~n1.exe"
if %answer%==3 "%~n1.exe"
if %answer%==4 exit /b

I'm not sure why you are enabling delayed expansion since I can't see anywhere that you use it. I left it in, but I'm pretty sure you can change setlocal EnableDelayedExpansion to just setlocal.

dbenham
  • 127,446
  • 28
  • 251
  • 390