Solved

batch file to move files to folders based on their names

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

627 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