How to make Copy, xCopy & Move ignore short file names?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rob.Stow
Posts: 2
Joined: 22 Mar 2016 17:12

How to make Copy, xCopy & Move ignore short file names?

#1 Post by Rob.Stow » 22 Mar 2016 17:44

In batch files like this simple example mx.cmd
md "%1 %2"
move *%1?%2*.* "%1 %2"
move *%2?%1*.* "%1 %2"
move *%2,?%2*.* "%1 %2"


The move command often moves files that it shouldn't because the real file names don't match *%1*%2*.* but the short file names do.

So ... how do I tell commands like Copy and Move to ignore the short file names and only use the real file names?

%1 and %2 are generally a person's first and last names. The most common failures of my little batch files are when I only have initials to work with instead of full names. For example:
"mx J Doe" is likely to cause files to be inappropriately moved but ...
"mx John Doe" is very unlikely to do so.

Another common failure point is when I _do_ have full names of the person to work with but those names are short, such as "Jin Wu".

I often call little batch files like that mx.cmd thousands of times from within a much larger batch file and having to manually check and rectify those inappropriate file moves is a real pain in the butt.

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

Re: How to make Copy, xCopy & Move ignore short file names?

#2 Post by foxidrive » 22 Mar 2016 19:15

This eliminates short filenames - and long filenames with ~ in them.

Code: Select all

md "%1 %2"
for /f "delims=" %%a in ('dir "*%1?%2*" "*%2?%1*" "*%2,?%2*" /b /a-d ^|findstr /v "~" ') do (
   move "%%a" "%1 %2"
)

Rob.Stow
Posts: 2
Joined: 22 Mar 2016 17:12

Re: How to make Copy, xCopy & Move ignore short file names?

#3 Post by Rob.Stow » 23 Mar 2016 00:04

Many thanks foxidrive - I'm looking forward to trying that out next time I have a big batch of files to clean up and organize.

Post Reply