2

So if I have

/folder/file1.txt
/folder/file2.jpg
/folder/file3.py

I want to create

/folder/file1/file1.txt
/folder/file2/file2.jpg
/folder/file3/file3.py

I have this batch file (be careful where you run it), which mostly works but if there is whitespace in the file name, the folder name will only be named up until the whitespace and so the file won't be moved inside of it.

Also, I only got it to work by arbitrarily putting the word "Folder" or some random string at the end of the folder name, if I exclude that, for some reason it won't work. I'm on windows 7.

@echo off

for /f %%a in ('dir /a-d /b') do (
  if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
)

goto :EOF

:func
set file=%~1
set dir=%file% Folder
md "%dir%" Folder 2>nul
move "%file%" "%dir%" 
goto :EOF

Any ideas on how to address the whitespace/name issues? Thanks in advance.

Charles Clayton
  • 17,005
  • 11
  • 87
  • 120

2 Answers2

2
@echo off

for /f "usebackq delims=?" %%a in (`dir /a-d /b`) do (
  if not "%%~dpnxa"=="%~dpnx0" call :func "%%~a"
)

goto :EOF

:func
set file=%~1
set dir=%file% Folder
md "%dir%" Folder 2>nul
move "%file%" "%dir%" 
goto :EOF

By setting the delims=? you are saying that your delimiter is a ? to split up a string, instead of the whitespace character, which allows you to read full file names with spaces in them. Usebackq means that you instead use ` around the command to be ran, which to me, just makes it more logical to read and understand "Hey, I'm actually executing this string."

Gyhth
  • 1,155
  • 9
  • 21
  • That did the trick, thanks! Any idea on that or how to ditch having to include some arbitrary string into the folder name? – Charles Clayton Jun 12 '14 at 20:46
  • On that one, you're best off waiting for the next reply as I'm not certain on that part at all, sorry. – Gyhth Jun 12 '14 at 20:48
1

To avoid problems with spaces in paths/file names, double quote all the references to them.

The reason for having to include a string at the end of the folder in your code is you are trying to create a folder with exactly the same name that the file (in your code, you are not removing the extension), and you can not have two elements (files or folders) inside a folder with the same name.

@echo off
    for %%a in ("c:\folder\*") do (
        if not "%%~fa"=="%~f0" (
            if not exist "%%~dpna\" echo md "%%~dpna" 
            if     exist "%%~dpna\" echo move /y "%%~fa" "%%~dpna"
        )
    )

For each file in the indicated folder

  • if the file is not the batch file

    • if not exist a folder with the same name that the file, create it
    • if the target folder exist, move the file to the folder

%%~fa = full path of the file being processed

%~f0 = full path of the batch file

%%~dpna = drive, path, and file name without extension of the current file being proccesed

In this code, the reason for the third if is to check if the possible previous folder creation has failed. If you have a file with no extension, you will not be able to create the folder, as it will have exactly the same name as the file and this is not allowed.

Code includes echo commands before md and move to show what will be executed. If the output is correct, remove the echo to make it work.

MC ND
  • 69,615
  • 8
  • 84
  • 126