Phil Jenkins
Phil Jenkins

Reputation: 161

Folder Names Based on Filenames

This isnt really a coding question. I need to put all my files into individual directories so each file has its own directory with the name based on the filename. So before i go make an application to do this, does anyone know any software that can do this? like Automator or something.

Upvotes: 1

Views: 3034

Answers (1)

dbenham
dbenham

Reputation: 130819

No need to build an application. This simple one liner run from the Windows command line will move each file in a directory into a sub-directory based on the root name of the file.

for %f in (*) do @(md "%~nf"&move "%f" "%~nf")>nul 2>&1

Two files with the same base name but different extensions will be moved into the same directory. For example, "test.txt" and "text.doc" will both be moved into a directory named "test"

Any file without an extension will not be moved.

If you want to run this from a batch file, then

@echo off
for %%f in (*) do (
  md "%%~nf"
  move "%%f" "%%~nf"
) >nul 2>&1

You requirements were not very clear. If your requirements differ, then the script can probably be modified fairly easily to suit your needs.

Upvotes: 4

Related Questions