Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file - get file name without extension from passed parameter

Tags:

batch-file

i am trying to create a batch file to run:

.tex --> .dvi : latex filename.tex  
.dvi --> .ps  : dvips -o filename.ps filename 
.ps --> .pdf  : ps2pdf filename.ps

I tried:

latex %1
dvips -o %~n%1.ps %n%1
ps2pdf %~n%1.ps

assuming that ~n will give me the file name without the extension of a passed file. However, it doesn't work (apart from the first line). Does anyone know the correct version?

like image 352
Felix Avatar asked Jun 03 '10 22:06

Felix


People also ask

What is @echo off in batch script?

When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.

What is %% A in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %% P in batch file?

The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.

What is %~ dp0 in batch script?

The %~dp0 (that's a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file. The variables %0-%9 refer to the command line parameters of the batch file. %1-%9 refer to command line arguments after the batch file name. %0 refers to the batch file itself.


2 Answers

Ok, some trial and error gave me

latex %1 
dvips -o %~n1.ps %~n1
ps2pdf %~n1.ps

which does the trick

like image 158
Felix Avatar answered Nov 25 '22 17:11

Felix


  1. You don't need to specify the extension when executing latex and dvips.
  2. But ps2pdf needs the extension.
  3. The following works because I use this everyday. Trust me!
rem this is a batch file, named mybatch.bat
rem it takes a filename without extension

echo off

latex %1
dvips %1
ps2pdf %1.ps
like image 41
xport Avatar answered Nov 25 '22 16:11

xport