0

I have a bat script in a folder called scr

/scr/
    script.bat
    test.txt 
    /folder/
        testinfolder.txt

When I want to copy test.txt which is in the same folder as the script, I use %~dp0 like this

copy %~dp0test.txt test.txt 

What if I want to copy testinfolder.txt which is not in the same folder as the running script, but is one level deeper inside folder. How can I copy that file? I tried this, but it didn't work.

copy %~dp0/folder/testinfolder.txt testinfolder.txt
samquo
  • 757
  • 7
  • 21
  • You should use `\ ` instead of `/`. Most of Windows will transparently translate `/` for you, but `cmd` is a major exception with built-in commands – since `/` is used there for command options. – Joey Dec 15 '10 at 17:48

1 Answers1

2

How about

copy %~dp0folder\testinfolder.txt testinfolder.txt
Raghuram
  • 51,854
  • 11
  • 110
  • 122
  • Hmm! worked for me... Could this (http://stackoverflow.com/questions/232651/why-the-system-cannot-find-the-batch-label-specified-is-thrown-even-if-label-ex) be the issue? Or this (http://stackoverflow.com/questions/1522129/windows-batch-file-the-system-cannot-find-the-batch-label-specified)? – Raghuram Dec 15 '10 at 12:37
  • confirm, `%~dp0` already contains backslash; @Raghuram: shouldn't it be "%~dp0folder`/`testinfolder.txt"? – barti_ddu Dec 15 '10 at 12:42
  • @barti_ddu: %~dp0 contains trailing \. File separator is / in Unix \ in Windows/DOS – Raghuram Dec 15 '10 at 12:47
  • @samquo - you could try creating a fresh batch file with just the relevant line and try if that works. – Raghuram Dec 15 '10 at 12:49
  • @Raugharm: heh, yes, of course; my addiction to bash on windows is too strong :) – barti_ddu Dec 15 '10 at 12:59