22
\$\begingroup\$

Everybody has heard of the phrase "be there or be square". So I thought it was time for a challenge about it.

Input

You will take a full absolute directory address as text as input via STDIN or equivalent.

Output

If the directory address exists and is valid, your program will move itself to that folder on your computer. If not, it will output via STDOUT or equivalent the following square:

+-+
| |
+-+

Requirements

  • Standard loopholes are disallowed.
  • You may output a single trailing newline if unavoidable.
  • Your program must produce no output if it has been moved.
  • Your program must be able to run again wherever it has been moved to.

Assumptions

  • You may assume that the input is never empty.
  • You may assume the directory never has a filename appended to the end.
  • You may assume that the input directory is never the current one.
  • You may assume you have permissions to write and execute everywhere.

This is , so shortest answer in bytes wins.

\$\endgroup\$
8
  • \$\begingroup\$ Can we hardcode the filename? \$\endgroup\$
    – BookOwl
    Commented Jan 22, 2017 at 20:27
  • \$\begingroup\$ Yes @bookowl you may \$\endgroup\$
    – FinW
    Commented Jan 22, 2017 at 20:29
  • \$\begingroup\$ Does the path include the filename (c:\users\Joe\program.txt) or not (c:\users\Joe\ )? In the latter case, must the name of the created file be the same as the source? \$\endgroup\$
    – Luis Mendo
    Commented Jan 22, 2017 at 21:33
  • \$\begingroup\$ @LuisMendo you will never get a file as input only a directory \$\endgroup\$
    – FinW
    Commented Jan 23, 2017 at 17:36
  • \$\begingroup\$ @FinW Sadly, you still haven't answered my question: must the name of the created file be the same as the original file? \$\endgroup\$
    – Luis Mendo
    Commented Jan 23, 2017 at 17:37

10 Answers 10

7
\$\begingroup\$

PowerShell, 59 62 61 60 bytes

$z=ls($d=$args)-di;('"+-+
| |
+-+"','mv b.ps1 "$d"')[$?]|iex

Try it online!

Explanation

PowerShell's Move-Item cmdlet (mv) renames a file as well, so giving it a directory that doesn't exist as the destination just renames the file to that last component instead (as long as the parent exists), so that was no good.

I could use Test-Path to determine that the destination exists and is a directory, but it's too long Test-Path $d -PathT C.

So instead I'm using Get-ChildItem (ls) with the (shortened) -Directory parameter, and checking $? to see if it was successful. The output if there is any is assigned to $z so that it isn't seen.

That's done in the form of an array with 2 elements, then indexing into the array with the boolean value of $?, which will get coalesced to 0 or 1, so the first array element is chosen if the destination directory doesn't exist, and second if it does.

The first array element is a string containing the box (quoted); newlines are allowed in strings, even when they're not heredocs. The second element is a string containing the move command.

The result of that array selection gets piped into Invoke-Expression (iex) to be executed. This is necessary because of I just leave the actual move command in the array, it gets executed no matter what (to populate the array), which defeats the purpose.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ The only reason this doesn't work on TIO is because the script is called .code.tio.ps1, not b.ps1. This works. There seems to be some stray output to STDOUT though. Not sure what causes that. \$\endgroup\$
    – Dennis
    Commented Jan 22, 2017 at 20:50
  • \$\begingroup\$ Wow awesome! The stray output was from gci, my test directories didn't exhibit that because they contained no child items, so this exposed that flaw. It's fixed now by just assigning the output. Thanks @Dennis ! \$\endgroup\$
    – briantist
    Commented Jan 22, 2017 at 20:55
  • \$\begingroup\$ I think ls is a shorter abbreviation for gci \$\endgroup\$
    – anna328p
    Commented Jan 23, 2017 at 6:09
  • 1
    \$\begingroup\$ @wat duh, it sure is! I use ls all the time and then in golfing I tend to forget it. Thank you. \$\endgroup\$
    – briantist
    Commented Jan 23, 2017 at 15:10
  • \$\begingroup\$ Nice. I think this is the first time I've seen $? used in golfing here. \$\endgroup\$ Commented Jan 25, 2017 at 18:27
5
\$\begingroup\$

Octave, 60 57 52 bytes

8 bytes saved thanks to @Stewie

if~movefile('f.m',input(''))disp("+-+\n| |\n+-+")end

This is a script that lives within a file called f.m. When run, it will prompt the user for the folder to move the file to. If the folder does not exist and the move operation fails, then movefile returns false (or 0) otherwise it returns true (or 1). If it's false, we display the string "+-+\n| |\n+-+".

\$\endgroup\$
1
4
\$\begingroup\$

Batch, 80 bytes

@if not "%1"=="" move %0 %1\>nul 2>nul&&%1\%~nx0||(echo +-+&echo ^| ^|&echo +-+)

Batch doesn't like it if you move the batch file as it's running, so by invoking %1\%~nx0 (which is the new name of the batch file) Batch stops looking for the old batch file.

\$\endgroup\$
4
\$\begingroup\$

Bash + coreutils, 54 bytes

if [ -d $1 ];then mv $0 $1;else echo "+-+
| |
+-+";fi

Simple enough. It does a test to see if the first argument exists, if it exists the program moves itself into the argument, else prints the square.

Does not work on Windows, however it works on Bash on Ubuntu on Windows / WSL. Does not take a drive letter, however I think that has been clarified to be OK.

This account is owned by Mendeleev.

\$\endgroup\$
1
  • \$\begingroup\$ I think you need echo -n so that you don't print a trailing newline. \$\endgroup\$ Commented Jan 24, 2017 at 7:36
2
\$\begingroup\$

Bash + coreutils, 43 42 bytes

mv -t $1 $0 2> >(:)||echo -n '+-+
| |
+-+'

This avoids printing the trailing newline via the -n flag to echo.

I'm not quite sure what the OP means by allowing a trailing newline if it's "unavoidable".

If it's acceptable to include a trailing newline, change

echo -n '+-+

to

echo '+-+

and save 3 bytes, for 39 bytes total.

\$\endgroup\$
1
\$\begingroup\$

Python 3, 71 bytes

from shutil import*
try:move('f',input())
except:print("+-+\n| |\n+-+")

It must be in a file named f

Fairly simple. It tries to move itself to whatever directory is given to it on STDIN, and if an error occurs it prints the box.

\$\endgroup\$
0
\$\begingroup\$

C 137 bytes

#include<dirent.h> g(char *f,char *z){DIR* d=opendir(z);if(d){strcat(z,f);rename(f,z);}else{puts("+-+");puts("| |");puts("+-+");}}

Ungolfed version:

#include<dirent.h> 
g(char *f,char *z)
{ 
  DIR* d=opendir(z);
  if(d)
  {
    strcat(z,f);
    rename(f,z);
  }
  else
  {
    puts("+-+");
    puts("| |");
    puts("+-+");
  }
}

f accepts the filename and z accepts the directory name. Destination string is a concatenation of z and f. rename() is used to move the file to the new location.

The main() would look like this:

int main(int argc, char *argv[])
{
    g(argv[0]+2,argv[1]); // 1st arg is the Destination string
    return 0;
},

Can definitely shortened somehow!

\$\endgroup\$
0
\$\begingroup\$

Ruby, 58+12 = 70 bytes

Uses flags -nrfileutils. Input is piped in from a file w/o newlines into STDIN.

FileUtils.mv$0,File.exist?($_)&&$_ rescue$><<"+-+
| |
+-+"
\$\endgroup\$
0
\$\begingroup\$

Minecraft ComputerCraft Lua, 74 bytes

if fs.isDir(...)then fs.move("f",... .."f")else print("+-+\n| |\n+-+")end

Filename is hard-coded as "f". This runs on an in-game computer and runs relative to that computer's directory structure. Uses CC Lua's builtin fs API.

Ungolfed:

local tArgs = { ... }      -- '...' is Lua's vararg syntax, similar to python's *args syntax
if fs.isDir(tArgs[1]) then -- Using '...' is like passing all args separately, extra args are usually ignored
    fs.move("file", tArgs[1] .. "file") -- '..' is Lua's string concatenation syntax
else
    print("+-+\n| |\n+-+") -- print the required output if the directory doesn't exist
end
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog Unicode), 41 bytes

{⎕NEXISTS⍵:'f'⎕NMOVE⍨⍵,'f'⋄3 3⍴'+-+| |'}⎕

Try it online!

filename is hardcoded as f, in the current working directory.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.