Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Get current directory

Alessandro_D_
새로운 기여자 I
10,867 조회수

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 포인트
1 솔루션
Steve_Lionel
명예로운 기여자 III
10,737 조회수

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.

원본 게시물의 솔루션 보기

19 응답
FortranFan
명예로운 기여자 III
10,851 조회수

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
0 포인트
FortranFan
명예로운 기여자 III
10,850 조회수

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

0 포인트
Steve_Lionel
명예로운 기여자 III
10,842 조회수

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

 

0 포인트
jimdempseyatthecove
명예로운 기여자 III
10,835 조회수

You could try

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

Then open "HereIAm.txt" and read it.

Jim Dempsey

GVautier
새로운 기여자 III
10,811 조회수
0 포인트
FortranFan
명예로운 기여자 III
10,799 조회수
Why not use getcwd function?

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

0 포인트
Steve_Lionel
명예로운 기여자 III
10,794 조회수

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_
새로운 기여자 I
10,752 조회수

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

0 포인트
mecej4
명예로운 기여자 III
10,743 조회수

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:\
0 포인트
JohnNichols
소중한 기여자 III
10,736 조회수

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

0 포인트
Steve_Lionel
명예로운 기여자 III
10,738 조회수

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.

jimdempseyatthecove
명예로운 기여자 III
10,679 조회수

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

0 포인트
Norman_K_
새로운 기여자 I
10,712 조회수

What Steve said but with

close(i, status="delete")
GVautier
새로운 기여자 III
10,707 조회수

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.

0 포인트
David_Billinghurst
새로운 기여자 III
9,330 조회수

You must also find an unused unit number to avoid side effects.

The Fortran 2008 NEWUNIT specifier on the open statement avoids this issue.  It automatically chooses an unused (negative) unit number.

 

0 포인트
David_Kinniburgh
초급자
6,670 조회수

This solution assumes that INQUIRE returns the full path. As discussed before, the standard merely requires that the processor returns a name that will find the correct path. While Intel compilers do return the full path, gcc and NAG, for example, do not and so in these cases, this routine returns a blank result. I do not understand why all compilers do not return a full path as this would provide a neat solution to what can be a tricky problem. Is it simply backward compatibility or a Fortran desire to avoid getting involved in any details of the operating system? Such a change would not require a change in the standard.

PS Adding status='DELETE' to the CLOSE statement should get rid of the temporary file.

0 포인트
Steve_Lionel
명예로운 기여자 III
6,613 조회수

There can be more than one "full path" to the same file, and some platforms may not have the ability to return the full path for an opened file. The standard requires that the returned string be suitable for use in an OPEN statement. 

0 포인트
Pathan
초급자
5,863 조회수

Please try this simple solution

use msflib
use portlib
.
.
.
integer * 4 iostat
iostat=getcwd(fil)
.
.
.
 
0 포인트
응답