Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

batch file to move files to folders based on their names

Posted on 2013-01-06
4
Medium Priority
?
880 Views
Last Modified: 2013-01-07
Hello experts ,
I have over 6000 mp3 files ( see attached ) in one folder
every file name is just 6 digits
 what I want is a batch file that do the following
1- moving every file to a folder ( create folder if not exit  )  based on the first 3 digits on the file name  ( leading zeros should be removed )

2- the  new   file name  must be the last 3 digits of the old file name ( leading zeros should be removed )

examples.

002001.mp3    should be moved to the folder named 2    and the new file name should be 1.mp3


023051.mp3    should be moved to the folder named 23    and the new file name should be 51.mp3
0
Comment
Question by:honestman31
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 38749985
Try the script below; just set the "SourceFolder" variable to the folder where the mp3 files currently are, and the "TargetFolder" variable to the folder where you want the new folders to be created.
The script is currently in test mode and will only display the "md" and "move" commands it would normally run. Remove the two uppercase ECHOs in lines 10 and 11 to run it for real.
@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Temp
set TargetFolder=D:\Temp
for %%a in ("%SourceFolder%\*.mp3") do (
	set OldFileName=%%~na
	echo Processing !OldFileName!.mp3 ...
	set /a NewFolderName = 1!OldFileName:~0,3! - 1000
	set /a NewFileName = 1!OldFileName:~3,3! - 1000
	if not exist "%TargetFolder%\!NewFolderName!" ECHO md "%TargetFolder%\!NewFolderName!"
	ECHO move "%%~a" "%TargetFolder%\!NewFolderName!\!NewFileName!.mp3"
)

Open in new window

0
 
LVL 10

Author Comment

by:honestman31
ID: 38750055
Hi,
I think something wrong with this file ,  I ran it live and I got up to folder 37  , should be 114 folders though
Thanks
0
 
LVL 85

Expert Comment

by:oBdA
ID: 38750063
Well - what happened?
Any error messages, and if so, which ones?
Are there other mp3 files in the source folder that do not match the file name pattern you specified?
0
 
LVL 10

Author Comment

by:honestman31
ID: 38750072
u r correct , it is my fault , something was wrong with original folder & files , fixed that  and it works fine
just doing my final check on it , I'll let you know if i have nay problem
Many thanks
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question