Help with a batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ArieS
Posts: 8
Joined: 25 Oct 2012 11:58

Help with a batch

#1 Post by ArieS » 25 Oct 2012 12:07

Hello,

I need some help with a batch script. I don't know anything about it and I found this batch on the net.


@echo off
for %%a in (*.*) do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)


I need to move 4 files in 1 folder.
I’m using it for XBMC and it moves 3 out of the 4 to 1 folder (i.e nameofmovie.mkv, nameofmovie.tbn, nameofmovie.nfo) except nameofmovie-fanart.jpg
This one is moved to its own folder.
Anyone can help with the missing command?
Thanks in advance!

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Help with a batch

#2 Post by trebor68 » 25 Oct 2012 15:54

Here not tested code.
This code will work when a file "nameofmovie.mkv" exist.

Code: Select all

@echo off
for %%a in (*.mkv) do (
md "%%~na" 2>nul
move "%%~na*.*" "%%~na"
)


This code will not work perfect when you have files like this:
NameOfMovie.mkv
NameOfMovie2.mkv

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

Re: Help with a batch

#3 Post by foxidrive » 25 Oct 2012 20:12

This should move all files to a single folder. It chooses one file to get the name from for the folder, and uses that name for all of the files.

Code: Select all

@echo off
for %%a in (*.*) do set "name=%%~na" & goto :gotit
:gotit
md "%name%" 2>nul
for %%a in (*.*) do move "%%a" "%name%"



EDITED: The do keyword was added

ArieS
Posts: 8
Joined: 25 Oct 2012 11:58

Re: Help with a batch

#4 Post by ArieS » 25 Oct 2012 23:59

trebor68 wrote:Here not tested code.
This code will work when a file "nameofmovie.mkv" exist.

Code: Select all

@echo off
for %%a in (*.mkv) do (
md "%%~na" 2>nul
move "%%~na*.*" "%%~na"
)


This code will not work perfect when you have files like this:
NameOfMovie.mkv
NameOfMovie2.mkv

Thank you so much !!
It worked perfectly!
I guess I'll be careful if I have some movies with sequels that everything is moved to the right folder.
You rock dude!

ArieS
Posts: 8
Joined: 25 Oct 2012 11:58

Re: Help with a batch

#5 Post by ArieS » 26 Oct 2012 00:00

foxidrive wrote:This should move all files to a single folder. It chooses one file to get the name from for the folder, and uses that name for all of the files.

Code: Select all

@echo off
for %%a in (*.*) do set "name=%%~na" & goto :gotit
:gotit
md "%name%" 2>nul
for %%a in (*.*) move "%%a" "%name%"


This one created a new folder nameofmovie-fanart but didn't move anything to it.
Thanks for your time though.

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

Re: Help with a batch

#6 Post by foxidrive » 26 Oct 2012 00:30

That was a typo on my part, I forgot the DO keyword. This is tested.

Code: Select all

@echo off
for %%a in (*.*) do set "name=%%~na" & goto :gotit
:gotit
md "%name%" 2>nul
for %%a in (*.*) do move "%%a" "%name%"

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

Re: Help with a batch

#7 Post by foxidrive » 26 Oct 2012 00:37

ArieS wrote:
trebor68 wrote:

Code: Select all

@echo off
for %%a in (*.mkv) do (
md "%%~na" 2>nul
move "%%~na*.*" "%%~na"
)


Thank you so much !!
It worked perfectly!
I guess I'll be careful if I have some movies with sequels that everything is moved to the right folder.
You rock dude!


trebor68's solution is fine unless the sorted filename has 'movie name.!artwork.bmp' first in the list, when it will skip the rest. When using FAT32 this will be an issue.

ArieS
Posts: 8
Joined: 25 Oct 2012 11:58

Re: Help with a batch

#8 Post by ArieS » 26 Oct 2012 10:56

foxidrive wrote:That was a typo on my part, I forgot the DO keyword. This is tested.

Code: Select all

@echo off
for %%a in (*.*) do set "name=%%~na" & goto :gotit
:gotit
md "%name%" 2>nul
for %%a in (*.*) do move "%%a" "%name%"

This one moves everything to 1 folder, even different file names.

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

Re: Help with a batch

#9 Post by foxidrive » 26 Oct 2012 20:12

Yes, it does. Your first example used *.* too and you said it worked, but only moved 3 out of 4 files.
Obviously that leads us to assume that there were only 4 files in the folder.

Maybe you can rephrase what you need to do.

Post Reply