Windows.BAT to move all directories matching the mask from dir A to directory B

I want to write a .BAT file to move all subdirectories (whose name matches the mask) C: \ WINNT \ Temp to H: \ SOMEOTHERPLACE.

So, if my mask is ABC *, then the directories are:

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 that match the mask) shouldn't. I want to move them and not copy.

Can anyone point me in the right direction?

+2


source to share


2 answers


OK I figured it out. If you write a moveirs.bat file containing a 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 that I want to move, and argument 2 being the directory where I want to move the directories) as

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

      

It produces the effect I want.



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

Just for posterity in researching this, I did something similar with xcopy, but then I would have to participate in deleting the source, so for my purposes, moving 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

      

To process directories, and also without extensions, for example, the command "C: \ MyDir * .MyExt" above will need the combined modifier (~ filename + extension) "~ nx":

for /d %%W in (%1) do xcopy %%W %2\%%~nxW /E /F /R /Y /I

      

+8


source


[Comments are useless for structured responses, so I'll repeat the comment here - with a few fixes!]

Thanks for your answer, but I found that you cannot use xcopy with a template in source. Or rather, you can use wildcards, but then you only get the created directories without any content. So if you do this ...

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

      



... you end up with directory ABC1 copied into your current directory as you might reasonably expect, but if you do ...

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

      

... you will get every directory name present in C: \ WINNT \ Temp that appears in your current directory, but those directories will be empty! Please tell me that I am wrong, but this is what I find!

0


source







All Articles