How do I make any executable dos code auto-detect a path?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
akberali67
Posts: 1
Joined: 25 Dec 2011 12:47

How do I make any executable dos code auto-detect a path?

#1 Post by akberali67 » 25 Dec 2011 12:52

I am writing a code to copy a few files for backup which is sort of everyday work for which I use CD using DOS to set the directory.

How do I create a DOS command line to auto detect the path were I have placed it (the location of code) rather than it automatically getting set to C:\Users\ME.

Sort of like:

CD C:\Users\ME\Ryan\Folder1
COPY *.txt C:\Users\ME\Ryan
CD C:\Users\ME\Ryan\Folder2
COPY *.xls C:\Users\ME\Ryan
{Code placed in C:\Users\ME\Ryan}

CD C:\Users\ME\Ethan\Folder1
COPY *.txt C:\Users\ME\Ethan
COPY *.xls C:\Users\ME\Ethan
{Code placed in C:\Users\ME\Ethan}

So Folder1 & Folder2 remains the same always.
So how can I make dos auto-update the path in the code (C:\Users\ME\Ryan, C:\Users\ME\Ethan) by checking where it exists.

Please help any clues would be appreciable. Thanks!!

aGerman
Expert
Posts: 4740
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How do I make any executable dos code auto-detect a path

#2 Post by aGerman » 25 Dec 2011 13:05

The path (with a trailing backspace) of the batch file can be found in

Code: Select all

%~dp0


The current working directory (without trailing backspace) can be found in

Code: Select all

%cd%


Regards
aGerman

Post Reply