Be there or be square!

19

3

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.

FinW

Posted 2017-01-22T19:30:45.713

Reputation: 477

Can we hardcode the filename? – BookOwl – 2017-01-22T20:27:40.357

Yes @bookowl you may – FinW – 2017-01-22T20:29:04.667

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? – Luis Mendo – 2017-01-22T21:33:18.007

@LuisMendo you will never get a file as input only a directory – FinW – 2017-01-23T17:36:47.920

@FinW Sadly, you still haven't answered my question: must the name of the created file be the same as the original file? – Luis Mendo – 2017-01-23T17:37:09.167

@luismendo there isn't a created file – FinW – 2017-01-23T17:40:18.907

@LuisMendo You're moving, not copying, so I'd assume so. – mbomb007 – 2017-01-23T19:01:10.317

@FinW Not letting the filename be hardcoded makes it a bit more challenging i guess! – Abel Tom – 2017-01-25T14:21:47.010

Answers

1

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.

Mitchell Spector

Posted 2017-01-22T19:30:45.713

Reputation: 3 392

7

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.

briantist

Posted 2017-01-22T19:30:45.713

Reputation: 3 110

2

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.

– Dennis – 2017-01-22T20:50:11.933

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 ! – briantist – 2017-01-22T20:55:35.970

I think ls is a shorter abbreviation for gci – dkudriavtsev – 2017-01-23T06:09:57.387

1@wat duh, it sure is! I use ls all the time and then in golfing I tend to forget it. Thank you. – briantist – 2017-01-23T15:10:47.130

Nice. I think this is the first time I've seen $? used in golfing here. – AdmBorkBork – 2017-01-25T18:27:49.473

Thanks @AdmBorkBork , My prospective solution on this challenge would have used $? but as far as I can tell it's impossible to convert from a number to a [char] in PowerShell without using alphanumerics.

– briantist – 2017-01-25T18:38:31.287

5

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+-+".

Suever

Posted 2017-01-22T19:30:45.713

Reputation: 10 257

1http://codegolf.stackexchange.com/a/107780/31516 – Stewie Griffin – 2017-01-23T09:47:47.093

4

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.

Neil

Posted 2017-01-22T19:30:45.713

Reputation: 95 035

4

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.

Stack Exchange App Test

Posted 2017-01-22T19:30:45.713

Reputation: 49

I think you need echo -n so that you don't print a trailing newline. – Mitchell Spector – 2017-01-24T07:36:24.803

1

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.

BookOwl

Posted 2017-01-22T19:30:45.713

Reputation: 291

0

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!

Abel Tom

Posted 2017-01-22T19:30:45.713

Reputation: 1 150

0

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$><<"+-+
| |
+-+"

Value Ink

Posted 2017-01-22T19:30:45.713

Reputation: 10 608

0

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

pizzapants184

Posted 2017-01-22T19:30:45.713

Reputation: 3 174