Pass variable file path to batch file?

Posted on 2007-11-15
Last Modified: 2012-08-13
I need some help passing a variable to a batch file, specifically the path of a file.  My batch file looks something like this:
net use lpt2:  serverdesignjet
copy /b filename.plt lpt2
net use lpt2: /delete

It would be great if users could just drag a .plt on to this batch file and have it run.  Currently the have to move the .plt file and rename it, which as you can imagine isn't very fast.

Question by:KPS2007
  • 3
  • 2
LVL 30

Accepted Solution

SteveGTR earned 100 total points
ID: 20292091
Could do this:
@echo off
set fileName=%~1
if "%~1"=="" set /p fileName=Please enter file name to processing: 
if "%fileName%"=="" echo Processing aborted&goto :EOF
if not exist "%fileName%" echo File does not exist&pause&goto :EOF
net use lpt2:  serverdesignjet
copy /b "%fileName%"  lpt2
net use lpt2: /delete 

Open in new window


Expert Comment

ID: 20292134
SteveGTR: are  you loving' this new 'code-snippett' feature or what? ;0)
LVL 30

Expert Comment

ID: 20292150
Yes I am.

Although it does appear that some users attempt to paste the line numbers with the code. You can do it by starting the drag at the numbers. One user insisted it was a bug and asked if I would repost the code in a message.
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.


Author Closing Comment

ID: 31409418
This snippet helped me get it running the way I like, thanks!

Expert Comment

ID: 20292185
ROFL, that cracks me up ... and so you can!  Perhaps EE could consider rendering them in a way that they can't become part of copy/paste op (or even provide a download this as a "TEXT" file button).
LVL 30

Expert Comment

ID: 20292374
That's a good idea. Because if one user can do it you know that more users can.

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question