Page 1 of 1

Use Relative path in Batch file?

Posted: 20 Jan 2016 15:09
by NewYears1978
So what I have is a simple batch that copies a file in a folder into all the other folders within that folder.

It works fine, but I want to use relative paths of the batch files location, therefore wherever I put the batch file it will copy the file into all the folders in that location.

Here is my script:

Code: Select all

for /r "S:\PathnameHere\" %%i in (.) do @copy "S:\PathnameHere\Atari 7800.psd" "%%i"


I tried replacing my paths with %%~dp0 but I guess my syntax was incorrect.


Something I wanted to add too but had no clue how is that when it copies the file it renames it to the name of the folder.

So say I have File1.ext and runt he above batch when it copied File1.ext into folder "Folder1" it would rename the file to "Folder1.psd" (keeping extension)

Thanks in advance.

Edit:
I figured the rename part out

Code: Select all

for /r %%a in (*.psd) do for %%b in ("%%~dpa\.") do ren "%%~a" "%%~nxb%%~xa"


Just need relative paths part on my first code ;)

Re: Use Relative path in Batch file?

Posted: 20 Jan 2016 16:19
by kwsiebert
For your first part, the closest solution to what you already have would be

Code: Select all

for /r %%i in (.) do copy "S:\PathnameHere\Atari 7800.psd" "%%i\"

for /r already defaults to the current directory if you leave one out, but using it with (.) as you did leaves a trailing '.' at the end of each folder name. Adding the trailing \ takes care of this. This will also copy the file into directory the batch is run from, and not just its sub directories. If you wanted to avoid this and only copy to sub directories, try

Code: Select all

for /d /r %%i in (*) do copy "S:\PathnameHere\Atari 7800.psd" "%%i\"


I tried replacing my paths with %%~dp0 but I guess my syntax was incorrect.

You don't use double % inside a batch for that, though I can understand the confusion. It's just %~dp0, if you really wanted to specify. You may want to get in the habit of explicitly setting the directory you are running in - it will always be the batch file's own location if you ran it directly, but if you launched it by drag and drop, Send To, or an extension association, it will be the location of the file that was passed.

Re: Use Relative path in Batch file?

Posted: 21 Jan 2016 01:38
by thefeduke
To add the d and p modifiers to the variable %%A one can use %%~dpA, as you have done.
Similarly, to modify the variable %~0, one would use %~dp0, rather than %%~dp0.
John A.

Re: Use Relative path in Batch file?

Posted: 21 Jan 2016 10:22
by NewYears1978
Thanks guys, I will try it out.

I should have mentioned I am not a coder of any type so all of this stuff for me is lots of googling and figuring things out :)

So, Is should be able to use %~dp0 for the second path as well? The file is alwasy located where I place the bat file. I will try it out after work and see what happens :)

Edit://

So I tried it out, but I don't understand how to use the variable %~dp0 with a filename, I don't know the syntax.

like %~dp0\Filename.ext (I know this is wrong, just showing what I meant.

I thought it would work as follows, but it says the syntax is wrong:

Code: Select all

for /d /r %%i in (*) do copy "Atari 7800.psd" "%%i\"


I also tried

Code: Select all

for /d /r %%i in (*) do copy "%~dp0\Atari 7800.psd" "%%i\"

Re: Use Relative path in Batch file?

Posted: 21 Jan 2016 10:59
by kwsiebert
Is there anything else in your batch file? Both of the lines you posted give the correct results when I test them myself.

Re: Use Relative path in Batch file?

Posted: 21 Jan 2016 11:57
by NewYears1978
kwsiebert wrote:Is there anything else in your batch file? Both of the lines you posted give the correct results when I test them myself.


Nope just that, and it says The syntax is wrong.

I tried it again and it's working...I must have had an extra character in there :)

Yay! Thanks all.