Need to write a batch file that moves files based on name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chbck
Posts: 2
Joined: 28 Jan 2015 03:46

Need to write a batch file that moves files based on name

#1 Post by chbck » 28 Jan 2015 03:51

My music directory is a mess. I want to write a batch file that will list the files in my "music" folder and move those files to the corresponding folders that bear their name.

For instance: In "music" I have 10 tracks from BandA, 15 tracks from BandB, and 4 tracks from BandC. I will manually create folders called BandA, BandB, and BandC, and I want a batch file that can analyze the list of tracks and move each file to their folders based on the name of the bands.

Any help would be greatly appreciated!

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Need to write a batch file that moves files based on nam

#2 Post by Squashman » 28 Jan 2015 07:14

Need to see the format of the file names before we could write any script for you.

chbck
Posts: 2
Joined: 28 Jan 2015 03:46

Re: Need to write a batch file that moves files based on nam

#3 Post by chbck » 28 Jan 2015 08:07

Sorry about that.

The filenames would be:
BandA - Song Name 1.mp3
BandA - Song 1.mp3
BandA - Name of song1.mp3
BandB - Name of song2mp3
BandB - Name1.mp3
BandC - Name of song3.mp3
and so on...

Basically, random combinations of things because they are all song titles. But, they all follow the Band Name - Song title.mp3 naming convention.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need to write a batch file that moves files based on nam

#4 Post by foxidrive » 29 Jan 2015 08:48

chbck wrote:Sorry about that.

The filenames would be:
BandA - Song Name 1.mp3
BandA - Song 1.mp3
BandA - Name of song1.mp3
BandB - Name of song2mp3
BandB - Name1.mp3
BandC - Name of song3.mp3
and so on...

Basically, random combinations of things because they are all song titles. But, they all follow the Band Name - Song title.mp3 naming convention.


If BandA B or C contain - characters then the code has to handle that. There are other situations where the names can break the code...

See here: viewtopic.php?f=3&t=6108

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Need to write a batch file that moves files based on nam

#5 Post by penpen » 29 Jan 2015 11:23

This may help you (untested):

Code: Select all

@for /D %%a in (*) do @(2>nul move "%%~a*" "%%~a")


penpen

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

Re: Need to write a batch file that moves files based on nam

#6 Post by Squashman » 29 Jan 2015 11:47

penpen wrote:This may help you (untested):

Code: Select all

@for /D %%a in (*) do @(2>nul move "%%~a*" "%%~a")


penpen

I like that. That should be fairly foolproof.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need to write a batch file that moves files based on nam

#7 Post by foxidrive » 29 Jan 2015 18:09

penpen wrote:This may help you (untested):

Code: Select all

@for /D %%a in (*) do @(2>nul move "%%~a*" "%%~a")


penpen


penpen, how is that going to move files into folders?

EDIT: Oh, he manually created all the band name folders - didn't see that bit.

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: Need to write a batch file that moves files based on nam

#8 Post by julesverne » 29 Jan 2015 21:26

@for /D %%a in (*) do @(2>nul move "%%~a*" "%%~a")


what does the "@" symbol do? I tried googling that, I don't get it.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Need to write a batch file that moves files based on nam

#9 Post by penpen » 30 Jan 2015 00:00

The command line interpreter has an echoing feature, which could be switched on (default) and off.
If this feature is enabled the command line interpreter:
- typically displays the full Path (for example "C:\temp>") within the shell.
- echoes a command before this command is executed.

In a batch program the @ character in front of a command tells the command line interpreter not to echo this command before executing it.

penpen


Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Need to write a batch file that moves files based on nam

#11 Post by Samir » 02 Feb 2015 10:31

It's cool how @ can be put on the command line in modern DOS/Command line. I don't think you were able to do that back in the 3.3-5.0 days.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need to write a batch file that moves files based on nam

#12 Post by foxidrive » 02 Feb 2015 16:01

Samir wrote:It's cool how @ can be put on the command line in modern DOS/Command line. I don't think you were able to do that back in the 3.3-5.0 days.

Download a dos boot disk, and maybe put it on a USB stick with the right tools, and try it.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Need to write a batch file that moves files based on nam

#13 Post by Samir » 03 Feb 2015 06:09

foxidrive wrote:
Samir wrote:It's cool how @ can be put on the command line in modern DOS/Command line. I don't think you were able to do that back in the 3.3-5.0 days.

Download a dos boot disk, and maybe put it on a USB stick with the right tools, and try it.
I remember it working in batch, but not command line. Have you had a different experience?

Post Reply