# How to change working directory for calculation and save

Hello Mathematica users,

I have problem to set working directory, I want to calculate and save my data on D, but down you can see that mathematica shift automatically on old C. How to use D partition for calculating if mathematica is installed on C?

If I do calculation as this, file is saved on D but first it is saved on C and on C I don't have enough space.

  In[7]:= ResetDirectory[]

"C:\\Users\\user\\Documents"

In[8]:= DirectoryStack[]

Out[8]= {}

In[10]:= SetDirectory["D:\\"]

Out[10]= "D:\\"

A:=5;

Export["A.wdx", A];

In[13]:= DirectoryStack[]

{"C:\\Users\\user\\Documents"}

-
What is the value of $TemporaryDirectory? – celtschk May 2 '12 at 21:34 Can you change SetDirectory["D:\\"] to SetDirectory[$TemporaryDirectory] and rerun the whole program? –  qazwsx May 2 '12 at 21:43
As reality check, have a look if and where "A.wdx" is in fact saved...? –  Yves Klett May 3 '12 at 9:43

The exact code (I also happen to have a D partition:) runs fine on my computer (Win7, mma 8). The file is created in D:\, just the output of DirectoryStack is misleading. I don't really know, why it doesn't include D:\

You can check your current working directory using Directoy.

ResetDirectory[]

(* ==> "E:\\Projekti\\MD\\SCRIPTS\\mathematica\\FitSpectra" *)

DirectoryStack[]

(* ==> {"C:\\Users\\ajasja\\Documents"} *)

SetDirectory["D:\\"]

(* ==> "D:\\" *)

A := 5;

Export["A.wdx", A];

DirectoryStack[]

(*
==> {"E:\\Projekti\\MD\\SCRIPTS\\mathematica\\FitSpectra", \
"C:\\Users\\ajasja\\Documents"}
*)

Directory[]

(* ==> "D:\\" *)

-

The Documentation page for ResetDirectory says under "MORE INFORMATION" field:

Successive calls to ResetDirectory yield earlier and earlier current directories.

ResetDirectory uses the directory stack given by DirectoryStack[].

ResetDirectory removes the last element from the directory stack, and makes the second-to-last element current.

Each call to SetDirectory prepends one element to the directory stack; each call to ResetDirectory drops one.

So it is not surprizing that DirectoryStack[] initially is a blank list. The call to SetDirectory adds to this list one element: previous working directory. To get the current working directory you should evaluate Directory[].

-