# batch file to move files



## innirvana1 (Jan 9, 2004)

I'm trying to make a batch file to move all the files from one folder into another folder (which is totally easy). And i know it gives the option of puting /y to automatically say yes to prompts of overwriting the file, but i don't want the file overwritten. but using /-y is no good because that just sits at the prompt and i need it automated.

is there a way to have the move command automatically say "no" to overwriting and just leave the files that are duplicates?

if not, is there more/other code i could use to automate the same process? maybe something like looping through the filenames and using an "if" to see if it's duplicated and only use the "move" command if it's ok to?

i rarely use batch files so i'm not real profecient with this coding...

thanks,
-jes


----------



## mole (Aug 24, 1999)

If this is from a Windows command prompt or Win9x "DOS" session, you can use the xcopy command. Investigate the various options for xcopy by typing "xcopy /?". Xcopy /d comes to mind as a useful way for over-writing or copying based upon date.

Another way, if you don't want to copy from the source if a file in the target location with the same name already exists would be:

for %a in (*.*) do if not exist {target}\%a copy %a {target}\%a

Where you invoke the above command from the source directory and {target} is the destination directory/path.

Practice on a test set of directoies first until you obtain the desired result.


----------



## innirvana1 (Jan 9, 2004)

*******
for %a in (*.*) do if not exist {target}\%a copy %a {target}\%a
********

oh awesome!! this is exactly what i need! 
here's what code i have to match what i need:

for %a in (*.*) do if not exist g:\Jesse1\%a copy %a g:\Jesse1\%a

but i'm having one small problem.....if i leave the "(*.*)" part alone, it just pulls in the files "autoexec.bat, boot.pcp, config.sys, pkgclnup.log" from i don't know where...i'm assuming where ever the defult location is since i didn't specify a place. BUT, when i do specify a location like this:

for %a in (g:\Jesse2\*.*) do if not exist g:\Jesse1\%a copy %a g:\Jesse1\%a

it stores that whole location in %a and is then excuted as:

if not exist g:\Jesse1\g:\Jesse2\filename.xxx copy ..........

does this make sense to you what my problem is?
i'm sure we'll get this to work though...i just need more help...
thanks so much!
-jes


----------



## mole (Aug 24, 1999)

It sounds like you are running the command from the "root", also known as c:\. You need to change directory into the place the *.* source files are located, then run the command:

g:
cd\Jesse2
for %a in (*.*) do if not exist g:\Jesse1\%a copy %a g:\Jesse1\%a

If you wish to run the command from another place, you need the whole path in both places. Warning - this can be "ticklish", I've seen different versions of Windows behave differently. The variations in the command processor from one version to another will drive a person "nuts":


for %a in (g:\Jesse2\*.*) do if not exist g:\Jesse1\%a copy %a g:\Jesse1


If this doen't work, use the first sequence of commands in this reply. Different behavior might be had appending a \%a at the very end. Also remember that from within a batch, the %'s will have to be doubled as in %%.


----------



## innirvana1 (Jan 9, 2004)

oh thank you thank you!!
i was having two problems and you answered both of them...i wasn't even aware about having doubling the %'s in batch files...thanks alot!
my file is working great now....but still just with one problem.... 
i'm running it on NT server......it doesn't like the files that don't fit the 8.3 format of the filenames......
is there an easy workaround for this??

otherwise i'll just run it on an xp or 2000 machine and it should run fine.....but i like having things run centrally on the server....

thanks alot though for your help!! i'm definitely coming back here for more help! 
-jes


----------



## mole (Aug 24, 1999)

I'm glad its working at least on W2K for you. 

Is the NT4 box at SP4 or greater or is it an antique 3.51 from the stone age?

NT4 should support long file names (greater than 8x3) as far as I know, mine do. Only if there are spaces would you need to wrap file names in quotes. (Generally it is best and easiest to not put spaces in file names.). If you still get the dreaded "~" in your longer file names, check to see what the command processor is. That is, what does %COMSPEC% resolve to when issuing the "set" command. It should be "cmd.exe" which I believe is called from %WINDIR%\system32 directory. If by chance it is command.com, then that is the problem. If for whatever reason you can't change it to cmd.exe, you should be able to run a spawned secondary CMD process from command.com like this:

cmd /c for %a in (*.*) do blah-blah-blah

And yes, running centrally on a server is best. Always do that when possible. I hate it especially when techs have users have desktop links pointed here and there! Point them up to "the sky" where you can manage from the server what they point to.

Glad you got some help here, Mike (Tech Guy) has been doing a great service as have all of the others who contribute here.


----------



## innirvana1 (Jan 9, 2004)

thanks again...
and after doing more tests...i noticed that long filenames do work and are ok....but i'm still stuck on filenames that have spaces....i probably could just tell all my users to never use spaces again.....
but i kinda still would rather allow them just so my users(mostly my manager) can stay lazy 

but where in my one line of crazy code can i put quotes to get it to work?

but i think i might be completly done with this...thanks so much for your expertise! 
-jes


----------



## mole (Aug 24, 1999)

Wrap everything in quotes and use wildcards in the target:

for %a in ("*.*") do if not exist "g:\Jesse1\%a" copy "%a" "g:\Jesse1\*.*"

Glad we made some progress. I always like to refer people interested in batch or command line scripts to:

http://www.robvanderwoude.com/

If you have much to do with servers and ever have a need to do some rather sophisticated things with fairly simple scripts not too far beyond batch, check out KiXtart. There are links on the site above.

EDIT: BTW - I want to add, that there is very little one does in Windows that cannot be done within the native command processor. Take some time one day and got through c:\winnt\*.* and c:\wint\system32\*.* and run each *.exe with a "/?" behind it and see the parameters that can be used from a batch/command line script. "Net.exe" is an especially powerful tool that you can run many things on a network with. Experiment and have fun with it!


----------

