61
19
It is simple. I cannot stand when people use spaces when naming files. It sometimes wrecks console commands and makes the output of ls ugly.
The challenge is to write a program (only ascii characters) which
- renames all files (including directories) in the current directory to versions with spaces removed or replaced by '_'
- on collision, you need to append a unique identifier (up to you)
- descends recursively into all subdirectories
You can assume UNIX-style path names. Who would need this program on a Windows machine anyways?
This is code golf, the shortest program wins (#ascii characters). Since I hate spaces so much, each space has to be counted twice.
Please provide your language, score, program and a short description of how to run it.
The program must compile and execute with reasonable effort on my linux machine.
EDIT: As Etan requested a file structure for testing, here is the script I currently use to create a suitable file tree:
#!/bin/bash
rm -r TestDir
touchfiles()
{
touch my_file
touch my__file
touch "my file"
touch "my file"
touch " my_file "
}
mkdir TestDir
cd TestDir
touchfiles
for dir in "Test Sub" Test_Sub "Te stSub" Te_stSub
do
mkdir "$dir"
cd "$dir"
touchfiles
cd ..
done
It's tempting to write an answer in batch.. ;) – globby – 2015-01-21T15:52:00.680
renames all files in the current directory to versions with spaces removed – Дамян Станчев – 2014-08-06T12:17:56.883
I will edit the challenge to allow for replacements. – M.Herzkamp – 2014-08-06T12:21:55.853
@ДамянСтанчев I know, but the first submission used a replacement and the OP didn't complain. ;) – Martin Ender – 2014-08-06T12:28:00.623
1How to replace one char with another in all filenames of the current directories? – The Guy with The Hat – 2014-08-06T14:02:22.367
22This is begging for a solution made without ascii chars. – Dennis Jaheruddin – 2014-08-06T15:44:33.033
Well, are you running TeX on a Windows machine? That will leave you viscerally hating that space you left in the name of your images directory. – E.P. – 2014-08-06T16:27:10.407
50Now I want to learn Whitespace – BrunoJ – 2014-08-06T18:03:45.633
2Is it okay to just replace all spaces in filenames on the system with random garbage from /dev/urandom? – Nit – 2014-08-06T18:27:48.727
You can remove spaces or replace them with '_' – M.Herzkamp – 2014-08-06T20:43:27.100
@Dennis Edited the challenge to fill that loophole – M.Herzkamp – 2014-08-06T20:56:05.623
Are "Tabs" (\t) considered spaces for scoring? – Kevin Fegan – 2014-08-07T00:51:11.547
In case of name collision, should "unique identifier" be appended to the file "name", and the file "extension" left alone? Like:
index 1.html
-to-index1-02.html
, notindex1.html-02
. – Kevin Fegan – 2014-08-07T00:57:08.603It might be wise to add an extended example. 'Collisions' get confusing with a deep tree where multiple subdirs have spaces. – Michael Easter – 2014-08-07T02:16:31.897
10@BrunoJ doing this in Whitespace would first require you to develop a file access system in WS. I think that would be more challenging than the actual challenge. – Nzall – 2014-08-07T06:40:07.430
1"Who would need this program on a Windows machine anyways?" - people calling "Program files" content in console ;) Its same issue as on Unix, and Windows for some time accepts paths with / as well. – PTwr – 2014-08-07T07:28:56.533
@Kevin Append the identifier to the extension. I don't hate tabs as much, so they only count once. – M.Herzkamp – 2014-08-07T09:35:16.637
1@PTwr "Windows for some time accepts paths with / as well" — since always. And MS-DOS too (since directories, to be accurate). – Athari – 2014-08-07T12:04:53.370
@Athari thanks for clarifying, I was not sure if non-NT supported it. – PTwr – 2014-08-07T13:04:42.133
7Waiting for someone to post a C/C++ solution so I can steal it, compile, post in hex as x86 machine code with ZERO spaces! [or maybe base64] – Mark K Cowan – 2014-08-07T13:35:48.353
10I hate underscores in filenames. Use dashes. – HostileFork says dont trust SE – 2014-08-07T17:07:56.217
Is there a corpus of files to test solutions against? – Etan Reisner – 2014-08-08T01:27:07.513
@Dr. Rebmu: Why? – M.Herzkamp – 2014-08-08T08:54:09.297
@Etan: included my test tree creator script – M.Herzkamp – 2014-08-08T08:54:34.837
When collisions are encountered, do we have to append to the filename that had spaces or can we append to the original file as well? – Dennis – 2014-08-08T13:50:45.347
@MarkKCowan you don't need to compile and use hex for that: all spaces outside strings can be substituted with newlines, and all spaces inside strings can be substituted with the corresponding ASCII code (32) – pqnet – 2014-08-08T14:17:31.897
1"I cannot stand when people use spaces when naming files. It sometimes wrecks console commands." ... man I hate this nail in my toolbox, it destroys my hammer! – user541686 – 2014-08-08T19:25:58.363
2You should include some characters that cause problems in badly-written shell scripts in your test harness: backslash, newline, tabs,
*
, … – Gilles 'SO- stop being evil' – 2014-08-08T21:32:42.5871
@M.Herzkamp Don't have to hit shift to type them, considered a word separator in RegEx/Unicode, doesn't sink down and disappear into a bounding box if the filename winds up in a box, less awkward typographically if you ever have to communicate about a filename as a URL in print, there are probably other reasons...
– HostileFork says dont trust SE – 2014-08-09T07:57:28.633@pqnet Compiled-to-hex asm would use less chars when posted here :) – Mark K Cowan – 2014-08-09T15:30:21.230
1Please include non English Characters as a test case. – ojblass – 2014-08-13T20:32:26.877