Solved

batch file to move files to folders based on their names

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

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 84

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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