views:

606

answers:

5

Hi - I want to write a .BAT file to move all sub-directories (whose name matches a mask) of C:\WINNT\Temp to H:\SOMEOTHERPLACE.

So if my mask is ABC* then the directories :

C:\WINNT\Temp\ABC1
C:\WINNT\Temp\ABC2
C:\WINNT\Temp\ABC3

should be moved to

H:\SOMEOTHERPLACE

and everything else (including files, as opposed to directories, which match the mask) should not. I do want to move them and not copy.

Can anyone point me in the right direction ?

A: 

you can use xcopy command with the /e option to copy files, and then just remove the source with the del command

Lyubomyr Shaydariv
Thanks for your answer but I've found that you can't use xcopy with a wildcard on the source. Or rather you can use wildcards but then you get only the directories being created without any content.So if you do this ...H:\SOMEOTHERPLACE>xcopy C:\WINNT\Temp\ABC1... you'll get the the ABC1 directory copied to your current directory but if you do this ...H:\SOMEOTHERPLACE>xcopy C:\WINNT\Temp\ABC*... you'll get each directory name present in C:\WINNT\Temp appearing in your current directory but those directories will be empty ! Please tell me I'm wrong but that's what I find !
southof40
A: 

[Comments are useless for structured answers so I'll repeat the comment here - with a couple of edits !]

Thanks for your answer but I've found that you can't use xcopy with a wildcard on the source. Or rather you can use wildcards but then you get only the directories being created without any content. So if you do this ...

H:\SOMEOTHERPLACE>xcopy C:\WINNT\Temp\ABC1 /E

... you'll get the the ABC1 directory copied to your current directory as you might reasonably expect but if you do this ...

H:\SOMEOTHERPLACE>xcopy C:\WINNT\Temp\ABC* /E

... you'll get each directory name present in C:\WINNT\Temp appearing in your current directory but those directories will be empty ! Please tell me I'm wrong but that's what I find !

southof40
+1  A: 

OK I've figured this out. If you write a movedirs.bat file containing the single line

for /d %%X in (%1) do move %%X %2\%%~nX

And then run it (with argument 1 being the mask for the directories I want to move and argument 2 being the directory I wish to move the directories to) as

C:\>movedirs.bat C:\WINNT\Temp\ABC* H:\SOMEOTHERPLACE\

It produces the effect I want.

The /d argument on the 'for' ensures that only directories are processed. The '~n' modifier on the %%X variable means that the original sub-directory name (as opposed to the entire path) is used as the target within the second command line argument.

Just for the sake of posterity in investigating this I did something similar with xcopy but then i would have had to get involved in deleting the source so for my purposes move works better but for the record here's the same idea wrapped around xcopy.

for /d %%X in (%1) do xcopy %%X %2\%%~nX /E /I
southof40
A: 

Wonderful!!

i just needed to move all my music album alphabetically. Now, I just need to create a directory for letter (A, B, C... ) and to do one code line for each, and one click : )

Thanks from Madrid

Javier
only one question, the script don´t works if the directory have spaces, for example "Bad Religion".
Javier
I haven't actually tried this but are you sure that putting quotes around the two arguments to movedirs.bat doesn't solve the problem with spaces ?
southof40
C:\>movedirs.bat "C:\WINNT\Temp\Bad Religion" "H:\SOMEOTHERPLACE\"
southof40
As I say I haven't tried (and I can't at the moment) but that would be my first guess - would be interested to hear how you go.
southof40
A: 

Hello, I tried it, but the result is the same with or without quotes. I´m testing, my movedirs.bat contains this:

for /d %%X in (%k:\prueba\a*) do move %%X %k:\b\%%~nX

I run it from other .bat which contains

k:\movedirs.bat "K:\prueba\a*" "k:\b"

I tried it without quotes and it´s the same. Also I tried without * ,it´s doesn´t run..

Also i tried with the directory exactly name but it don´t run:

1)for /d %%X in (%k:\prueba\as aaa) do move %%X %k:\b\%%~nX 2)k:\movedirs.bat "K:\prueba\as aaa" "k:\b"

(In k:\prueba\ there are two subdirectories "as aaa" and "as". The first isn´t moved, the second is moved).

I don´t know why I have to run movedirs.bat from other .bat, the result runs movedirs.bat directly it´s the same.

I hope you can understand my english. I´ll continue trying it. If i find something I´ll tell you. Thanks anyway.

Javier