2

I have to convert the following shell file into a batch file:

\#!/bin/bash
\#
\# Generates and compiles a lexer or parser and runs it (this means
\# that the lexer/parser must contain a main method)
\#
\# Example usage: antlr4-run.sh Lexer.g4

DIR="$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)"
ANTLR_JAR=$DIR/lib/antlr-4.2.2-complete.jar
BASENAME="${1%.*}"
OUTDIR=out

echo "Generating..."
java -jar $ANTLR_JAR $1 -o $OUTDIR || exit 1

echo "Compiling..."
(cd $OUTDIR; javac -cp .:$ANTLR_JAR ${BASENAME}*.java) || exit 1

\#\# run (try parser first, then lexer, then plain)
echo "Input (end with ^D):"

if [[ -a $OUTDIR/${BASENAME}Parser.class ]] ; then
      (cd $OUTDIR; java -cp .:$ANTLR_JAR ${BASENAME}Parser) || exit 1 
elif [[ -a $OUTDIR/${BASENAME}Lexer.class ]] ; then
      (cd $OUTDIR; java -cp .:$ANTLR_JAR ${BASENAME}Lexer) || exit 1 
elif [[ -a $OUTDIR/${BASENAME}.class ]] ; then
      (cd $OUTDIR; java -cp .:$ANTLR_JAR ${BASENAME}) || exit 1 
fi

exit 1

Could you please help me with give an example for the following line?

DIR="$(cd `dirname "${BASH_SOURCE[0]}"` && pwd)"

I know that I have to define a variable in a batch file with set Directory ="..." for example and that $ is a commandoline parameter... but I don't know how to write this line in a batch file :/

0

1 Answer 1

2

You should change the variable name DIR to something else (Win is case insensitive, and dir is a command). However, here's the equivalent (check [MS.Docs]: Batch parameters for more details):

set _DIRNAME=%~dp0

Note that it will not remove the trailing backslash ("\").

More: if the trailing "\" is a problem, this is what can be done:

set _DIRNAME=%~dp0
set _DIRNAME=%_DIRNAME:~0,-1%

It is a workaround (gainarie), but it works.

@EDIT0:

Also, posting the code from (last) comment.

@EDIT1:

Since that method is not robust (e.g. if the extension is present elsewhere in the path, it will be stripped out as well), also posting an alternative that works and only requires one line:

script.bat:

@echo off
setlocal enabledelayedexpansion

set _EXT=%~x1
set _NAME_EXT=%~1
set _NAME=!_NAME_EXT:%_EXT%=!

echo Name (env vars): %_NAME%

:: This does the trick, and it's waaay better in every possible way
echo Name (bat args): %~d1%~p1%~n1

Output:

e:\Work\Dev\StackOverflow\q029928513>script.bat "e:\Work\Dev\StackOverflow\q029928513\a.bat"
Name (env vars): e:\Work\Dev\StackOverflow\q029928513\a
Name (bat args): e:\Work\Dev\StackOverflow\q029928513\a

e:\Work\Dev\StackOverflow\q029928513>script.bat "c:\dir_bat\a.bat"
Name (env vars): c:\dir_bat\a
Name (bat args): c:\dir_bat\a

e:\Work\Dev\StackOverflow\q029928513>script.bat "c:\dir.bat\a.bat"
Name (env vars): c:\dir\a
Name (bat args): c:\dir.bat\a

e:\Work\Dev\StackOverflow\q029928513>script.bat "c:\dir.batch\a.bat"
Name (env vars): c:\dirch\a
Name (bat args): c:\dir.batch\a

e:\Work\Dev\StackOverflow\q029928513>script.bat "c:\dir.batch\a.bat1"
Name (env vars): c:\dir.batch\a
Name (bat args): c:\dir.batch\a

e:\Work\Dev\StackOverflow\q029928513>script.bat "e:\Work\Dev\StackOverflow\q029928513\ a.bat"
Name (env vars): e:\Work\Dev\StackOverflow\q029928513\ a
Name (bat args): e:\Work\Dev\StackOverflow\q029928513\ a
Sign up to request clarification or add additional context in comments.

4 Comments

To also remove the trailing "\", here's what you can do: code set DIRNAME_=%~dp0 set DIRNAME=%DIRNAME_:~0,-1% code It is a workaround(gainarie) but it works.
thank you! so that all i need to write for this line in the shell script? whats about the BASH_SOURCE[0]? And could you help me with the line BASENAME="${1%.*}" too? i already looked for older posts and the common shell and batch (cmd) commands....
BASH_SOURCE[0] is the name of the script (works when the script is run and sourced).
Here's the code: setlocal enabledelayedexpansion set EXT=%~x1 set SELF=%~1 set BASENAME=!SELF:%EXT%=!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.