Can someone suggest why this script is failing?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ande-s
Posts: 1
Joined: 23 Sep 2011 08:08

Can someone suggest why this script is failing?

#1 Post by ande-s » 23 Sep 2011 08:10

Thanks in advance.

@Echo Off
SetLocal EnableDelayedExpansion
::Script to input text from a text file, and check each line to see if it starts "curPath: "
::Once the "curPath: " line has been identified, save it to a variable

Set _InputFile=F:\Locations.txt

for /f "tokens=* delims= " %%a in (%_InputFile%) do (
set /a N+=1
set v!N!=%%a
set vPath=!v!N!

::Remove the first 9 characters of the text line and check if it starts "curPath: "

if %vPath:~9% == "curPath: "
varPath == vPath

)
::Trim the path
set varPath=%varPath:curPath: =%

::echo %varPath%

IF "%varPath"=="Path1" set appPath="C:\Path1.py"
IF "%varPath"=="Path2" set appPath="C:\Path1.py"
IF "%varPath"=="Path3" set appPath="C:\Path1.py"
IF "%varPath"=="Path4" set appPath="C:\Path1.py"

start "" "%appPath%"

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Can someone suggest why this script is failing?

#2 Post by Ed Dyreen » 23 Sep 2011 16:21

'
You've got so many things wrong that I just eliminated the most obvious errors, there are more less obvious ones I'm sure.

Code: Select all

@Echo Off &SetLocal EnableDelayedExpansion

::Script to input text from a text file, and check each line to see if it starts "curPath: "
::Once the "curPath: " line has been identified, save it to a variable

Set "_InputFile=F:\Locations.txt"

for /f "tokens=* delims= " %%! in (

  "!_InputFile!"

) do (
  set /a N += 1
  set "v!N!=%%!"
  "set vPath=!v!N!"
  ::Remove the first 9 characters of the text line and check if it starts "curPath: "
  if /i ["!vPath:~9!"] == ["curPath: "] (
    ::
    set "varPath=!vPath!"
  )
)
::Trim the path
set "varPath=!varPath:curPath: =!"

::echo %varPath%
set /a $error = 1
for /l %%! in (

  1, 1, 4

) do if /i ["!varPath!"] == ["Path%%~!"] (
  ::
  set /a $error = 0
)
if !$error! equ 0 (
  ::
  set appPath="C:\Path1.py"
  start "" "!appPath!"
)
Why do you enable delayed and then not use it ??

Your coding is very illogical, confusing... I rarely see such bad coding !
Maybe copy the dostips functions or mine, you may learn something.
Dostips functions :arrow:
http://www.dostips.com/
ED's functions :arrow:
viewtopic.php?f=3&t=1893&start=0

I've noticed you use a leading underscore when defining a variable.
In many languages the leading underscore means that the variable is globally available and is read-only, not to be written to by the programmer. Or even worse, that it is a build-in function. That could cause confusion, when mastering other langs.
Like marijuana I see DOS as a gateway drug :mrgreen:
But there are other leading chars available to choose from when identifying a var.
I think the larger part of this community uses $ to represent variables as it is often used in other langs like so.

Post Reply