Skip to main content
Engineering LibreTexts

2.4: Renaming, Copying and Deleting Files and Directories

  • Page ID
    43795
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Files may be copied and renamed using the messages copyTo: and renameTo:. Note that while copyTo: tasks as argument another fileReference, renameTo: takes a path, pathname or reference.

    | working |
    working := FileSystem disk workingDirectory.
    working / 'foo.txt' writeStreamDo: [ :stream | stream nextPutAll: 'Hello World' ].
    working / 'foo.txt' copyTo: (working / 'bar.txt').
    
    | working |
    working := FileSystem disk workingDirectory.
    working / 'bar.txt' readStreamDo: [ :stream | stream contents ].
        → 'Hello World'
    
    | working |
    working := FileSystem disk workingDirectory.
    working / 'foo.txt' renameTo: 'skweek.txt'.
    
    | working |
    working := FileSystem disk workingDirectory.
    working / 'skweek.txt' readStreamDo: [ :stream | stream contents ].
        → 'Hello World' 
    

    Directory creation. To create a directory, use the message createDirectory as follows:

    | working |
    working := FileSystem disk workingDirectory.
    backup := working / 'cache-backup'.
    backup createDirectory.
    backup isDirectory.
        → true
    backup children.
        → #()
    

    Copy everything. You can copy the contents of a directory using the message copyAllTo:. Here we copy the complete package-cache to the backup directory using copyAllTo::

    cache copyAllTo: backup.
    

    Note that before copying the target directory is created if it does not exist.

    Deleting. To delete a single file, use the message delete:

    (working / 'bar.txt') delete.
    

    To delete a complete directory tree (including the receiver) use deleteAll. Be careful to not delete the wrong folder though.

    backup deleteAll.
    

    This page titled 2.4: Renaming, Copying and Deleting Files and Directories is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.