Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Get current directory

Alessandro_D_
New Contributor I
1,004 Views

Dear users,

I would like to get the name of the current directory in Fortran. I tested the following code but it doesn't work:

program test
implicit none
character(len=128) :: pwd
call get_environment_variable('CD',pwd)
print *, "The current working directory is: ",trim(pwd)
pause
end program

The original example was call get_environment_variable('PWD',pwd) but since I am on Windows I replaced PWD with CD. But even then, it doesn't display anything

 

 

0 Kudos
1 Solution
Steve_Lionel
Black Belt Retired Employee
875 Views

Visual Studio considers the folder with the .vfproj file to be the "current directory". You can change this with the "Start in" property on the Debug property page. But I agree that you should not depend on this, as if you run the program from outside VS, it will be different.

Instead, I suggest that you prompt the user for where to store the files. Windows has the GetSaveFileName API for this purpose.

View solution in original post

15 Replies
FortranFan
Honored Contributor II
988 Views

If I recall correctly, "CD" (or the alternate "CHDIR") are pseudo environment variables on Windows OS and as such, you may not be able to fetch the value via Fortran intrinsic 'get_environment_variable' on this OS.

A safer bet is the Windows API 'GetCurrentDIrectory':

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory

If portability across different Fortran compilers is not a concern, you can consider using Intel Fortran solution via their provided Fortran module 'KERNEL32':

   use ifwin, only : GetCurrentDirectory
   character(len=256) :: CurrentDir
   integer :: ilen
   ilen = GetCurrentDirectory( len(CurrentDir), CurrentDir )
   print *, "Current directory: ", trim(CurrentDir)
end
FortranFan
Honored Contributor II
987 Views

Oops, I meant 'use kernel32, only : GetCurrentDirectory' but 'use ifwin ..' should also work.

Steve_Lionel
Black Belt Retired Employee
979 Views

Neither CD nor PWD are environment variables automatically created by Windows. Instead, they are commands.

 

jimdempseyatthecove
Black Belt
972 Views

You could try

result = SYSTEMQQ("cd>HereIAm.txt")

Then open "HereIAm.txt" and read it.

Jim Dempsey

GVautier
New Contributor II
948 Views
FortranFan
Honored Contributor II
936 Views
Why not use getcwd function?

It's a vendor-specific solution that may further constrain portability.

Steve_Lionel
Black Belt Retired Employee
931 Views

First, I'll ask why you need this. Perhaps there's a better solution.

Second, I'll propose the following standard code (it does leave the file around, though):

 

implicit none
character(256) :: filespec
integer :: i
open (newunit=i,file="dummy.xyz",status='unknown')
inquire (unit=i,name=filespec)
close (i)
i = index(filespec,"dummy.xyz",back=.true.)
filespec = filespec(1:i-1)
print *, trim(filespec)
end

 

  

Alessandro_D_
New Contributor I
889 Views

Thanks for your answer! Indeed it is probably better if I explain what I would like to do. I want to write all the results generated by my program in some txt files stored in a subfolder called "output". The structure of my project folder is the follwing

Main folder contains these subfolders:

--> Console1, where sln,vproj files are located (under Console1, there are Debug and Release)

--> output, where I want to store txt files generated by Fortran

--> src, where I store the source .f90 files

Then in my code I define a character with the path to the "output" folder i.e.

character(len=10) :: savedir

and I assign it as 

savedir='..\output\'

Last, if I want to save the array x to the file x.txt and store x.txt in the folder "output", I write

open(15,file=savedir//'x.txt',iostat=ierr,status='replace')
if (ierr/=0) call myerror("cannot open file")
write(15,*) x
close(15)

The key part is to specify savedir correctly. This is why I wanted to know what folder is considered as current folder by visual studio

mecej4
Black Belt
880 Views

A word of caution is in order.

An EXE may be run using the command processor or a batch file, and may be invoked by another program. This other program may be your program or a program such as VS, and may itself have been called by using a long pathname. Part of the path prefix may not be explicit, but obtained by using %PATH% or other environmental variables.

Therefore, it is incorrect to assume that there is a unique current directory for a given EXE. To illustrate this, we may run Steve L's program twice, once from the directory containing the EXE, and then from the parent directory.

S:\LANG>ifx /Od /MD showdir.f90
S:\LANG>showdir
 S:\LANG\
S:\LANG>cd ..

S:\>lang\showdir
 S:\
JohnNichols
Valued Contributor II
876 Views

A simpler way is to use a small subroutine in C# and use its standard function and simply return the name. 

Steve_Lionel
Black Belt Retired Employee
876 Views

Visual Studio considers the folder with the .vfproj file to be the "current directory". You can change this with the "Start in" property on the Debug property page. But I agree that you should not depend on this, as if you run the program from outside VS, it will be different.

Instead, I suggest that you prompt the user for where to store the files. Windows has the GetSaveFileName API for this purpose.

View solution in original post

jimdempseyatthecove
Black Belt
816 Views

Generally, excepting during development, it is a bad Idea to place your data files in some place in your development folders.

If the program is launched with doubleclick/click,open you have no assurances of what the current directory is.

It may be best to require passing the input and output folder(s) and/or file names as command line arguments. Note, launched with doubleclick/click,open it would have no arguments, and to which you could display a complaint and exit.

You can easily specify these in the Debug properties for development.

From command line:

CD \YourDesieredWoekingFolder
YourProgram .\input .\output arg arg arg

or

YourProgram "\This is your input folder" "\And your output folder" arg arg arg

Jim Dempsey

Norman_K_
New Contributor I
849 Views

What Steve said but with

close(i, status="delete")
GVautier
New Contributor II
844 Views

You must test if the file already exists before opening and delete it only if not. You must also find an unused unit number to avid side effects. It's somewhat complicated.

Furthermore, that method will fail if the current drive/directory is readonly.

Reply