Page 1 of 1

Handling property files in a DOS batch script.

Posted: 09 Jan 2012 12:38
by djangofan
I created this test script to handle property files in DOS batch scripts. The scripts works fairly well as-is but I am hoping for any tips that anyone might have for improving it?

NOTE: On the PROPEDIT function, there is an error that I am unable to figure out :
The system cannot find the file specified.
Error occurred while processing: .exe.


Anyway, thanks to anyone who might want to contribute some advice on this:

Code: Select all

@echo off

CALL :PROPEDIT # Key4 Value446 test.properties

GOTO :END

:PROPEDIT [#] PropKey PropVal File
IF "%~1"=="#" (
  :: Passing a first argument of "#" will disable the line while editing
  SET "_PREFIX=#"
  SHIFT
)
IF "%~3"=="" (
  ECHO PROPEDIT: Function requires 3 args: [#] PropKey PropVal File
  PAUSE
  GOTO :END
) ELSE (
  SET "_PROPKEY=%~1"
  SET "_PROPVAL=%~2"
  SET "_FILE=%~3"
)
MOVE /Y "%_FILE%" "%_FILE%.bak">nul
FOR /F "USEBACKQ tokens=*" %%A IN (`TYPE.exe "%_FILE%.bak" ^|FINDSTR.exe /N /I "%_PROPKEY%="`) DO (
  SET LINE=%%A
)
FOR /F "tokens=1,2* delims=:" %%S IN ("%LINE%") DO SET LINE=%%S
SET /A COUNT=1
FOR /F "USEBACKQ delims=" %%A IN (`TYPE.exe "%_FILE%.bak" ^|FIND.exe /V /N ""`) DO (
  SET "LN=%%A"
  SETLOCAL ENABLEDELAYEDEXPANSION
  SET "LN=!LN:*]=!"
  IF "!COUNT!" NEQ "%LINE%" (
      ECHO(!LN!>>%_FILE%
  ) ELSE (
      ECHO %_PREFIX%%_PROPKEY%=%_PROPVAL%>>%_FILE%
      ECHO Updated '%_FILE%' with value '%_PREFIX%%_PROPKEY%=%_PROPVAL%'.
  )
  ENDLOCAL
  SET /A COUNT+=1
)
EXIT /B 0

:END
ECHO --- Finished Test ---
pause

Re: Handling property files in a DOS batch script.

Posted: 14 Jan 2012 13:20
by djangofan
I simplified my question as much as I could. Here is the output from the file now:

Code: Select all

The system cannot find the file specified.
Error occurred while processing: .exe.
test.properties.bak
The system cannot find the file specified.
Error occurred while processing: .exe.
test.properties.bak
--- Finished Test ---
Press any key to continue . . .

Re: Handling property files in a DOS batch script.

Posted: 14 Jan 2012 17:34
by alan_b
I would guess that it is complaining about ".exe"
4 possibilities in your code

TYPE.exe ^|FINDSTR.exe
TYPE.exe ^|FIND.exe

Do you need to tell CMD.EXE that these are executable's ?
Perhaps there is some strange "parsing" effect caused by the '.' dot before the EXE

Could it work well be removing every instance of '.exe'

Re: Handling property files in a DOS batch script.

Posted: 14 Jan 2012 19:40
by aGerman
A good way to debug a batch file is to remove the "@echo off". This enables you to figure out what line causes the problems.

Also make sure you are in a working directory where the files are available, the PATH environment variable is inviolated and your batch file has not the same name of one of the used commands.

Regards
aGerman

Re: Handling property files in a DOS batch script.

Posted: 19 Jan 2012 17:14
by djangofan
Thanks. By adding this to the top of the file, I was able to get it working:

@echo off &SETLOCAL ENABLEDELAYEDEXPANSION

More specifically, the "SETLOCAL" is what made the difference.

Also, i had to change TYPE.exe to just "TYPE" . Find.exe and Findstr.exe worked fine when called that way.

Any other suggestions on a quicker or easier way to do this kind of thing would be welcome (but not expected).

Re: Handling property files in a DOS batch script.

Posted: 19 Jan 2012 17:45
by aGerman
djangofan wrote:More specifically, the "SETLOCAL" is what made the difference.

Strange. Don't see any reason why :?


djangofan wrote:Also, i had to change TYPE.exe to just "TYPE" . Find.exe and Findstr.exe worked fine when called that way.

Of course. You have to distinguish between external command line tools and internal functions of cmd.exe.
This batch file is processing the output of HELP and searching for an exe or com file with the same name. That should give you an idea of what I'm talking about.

Code: Select all

@echo off &setlocal EnableDelayedExpansion
pushd "%SystemRoot%\system32"
for /f %%i in ('help^|findstr /rbc:"[A-Z][ABCDEFGHIJKLMNOPQRSTUVWXYZ]"') do (
  dir /b "%%i.exe" "%%i.com" 2>nul ||(
    set "line=%%i                    "
    echo !line:~,20! - internal
  )
)
popd
pause

Regards
aGerman