- Current work directory windows
- Power to Build
- Quicktip: Windows 7 Batch File – Current Working Directory
- Archives
- Windows shell command to get the full path to the current directory?
- 14 Answers 14
- Current work directory windows
- How To Mount Your Current Working Directory To Your Docker Container In Windows
- The Trick
- Example
Current work directory windows
Hi, I am currently following a book about SFML, but I am having a problem with getting the current working Directory. What is wrong with it, I have read other that have had the same problem but I still can’t figure it out.
Here is my program:
What problem do you have?
I get the same results with your function as I do with this one:
There is an overload of strcat_s that handles the sizeof() param if it’s passed an array (but not a pointer) as the first param. So.
What error are you getting? Are you hitting Unicode vs ANSI issues? If so, do what Chervil did and use the -A forms of function (GetModuleFileNameA, PathRemoveFileSpecA)
Or is something else going wrong?
But the right way to get the (current) working directory is to use GetCurrentDirectory(), as shown by Chervil. Your function is returning the directory where the running executable is located, which is not always the working directory. If that’s what you need, give your function another name as it doesn’t behave the same way as the standard CRT getcwd() (sometimes _getcwd() with Microsoft CRT) and GetCurrentDirectory() and could lead to confusion.
And the WinSDK headers supply the define MAX_PATH for use when declaring buffers to hold paths (it’s value is 260.) So you should use that instead.
(You will also see _MAX_PATH being used, which is the corresponding define provided by the Microsoft CRT library. It has the same value.)
There is an overload of strcat_s that handles the sizeof() automatically if the first parameter is an array (but not a pointer.) So
But note that the WinSDK headers provide the define MAX_PATH for in this situation.
It has a value of 260, as does the corresponding Microsoft CRT define _MAX_PATH.
What error are you getting? If it’s a Unicode versus ANSI problem, then do as Chervil did and use the -A entrypoints (i.e. GetModuleFileNameA and PathRemoveFileSpecA.)
Or is something else going wrong?
1. Passing NULL to GetModuleFileName() does the same thing as passing the current module handle as obtained by calling GetModuleHandle(NULL.) So you can replace
(Incidentally, the if() in your code isn’t needed as GetModuleHandle(NULL) will never, ever fail. The current exe must always be there!)
2. As you’re using the Path API already, you could use PathAppendBackslash rather than strcat_s()
3. Your function does not return the (current) working directory, as returned by the CRT call getcwd() or _getcwd() or GetCurrentDirectory(). It returns the directory where the running exe is located, which is not always the case. So your function should be renamed to avoid confusion with the standard calls.
When run from Visual Studio I get
As the default working directory is the project directory but the exe is built into the solution’s debug folder.
Power to Build
Power of building and using beautiful Software
Home » Misc » Quicktip: Windows 7 Batch File – Current Working Directory
Quicktip: Windows 7 Batch File – Current Working Directory
Archives
Windows Current (Working) Directory
When you are running a script in Windows command line (old DOS prompt), you have 2 paths to deal with:
Current (Working) directory and
Path of the script you are running.
Most of the time, they may be the same. Sometimes, you would change working directory to a different path, so you can pick up files/scripts from there. If you ever set up a shortcut to a batch file, you will see a Start in Folder – this corresponds to the current working directory. This can be different from actual path of the script.
To change directory in Windows command line, you can use the CD command. This command affects the current working directory. Windows stores the current working directory in a special variable called %CD%. This variable can be used both on command line and in batch files.
To save the Current working directory, before running a step in a batch file (like calling another script), you can use PUSHD. This simply saves the current path in a stack. Upon return, you can simply do a POPD operation to restore the current working directory.
Windows 7 Default for Current (Working) Directory
Windows 7 sometimes defaults current working directory to C:\Windows\System32 (%Windir%\System32) in command prompt (This incidentally is the path of the cmd.exe). So, if you have some old Batch File scripts that ran fine earlier and suddenly fail to find programs/scripts, chances are, your script is suffering from the above defaulting behavior.
To overcome this, I add the below lines to the top of all my scripts, to force the current
The above snippet forces the current working directory back to the directory where the script is run from. I could have simply said cd /d %
dp0, but I save it in a variable that I can use elsewhere in the script.
Script Arguments and Modifiers
The weird syntax with tilde (
), in the above code snippet, is a special modifier to dissect paths in a Windows Batch File.
If you want to get the full path of a script, you can use the variable %0. For e.g., if you are running a script, c:\dev\test.bat, then
If you want to get parts of this path string, then you would use modifiers. %
p0 refers to the path without the drive letter of the %0.
To get the Drive letter alone, you can use %
d0. This will print C:
You can combine the 2 using %
dp0. This gives the drive letter and path (minus the file name).
(To get fully quality path name use f instead of p).
You can also get just the file name, by using the modifier n and x to get the extension. So to print only name of the script (minus the directory it is in), use %nx0.
%0 refers to the script being run. %1 – %9 are the command line arguments (if any) passed into this scripts. You can use the modifiers on these arguments the same way as well. For e.g., if you have a command like,
%0 refers to process_data.bat
%1 refers to c:\data\datafile.dat
%dp1 would then give us c:\data, the directory in which data file is located and %nx1 will give you datafile.dat.
Unlike the CD command, getting the parts of the path of a script/file can only be done inside the script. These cannot be run on command line as such.
There is a lot more to parameters and modifiers. See here , here , here for details.
Windows shell command to get the full path to the current directory?
Is there a Windows command line command that I can use to get the full path to the current working directory?
Also, how can I store this path inside a variable used in a batch file?
14 Answers 14
Use cd with no arguments if you’re using the shell directly, or %cd% if you want to use it in a batch file (it behaves like an environment variable).
You can set a batch/environment variable as follows:
sample screenshot from a Windows 7 x64 cmd.exe.
Update: if you do a SET var = %cd% instead of SET var=%cd% , below is what happens. Thanks to jeb.
Quote the Windows help for the set command ( set /? ):
Note the %CD% — expands to the current directory string. part.
This has always worked for me:
For Windows we can use
command is there.
For Windows, cd by itself will show you the current working directory.
For UNIX and workalike systems, pwd will perform the same task. You can also use the $PWD shell variable under some shells. I am not sure if Windows supports getting the current working directory via a shell variable or not.
On Windows:
CHDIR Displays the name of or changes the current directory.
In Linux:
PWD Displays the name of current directory.
Based on the follow up question (store the data in a variable) in the comments to the chdir post I’m betting he wants to store the current path to restore it after changeing directories.
The original user should look at «pushd», which changes directory and pushes the current one onto a stack that can be restored with a «popd». On any modern Windows cmd shell that is the way to go when making batch files.
If you really need to grab the current path then modern cmd shells also have a %CD% variable that you can easily stuff away in another variable for reference.
Current work directory windows
Hi, I am currently following a book about SFML, but I am having a problem with getting the current working Directory. What is wrong with it, I have read other that have had the same problem but I still can’t figure it out.
Here is my program:
What problem do you have?
I get the same results with your function as I do with this one:
There is an overload of strcat_s that handles the sizeof() param if it’s passed an array (but not a pointer) as the first param. So.
What error are you getting? Are you hitting Unicode vs ANSI issues? If so, do what Chervil did and use the -A forms of function (GetModuleFileNameA, PathRemoveFileSpecA)
Or is something else going wrong?
But the right way to get the (current) working directory is to use GetCurrentDirectory(), as shown by Chervil. Your function is returning the directory where the running executable is located, which is not always the working directory. If that’s what you need, give your function another name as it doesn’t behave the same way as the standard CRT getcwd() (sometimes _getcwd() with Microsoft CRT) and GetCurrentDirectory() and could lead to confusion.
And the WinSDK headers supply the define MAX_PATH for use when declaring buffers to hold paths (it’s value is 260.) So you should use that instead.
(You will also see _MAX_PATH being used, which is the corresponding define provided by the Microsoft CRT library. It has the same value.)
There is an overload of strcat_s that handles the sizeof() automatically if the first parameter is an array (but not a pointer.) So
But note that the WinSDK headers provide the define MAX_PATH for in this situation.
It has a value of 260, as does the corresponding Microsoft CRT define _MAX_PATH.
What error are you getting? If it’s a Unicode versus ANSI problem, then do as Chervil did and use the -A entrypoints (i.e. GetModuleFileNameA and PathRemoveFileSpecA.)
Or is something else going wrong?
1. Passing NULL to GetModuleFileName() does the same thing as passing the current module handle as obtained by calling GetModuleHandle(NULL.) So you can replace
(Incidentally, the if() in your code isn’t needed as GetModuleHandle(NULL) will never, ever fail. The current exe must always be there!)
2. As you’re using the Path API already, you could use PathAppendBackslash rather than strcat_s()
3. Your function does not return the (current) working directory, as returned by the CRT call getcwd() or _getcwd() or GetCurrentDirectory(). It returns the directory where the running exe is located, which is not always the case. So your function should be renamed to avoid confusion with the standard calls.
When run from Visual Studio I get
As the default working directory is the project directory but the exe is built into the solution’s debug folder.
How To Mount Your Current Working Directory To Your Docker Container In Windows
Apr 23, 2019 · 3 min read
Docker is super handy for anyone wanting to write software in a Linux environment while still running a Windows computer. One of the biggest hurdles I’ve learnt using Docker with Windows is knowing how to mount my working directory to any arbitrary docker container. Instead of anyone else going through the same pain I have, I decided to quickly write up an article to help new Docker users how to do this.
You’d expect that there woul d be an easy answer using either the ADD or VOLUME command to your Dockerfile. The problem with using this method is that you have to rebuild your image every time the parent folder changes. This could be a change because of renaming of your project, or a change from simply working on a new project. This is not ideal. There is instead another way.
The Trick
Instead I’ve found the easiest method is to use the —volume flag when running the docker run command. With this command, you can attach the local directory to your docker container at runtime instead of during the build process.
The —volume command takes a single parameter formatted like so:
where the host directory is the directory you wish to mount and the target directory is the location in the container where this directory is accessible.
There is an additional part to this trick however — you can not use native Windows path formats for your host directory. Instead, you need to convert your path into a quasi-Linux format. The formatting rules (which I’ve noticed so far) are as so:
- You must replace any drives of the format C:\ with the new format //c/ , obviously replacing the C and c with the drive that is to be used.
- Any back-slash is to be replaced with a forward-slash.
I’ve read online that, in theory, it is possible to remove all hard-coded values using the $(pwd) in PowerShell yet I’m unable to get that to work successfully. Writing my path once every few hours is okay enough for me.
Example
It’s easier to understand the trick when put into practice. Here I’ve created a scenario where I would like to mount my current working directory ( C:\Users\kale\my_project ) into the ubuntu:latest image at the /home/project location in the container. We would do that as so:
docker run -it —volume //c/Users/kale/my_project:/home/project ubuntu:latest
This should start up your container and attach your working directory. Now any changes you make locally (i.e. in your Windows machine) will be reflected in your docker container!
As always, thanks for reading. If you liked the article please leave a few claps so other users can find it more easily. Also feel free to give me a follow to stay up to date with any articles I publish.