batch file and xcopy question
-
I'm trying to do some file copying using a batchfile, but am stuck on the issue of relative paths. I want to copy from \source to \dest, wehre is the arbitrary folder the batch file and source folder are located in. If I hardcode an absolute path into the xopy it works fine, but any type of relative path is linked to C:\windows\, and not the location of the batchfile.
-
I'm trying to do some file copying using a batchfile, but am stuck on the issue of relative paths. I want to copy from \source to \dest, wehre is the arbitrary folder the batch file and source folder are located in. If I hardcode an absolute path into the xopy it works fine, but any type of relative path is linked to C:\windows\, and not the location of the batchfile.
What exactly is your question? :confused:
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
I'm trying to do some file copying using a batchfile, but am stuck on the issue of relative paths. I want to copy from \source to \dest, wehre is the arbitrary folder the batch file and source folder are located in. If I hardcode an absolute path into the xopy it works fine, but any type of relative path is linked to C:\windows\, and not the location of the batchfile.
The relative path is relative to the current directory, not the location of the batchfile.
Luis Alonso Ramos Intelectix Chihuahua, Mexico
Not much here: My CP Blog!
-
The relative path is relative to the current directory, not the location of the batchfile.
Luis Alonso Ramos Intelectix Chihuahua, Mexico
Not much here: My CP Blog!
Then how do I get the folder the batchfile is in so I can get the paths correctly? As a last resort I can hardcode the paths into the file, but that would break the .bat any time the user decided that instead of installing to to C:\prog files\default_install_folder\ he wanted it in C:\prog files\Somewhere_else\
-
I'm trying to do some file copying using a batchfile, but am stuck on the issue of relative paths. I want to copy from \source to \dest, wehre is the arbitrary folder the batch file and source folder are located in. If I hardcode an absolute path into the xopy it works fine, but any type of relative path is linked to C:\windows\, and not the location of the batchfile.
dan neely wrote:
I want to copy from \source to \dest, wehre is the arbitrary folder the batch file and source folder are located in. If I hardcode an absolute path into the xopy it works fine, but any type of relative path is linked to C:\windows\, and not the location of the batchfile.
Try one of these two commands...
xcopy sourcedir destdir
xcopy ./sourcedir ./destdirPutting just a slash in the front will mess it up. Jeremy Falcon
-
What exactly is your question? :confused:
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
DavidCrow wrote:
What exactly is your question?
I thought his problem was pretty obvious. :-) Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
The Ultimate Grid - The #1 MFC grid out there! -
Then how do I get the folder the batchfile is in so I can get the paths correctly? As a last resort I can hardcode the paths into the file, but that would break the .bat any time the user decided that instead of installing to to C:\prog files\default_install_folder\ he wanted it in C:\prog files\Somewhere_else\
Run the batch file from the folder containing it. Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
The Ultimate Grid - The #1 MFC grid out there! -
dan neely wrote:
I want to copy from \source to \dest, wehre is the arbitrary folder the batch file and source folder are located in. If I hardcode an absolute path into the xopy it works fine, but any type of relative path is linked to C:\windows\, and not the location of the batchfile.
Try one of these two commands...
xcopy sourcedir destdir
xcopy ./sourcedir ./destdirPutting just a slash in the front will mess it up. Jeremy Falcon
-
Then how do I get the folder the batchfile is in so I can get the paths correctly? As a last resort I can hardcode the paths into the file, but that would break the .bat any time the user decided that instead of installing to to C:\prog files\default_install_folder\ he wanted it in C:\prog files\Somewhere_else\
dan neely wrote:
Then how do I get the folder the batchfile is in so I can get the paths correctly?
Pass the install path as argument to the bat file, eg:
REM mybat.bat
ECHO the install directory is %1call it:
mybat.bat C:\Somewhere_else
-
DavidCrow wrote:
What exactly is your question?
I thought his problem was pretty obvious. :-) Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
The Ultimate Grid - The #1 MFC grid out there!Nishant Sivakumar wrote:
I thought his problem was pretty obvious.
Well, I didn't.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Run the batch file from the folder containing it. Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
The Ultimate Grid - The #1 MFC grid out there! -
I'm trying to do some file copying using a batchfile, but am stuck on the issue of relative paths. I want to copy from \source to \dest, wehre is the arbitrary folder the batch file and source folder are located in. If I hardcode an absolute path into the xopy it works fine, but any type of relative path is linked to C:\windows\, and not the location of the batchfile.
%~dp0
That expands to the path where the batch file is located. So, assuming you drop intoc:\gimlets
a batchfile containing:copy "%~dp0MyFile.txt" "%~dp0\backups\"
..it'll copy
c:\gimlets\MyFile.txt
toc:\gimlets\backups\MyFile.txt
. :) Read up on batchfiles and cmd.exe in the windows help files sometime. It's just about the only useful thing in those helpfiles after all, you might as well get your money's worth...---- Scripts i've known... CPhog 0.9.9 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.1 - printer-friendly forums
-
I assume you meant .\ since the other one is used to flag a command. I did try this, it still does the path relative to the location of cmd.exe (C:\windows\), not the folder the batfile I doubleclicked on is located..
dan neely wrote:
I assume you meant .\ since the other one is used to flag a command.
You are correct. I'm too used to working in Unix for the command line. :laugh:
dan neely wrote:
I did try this, it still does the path relative to the location of cmd.exe (C:\windows\), not the folder the batfile I doubleclicked on is located..
I see what you're saying. Well in that case, as was said already, it uses the CWD. Even running the batch file in the same dir won't work if the CWD isn't set to the dir you need it to be in. What you'll need to do then is pass the directory as a parameter to the batch file so the batch file can resolve the full directory for the xcopy command. Jeremy Falcon
-
The level of user I'm dealing with needs open folder X, double click on the icon level instructions. Not goto start-run, etner cmd.exe, type cd type level instructions.
If that's the case then the CWD should be set already. Instead of using the command prompt, creat a batch file using the
./
and double it's icon in explorer (the batch file should be in the dir where you want your relative starting point to be). Jeremy Falcon -
%~dp0
That expands to the path where the batch file is located. So, assuming you drop intoc:\gimlets
a batchfile containing:copy "%~dp0MyFile.txt" "%~dp0\backups\"
..it'll copy
c:\gimlets\MyFile.txt
toc:\gimlets\backups\MyFile.txt
. :) Read up on batchfiles and cmd.exe in the windows help files sometime. It's just about the only useful thing in those helpfiles after all, you might as well get your money's worth...---- Scripts i've known... CPhog 0.9.9 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.1 - printer-friendly forums
Shog9 wrote:
Read up on batchfiles and cmd.exe in the windows help files sometime. It's just about the only useful thing in those helpfiles after all, you might as well get your money's worth...
Cool, that's exactly what I was looking for. Where is the windows helpfile hidden. Having looked at it before I assumed someone as MS decided writing worthless help was one area they could easily surpass *nix and purged where i found it from my memory.
-
Shog9 wrote:
Read up on batchfiles and cmd.exe in the windows help files sometime. It's just about the only useful thing in those helpfiles after all, you might as well get your money's worth...
Cool, that's exactly what I was looking for. Where is the windows helpfile hidden. Having looked at it before I assumed someone as MS decided writing worthless help was one area they could easily surpass *nix and purged where i found it from my memory.
dan neely wrote:
Where is the windows helpfile hidden.
On XP, it's called the "Help and Support Center", and initially, there's a link to it from the Start menu. Not sure about previous versions of Windows, i don't really remember ever using it.
---- Scripts i've known... CPhog 0.9.9 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.1 - printer-friendly forums
-
%~dp0
That expands to the path where the batch file is located. So, assuming you drop intoc:\gimlets
a batchfile containing:copy "%~dp0MyFile.txt" "%~dp0\backups\"
..it'll copy
c:\gimlets\MyFile.txt
toc:\gimlets\backups\MyFile.txt
. :) Read up on batchfiles and cmd.exe in the windows help files sometime. It's just about the only useful thing in those helpfiles after all, you might as well get your money's worth...---- Scripts i've known... CPhog 0.9.9 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.1 - printer-friendly forums
Well that's so easy we all should've been born knowing that. %~dp0 <--- Who came up with that
-
Well that's so easy we all should've been born knowing that. %~dp0 <--- Who came up with that
-
Shog9 wrote:
Read up on batchfiles and cmd.exe in the windows help files sometime. It's just about the only useful thing in those helpfiles after all, you might as well get your money's worth...
Cool, that's exactly what I was looking for. Where is the windows helpfile hidden. Having looked at it before I assumed someone as MS decided writing worthless help was one area they could easily surpass *nix and purged where i found it from my memory.
Yeah, those are pretty handy. BTW just as %~dp0 returns the full path, %~dpnx0 returns the full path with filename. Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
The Ultimate Grid - The #1 MFC grid out there! -
Well that's so easy we all should've been born knowing that. %~dp0 <--- Who came up with that
:shrug: If you want intuitive, stay clear of the command line. Cryptic incantations with vast, irreversible results - it's the power we're after here, boy! :)
---- Scripts i've known... CPhog 0.9.9 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.1 - printer-friendly forums