Filenames to folder names
May 9, 2024 3:18 PM   Subscribe

I have a folder with a list of .txt files in it - file list example (imgur). I would like to create a folder for each file name and to name that folder after the filename.

Header folder is located on a NAS (but I can shift it to my desktop if that proves to be an issue).

I don't really need the .txt files to be subsequently moved to the new folders, although it would be a nice to have.

System is Windows Home, filepath has no spaces, and I’m fine with cmd type solutions, just lacking the mental tools to write them.
posted by unearthed to Computers & Internet (7 answers total) 1 user marked this as a favorite
 
You may get someone to write a script for you here. You may not. Consider StackExchange/StackOverflow
posted by falsedmitri at 3:59 PM on May 9


You can get the file list from the command line, write that to a TXT/CSV, paste into Excel, then use some string functions to write out a batch file. I'd be glad to help with this!
posted by jmfitch at 4:40 PM on May 9 [1 favorite]


Bulk Rename Utility can do this.
posted by soelo at 6:09 PM on May 9


If it were me, the quick and easy way I would do it is:


* select all the .txt files in Windows and Copy to get the list of file names - or, on command line,
ls -1 * > list-of-file-names.txt
to get a list of file names
*paste the list into a spreadsheet
* use a spreadsheet formula to strip the .txt from the file name - for example, in Libreoffice Calc, you could enter =LEFT(a1,LEN(a1)-4) into column B
* use a spreadsheet formula to make commands to make directories - again, in Libreoffice Calc, something like =CONCAT("mkdir ",B1) into column C should do it
* copy the contents of column C, which is now a big set of "mkdir" commands, and either paste that straight into your command line, or paste it into a text file, then run that text file from the command line

I'm sure there are much better ways, but that's one quick way.
posted by kristi at 7:48 PM on May 9 [1 favorite]


Best answer: I only have a Linux commandline (Bash shell) solution, but I'm told that today every Windows comes with WSL (Windows Subsystem for Linux). If you have to set that up and it exceeds the level of annoyance you have spoons for, please ignore me. Otherwise, inside the same directory:

for textfile in *.txt; do parent="${textfile%.*}"; mkdir -p "$parent"; mv "$textfile" "$parent"; done

This also moves the files into the directories. I tested it with filenames with spaces. You might run into an issue if any of the names contain characters that have special meaning in bash, although this seems unlikely (I suggest working on a copy just in case).

(I had pretty much this same script already written, but for different file extensions, for aggregating ebooks downloaded in different formats from Humble Bundle, for easier import into Calibre.)
posted by confluency at 11:43 PM on May 9 [2 favorites]


I use mmv, a python script that allows you to create wildcard blocks and move parts of filenames in a long path into parts of directory names.
posted by k3ninho at 5:53 AM on May 10


Response by poster: Thanks everyone and especially toconfluency for the bash route.

As I only have some flavour of Windows Home I (apparently) lack WSL, but I found Git for Windows, which installs as Git Bash* on the Windows desktop.

so
cd /n/test_bash {if there's a space in the path, that would be "/n/test bash"}

and run confluency's script:

for textfile in *.txt; do parent="${textfile%.*}"; mkdir -p "$parent"; mv "$textfile" "$parent"; done

A perfect script that ran the first time, thanks again confluency. Presto, a group of named folders with the same-named .txt file moved into each folder.

For future me, or others new to bash:

How to change drive letter in bash
Working with git bash commands
Commands list on a LinkedIn page

* git bash is a stripped-down bash so a lot of functionality is missing, but it still does a lot!
posted by unearthed at 12:54 AM on May 11 [1 favorite]


« Older East Sussex without a car?   |   How do you plan a kitchen, quickly? Newer »

You are not logged in, either login or create an account to post comments