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.
%~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
-
Hey it's something you'll never forget. You can't forget something you never were able to remember in the first place. :-/
Good point :-O
-
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
Click on the desktop and press F1 is the way I get at it, then search for 'for' or 'if', to get to the command processor reference.