Solved

batch file to move files to folders based on their names

Posted on 2013-01-06
4
867 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

18 Experts available now in Live!

Get 1:1 Help Now