There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
DOS/PDA/Other
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > DOS/PDA/Other >
batch file to move files


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
innirvana1's Avatar
Junior Member with 4 posts.
 
Join Date: Jan 2004
09-Jan-2004, 10:16 AM #1
batch file to move files
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's Avatar
Senior Member with 795 posts.
 
Join Date: Aug 1999
Location: Indian Head, MD "The land of large boxes."
09-Jan-2004, 10:27 AM #2
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.
__________________
mole
innirvana1's Avatar
Junior Member with 4 posts.
 
Join Date: Jan 2004
09-Jan-2004, 11:32 AM #3
*******
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's Avatar
Senior Member with 795 posts.
 
Join Date: Aug 1999
Location: Indian Head, MD "The land of large boxes."
09-Jan-2004, 11:52 AM #4
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 %%.
__________________
mole
innirvana1's Avatar
Junior Member with 4 posts.
 
Join Date: Jan 2004
09-Jan-2004, 03:27 PM #5
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's Avatar
Senior Member with 795 posts.
 
Join Date: Aug 1999
Location: Indian Head, MD "The land of large boxes."
09-Jan-2004, 03:45 PM #6
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.
__________________
mole
innirvana1's Avatar
Junior Member with 4 posts.
 
Join Date: Jan 2004
09-Jan-2004, 05:01 PM #7
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's Avatar
Senior Member with 795 posts.
 
Join Date: Aug 1999
Location: Indian Head, MD "The land of large boxes."
09-Jan-2004, 05:29 PM #8
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!
__________________
mole

Last edited by mole : 09-Jan-2004 05:36 PM.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:44 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.