# need batch help



## flyhigh427 (Oct 22, 2008)

hi im trying to write a program to find the dir my bat file is in
then copy a file from that directory to another directory this 
is all ive got so far.

for /f %i in ("%0") do set curpath=%~dpi



am i on the right track
thanks


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

Close. It's much easier though: *%~dp0* will get the drive and path to your batch file.

So just use *copy %~dp0\FileToCopy.ext C:\Destination*

Also, in a batch file you'll need to double the percent signs for the loop variable, so use %%i in a batch, %i on the command line.

Using modifiers on a loop variable can give unexpected results. If the variable doesn't have a valid drive and path, dp will return the current drive and path. And %0 shows the batch file *as called*.
Example, you have a folder for batch files that you've added to the path statement so you can run them from anywhere named c:\Scripts
You have a batch file named mybatch.bat in *C:\Scripts* with this for loop:
*For /f %%i in ("%0") do Echo %%~dpi*

You are in a test folder *c:\test*
You call your batch file by typing just *mybatch*
%0 will be *mybatch*
%%i will be *mybatch*
%%~dpi will be *C:\test\*, not *C:\Scripts\*
%~dp0 will always be *C:\Scripts\*

HTH

Jerry


----------



## flyhigh427 (Oct 22, 2008)

hi there to make the bat file find the directory its in then copy a needed file from that folder to c:\ these lines wont work 

For /f %%i in ("%0") do Echo %%~dpi
copy %~dp0\findcomp.avi C:\findcomp.avi

ive tryed many different ways what am i doing wrong?
thanks


----------



## TheOutcaste (Aug 8, 2007)

You should only need this:
*copy %~dp0\findcomp.avi C:\*

You don't need to specify the filename for the destination, unless you are changing it, but it should work either way.
The For loop is not needed.
You can put *Echo %~dp0* in the file to display the path it will use.
Note that this *HAS* to be in a batch file, you can't use the batch variables at the command line.
What error do you get?

Jerry


----------



## flyhigh427 (Oct 22, 2008)

system cant find file


----------



## TheOutcaste (Aug 8, 2007)

If this comes after the copy command is displayed it means *findcomp.avi* is not in the same folder as the batch file.

When you execute the file, the *copy %~dp0\findcomp.avi C:\* command should display as
*copy C:\Scripts\findcomp.avi C:\*
Assuming the batch file is in the *C:\Scripts* folder that is.

If you have an Echo off command at the start of the file, comment it out so you can see each line.

If you do a dir of the folder with the batch file, can you see both files?


----------



## flyhigh427 (Oct 22, 2008)

ok here my problem i need my bat file to find out what directory it is in
then copy a file from that folder to c:\ drive but i keep failing can someone point me in the right direction please 
thanks


----------



## flyhigh427 (Oct 22, 2008)

even this wont work

copy c:\documents and setting\hello\hello.exe c:\

im using windows xp


----------



## TheOutcaste (Aug 8, 2007)

flyhigh427 said:


> even this wont work
> 
> copy c:\documents and setting\hello\hello.exe c:\
> 
> im using windows xp


Anything with spaces must be quoted, and you are missing a letter:

* copy "c:\documents and settings\hello\hello.exe" c:\*
The location of the quotes is not always important, so this will also work:
* copy c:\"documents and settings"\hello\hello.exe c:\*
However, the quotes can not surround two elements, so this will not work:
* copy "c:\documents and settings\hello\hello.exe c:\"*
Copy will see this as the source: *"c:\documents and settings\hello\hello.exe c:\"* which is invalid, and will not see a destination.

The current directory is stored in the *CD* variable so if your batch file is in C:\Scripts, and C:\Scripts is in the path
you are in a command window in c:\documents and settings\hello
%~dp0 will be *C:\Scripts\*
%CD% will be *C:\Documents and Settings\hello*

Note that %~dp0 will always have a trailing slash
%CD% does not have a trailing slash unless you are in the root directory,
So if the current directory is c:\, %CD% will be *c:\*
The built in help has a lot of good info:
Set /?
For /?
If /?
Call /?
cmd /?

Some useful DOS and VB Scripting Links
Command-line reference A-Z
Using batch parameters
Windows 95/98/ME Batch file Tutorial (Still a good basic reference for WinNT/2K/XP)
Batch File Functions for NT4/2000/XP/2003
Rob van der Woude's Scripting Pages

HTH

Jerry


----------

