Solved Filename to Folder script

October 25, 2012 at 01:38:39
Specs: Windows 7, Pentium Dual E5700 (3.0GHz)/2GB RAM
I’m looking for a way to shift files into folders automatically by creating them.
e.g.
800025617-L1.jpg
800025617-S1.jpg
800025617-T1.jpg
800025888-L1.jpg
800025888-S1.jpg
800025888-T1.jpg

So the Output ought to be-
<folder>\800025617\800025617-L1.jpg
<folder>\800025617\800025617-S1.jpg
<folder>\800025617\800025617-T1.jpg
<folder>\800025888\800025888-L1.jpg
<folder>\800025888\800025888-S1.jpg
<folder>\800025888\800025888-T1.jpg
so forth...


Requirements: -
1. The numbers are 9 characters in length, most of them, some are longer and some shorter. So I’ll need variables
2. There can be other files than .jpg’s too.
3.Folders will keep varying, so I’ll need to know what to alter for Input and output.
4. Please make it as universal as possible so that other users can also benefit from this question

System -
Win 7 32bit

What I found is this
But it does not work and gives only output of a folder called “!dirname!” but nothing in it. Found at
http://www.computing.net/answers/pr...


@echo off
setlocal enabledelayedexpansion
cls

pushd c:\files\

for /f "tokens=*" %%1 in ('dir /a-d /b c:\files\*.jpg') do (
set filename=%%1&set dirname=!filename:~0,7!

if not exist c:\files\!dirname! (md c:\files\!dirname!)
copy %%1 c:\files\!dirname!\>nul
)


Much thanks in advance. I’ll keep giving feedback as I test.


See More: Filename to Folder script

Report •


#1
October 25, 2012 at 03:11:34
✔ Best Answer
Here the script (untested). If you want to delete the files transferred from the input folder, replace copy with move.

@echo off & setlocal
echo.
set /P FolderIN=Enter input folder^>
set /P FolderOU=Enter output folder^>
echo.
echo.  Input Folder  "%FolderIN%"
echo.  Output Folder "%FolderOU%"
echo.  Press any key to continue or CTRL+C to abort...
pause >nul

echo.Transferring files, please wait...
set cnt=0
pushd %FolderIN%
for /F "tokens=1-2 delims=-" %%j in ('dir /B *.jpg') do (
  set /A cnt+=1
  if not exist "%FolderOU%\%%j" md  "%FolderOU%\%%j"
  copy "%%j-%%k" "%FolderOU%\%%j\%%j-%%k" >nul
)
popd
echo.DONE. %cnt% files transferred.


Report •

#2
October 25, 2012 at 23:51:22
Sorry, coding in a rush I missed a statement. Now I edited my previous post that should be correct.

Report •

#3
October 26, 2012 at 01:57:23
Wow IVO, YOU ARE THE BEST...it works PERFECTLY!!!
If you are in Mumbai, India. Coffee is on me ;)

Report •
Related Solutions


Ask Question