Get Batch file to run relative to actual folder, not shortcut

I've created a few batch files that moves files around relative to the folder the batch file is in.

I've added the folder to my Taskbar using Toolbars => New Toolbar..., however now when I run the batch files, the starting position is My Documents (U:), not the actual location of the batch files. Is there any way I can get it to use the actual path?


Solution 1:

Put the following at the top of your batch file:

CD /D %~dp0

Explanation:

  • The /D option tells CD to change current drive as well as current directory for a drive.
  • %0 is the name of the batch file
  • The ~ says we want to use some modifiers
  • d expands to drive letter.
  • p expands to a path.

So %~dp0 is the directory in which the batch file resides.