Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
djangofan
- Posts: 23
- Joined: 04 Nov 2011 12:28
#1
Post
by djangofan » 09 Jan 2012 12:38
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
Last edited by
djangofan on 14 Jan 2012 14:16, edited 1 time in total.
-
djangofan
- Posts: 23
- Joined: 04 Nov 2011 12:28
#2
Post
by djangofan » 14 Jan 2012 13:20
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 . . .
-
alan_b
- Expert
- Posts: 357
- Joined: 04 Oct 2008 09:49
#3
Post
by alan_b » 14 Jan 2012 17:34
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'
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#4
Post
by aGerman » 14 Jan 2012 19:40
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
-
djangofan
- Posts: 23
- Joined: 04 Nov 2011 12:28
#5
Post
by djangofan » 19 Jan 2012 17:14
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).
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#6
Post
by aGerman » 19 Jan 2012 17:45
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