Move files into folder partial name

December 21, 2011 at 10:43:59
Specs: Windows 7
Hello,

This is the script I have:

@echo off
pushd "C:\Folders\"
for %%j in (*) do (
md "%%~nj"
move "%%j" "%%~nj"
)
popd
pause
exit

I hope I have explained it well,

The scrip above work well to:

Move files into a folder with the same name, if there is not folder it will create it. It will also overwrite without warning.

However for me, I need it to ONLY look at the fist 5 letters, e.g.

A2383 - Test.txt
A2383 - Test.jpg
A2383 - Test small.mbs

All the above will be move to the same folder. I suppose the interesting part is if the folder does not exist, which one will it pick to make the folder.... hmmmm. Ideally I guess the folders should be there.

Thank you


See More: Move files into folder partial name

Report •

#1
December 21, 2011 at 11:14:15
I hope I have explained it well,

The scrip above work well to:

Move files into a folder with the same name, if there is not folder it will create it. It will also overwrite without warning.

However for me, I need it to ONLY look at the fist 5 letters, e.g.

A2383 - Test.txt
A2383 - Test.jpg
A2383 - Test small.mbs

All the above will be move to the same folder. I suppose the interesting part is if the folder does not exist, which one will it pick to make the folder.... hmmmm. Ideally I guess the folders should be there.

Thank you


Report •

#2
December 21, 2011 at 12:39:08
Please can any one help, I have over 3000 to do in the next 4 hours.


Report •

#3
December 22, 2011 at 03:09:39
Please, can anyone help

Report •

Related Solutions

#4
December 22, 2011 at 11:31:05
Well this is great, no one willing to help....

Report •

#5
December 25, 2011 at 13:28:37
@echo off
setLocal EnableDelayedExpansion
for %%j in (*) do (
set name=%%j
set name=!name:~0,5!
if not exist !name! (
md !name!
)
move "!name!*" !name!
)

Take note that the batch file will be moved into a folder of it's name as well.. Just move it back out or delete it after..


Report •

#6
December 31, 2011 at 11:22:10
Hi, I tried this however it CREATES a new folder. The folders already exists so it only have to move the the existing folders. It will identify the folders by the fist 5 letters.

Any help would be great...


Report •

#7
December 31, 2011 at 11:25:38
Thank you for your help SandyClaws, almost there

Report •

#8
January 2, 2012 at 06:03:09
Hello, I tried this but it did not work.

@echo off
setLocal EnableDelayedExpansion
for %%j in (*) do (
set name=%%j
set name=!name:~0,5!
move "!name!*" !name!
)

Any help is welcomed.

Thank you


Report •

#9
January 4, 2012 at 02:46:08
The Solution:

@echo on
setlocal enabledelayedexpansion
cls

pushd c:\folders\

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

for /f "tokens=*" %%A in ('dir /ad /b') do (
set dirid=%%A&set dirid=!dirid:~0,4!

if "!dirid!" equ "!dirname!" move %%1 %%A
)
)

Thanks to Wahine...


Report •

Ask Question