locked
How2 change working directory? RRS feed

  • Question

  • How do you change the working directory from within a program in small basic?

    i.e "cd d:\data"

    • Edited by jwzumwalt1 Friday, March 30, 2018 9:37 PM
    Friday, March 30, 2018 7:44 AM

Answers

  • The working directory (Program.Directory) is where the *.sb file is located, so no you cannot change this from the program.

    You can set another variable to a variety of folder locations in all sorts of ways with extensions or not (as per Pappa's suggestions), but not change the Program.Directory which is exclusively where the progam currently is.

    Perhaps give more details of what you are wanting to achieve and we can help make further suggestions.

    Friday, March 30, 2018 11:06 PM

All replies

  • Working directory (Program.Directory) is the location where the entry assembly (yor currently executed SB program.exe) is saved/executed.

    To set the working directory for an external application (like notepad) when run from LDProcess.Start you can use

    LDProcess.Start("path\to\np.lnk", ""), where np.lnk  is a shortcut link to eg. notepad.exe and set the 'working directory'/'start from'  in the link properties to a directory path eg. C:\Temp, to start notepad with working directory.

    For cmd.exe to change its working directory you could similar use:

    LDProcess.Start with a cmdline like:

    cmd.exe /d /k cd /d "directoryPath"

    or

    cmd.exe /d /k cd /d "directoryPath" && "exe/batPath"

    or

    cmd.exe /d /c start "" /d directoryPath exePath

    About the right arguments see the cmd/cd/start /?   help in the console.
    Friday, March 30, 2018 12:07 PM
    Answerer
  • Thank you for taking the time to explain the LD extension use.

    Is there a way to change the working directory within the program without an extension?

    Friday, March 30, 2018 10:52 PM
  • The working directory (Program.Directory) is where the *.sb file is located, so no you cannot change this from the program.

    You can set another variable to a variety of folder locations in all sorts of ways with extensions or not (as per Pappa's suggestions), but not change the Program.Directory which is exclusively where the progam currently is.

    Perhaps give more details of what you are wanting to achieve and we can help make further suggestions.

    Friday, March 30, 2018 11:06 PM
  • Why not launch your program from a shortcut link, with the directory path set under the shortcut properties (like described above)?

    Example program, following oneliner:
    TextWindow.WriteLine(File.GetFiles("."))

    If started from a shortcut link, with 'Ausführen in' (de-de, in engl. something like 'start from' or 'working directory' or similar on the properties tab) set to C:\Temp, will list all the files in the C:\Temp base directory.

    If you need this for the 'File' object, then note, that all methods there with access to the file system also support:

    * Environment variables %envvar% like %APPDATA% etc. in their paths,

    as well as

    * relative paths like   . currentDir , .. parentDir , \  root.


    Else, perhaps you could explain a bit closer, what you are trying to achieve.
    Friday, March 30, 2018 11:16 PM
    Answerer
  • It is often advantages to switch to the actual directory to perform certain functions without the risk or overhead of defining the whole path. For those accustom to working with DOS and Linux/Unix, this is called the working directory.

    The startup directory is not sufficient. For example the program may need to enter "dir1" and perform certain functions then move to "dir2".

    DOS, Win, and Linux/Unix use the CD command to accomplish this.

    Someone has marked 6 of the answers in this question as "correct"... none of them are.

    The answers are providing a "startup" directory not a "working" directory; there is a difference.




    • Edited by jwzumwalt1 Sunday, April 8, 2018 3:34 AM
    Sunday, April 8, 2018 3:31 AM
  • Is there a way to change the working directory within the program without an extension?

    However, if you allow a tiny extension (4kB), you can use this as a single assembly or from LDInline:

    /*
    see https://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx
    C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe /nologo /t:library /r:"C:\Program Files (x86)\Microsoft\Small Basic\SmallBasicLibrary.dll" /doc:SBCurDir.xml SBCurDir.cs
    */
    using System;
    using System.IO;
    using Microsoft.SmallBasic.Library;
    
    /// <summary>CurrentDirectory SB Extension</summary>
    [SmallBasicType]
    public static class SBCurDir
    {
    		/// <summary>Get/Set the full path of the current working directory (w/o trailing '\') for the current application.</summary>
    		public static Primitive CurrentDirectory
    		{
    			get { return Environment.CurrentDirectory; }
    			set {
    				string fullPath = Environment.ExpandEnvironmentVariables(value);
    				if (Directory.Exists(fullPath) && Path.IsPathRooted(fullPath))
    					Environment.CurrentDirectory = fullPath.TrimEnd(Path.DirectorySeparatorChar);
    				}
    		}
    }

    Sunday, April 8, 2018 1:24 PM
    Answerer