Search code examples
batch-fileinputwindows-8

Path of input in batch file


I have a .bat file that I use to convert a xps file into pdf. In order to have a context menu item, i added a shortcut to this .bat file to the "send to" folder.

The .bat file is located in c:\programFiles\myFolder\, and roughly is like this:

@echo off
c:\pathToExe\executable.exe -options -save.as=%1.pdf %1
del %1

executable.exe produces a pdf file with same name as the input xps one. The problem is, it saves it into pathToExe, as it doesn't know the path where the input file is located.

I've tried with pushd and popd, but either I didn't use them properly, or they are used for a different thing (they store/retrieve the folder the .bat file is located).

I'd like to get the input file's folder, so in the script I could use the call as this:

c:\pathToExe\executable.exe -options -save.as=inputFolder/%1.pdf %1

If you consider there's a different, better approach, I'm totally open to hear from it.


Solution

  • %1 should be the whole path to the file if you are using the send to option. It would be no different then dragging and dropping onto the batch file. So technically you are saving it as a double extension as far as I can see. So you can use the command modifiers to get the path and name of the file without the extension.

    c:\pathToExe\executable.exe -options -save.as="%~dpn1.pdf" "%~1"