Solved

batch file to move files to folders based on their names

Posted on 2013-01-06
4
869 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
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scripting a move of folders that do not end with two digits 8 79
netsh interface ip show interface in Windows XP 4 118
MailSend Attachments 41 141
Recursively Delete Files 5 66
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now