Computer Hope

Microsoft => Microsoft DOS => Topic started by: gitman7 on July 25, 2008, 10:34:14 AM

Title: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 10:34:14 AM
I have a DOS batch file that moves files from one directory and sorts them into separate folders alphabetically. The problem is existing files get replaced. I know I can add the -y switch to be prompted but there's too many prompts. Does anyone know how I can skip if file exists automatically or better yet, rename it? I don't see that option anywhere in dos move command. Was wondering if I can use the "IF" variable somehow. Like IF exists skip or no replace etc. Can't find answer anywhere on web. TIA any help. Here is an excerpt from my batch file:

move a*.* D:\!TLF\a
move b*.* D:\!TLF\b
move c*.* D:\!TLF\c
move d*.* D:\!TLF\d
move e*.* D:\!TLF\e
move f*.* D:\!TLF\f
move g*.* D:\!TLF\g
Title: Re: Move batch command with no replace if filename exists?
Post by: devcom on July 25, 2008, 10:43:27 AM
You mean sth like this?
Code: [Select]
for %%F in (a b c d e f g) do (
if exist %%F*.* (
move %%F*.* D:\!TFL\%%F
)
)

edit
does't move have option to not replace ?
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 11:35:15 AM
in response to: "does't move have option to not replace ?"

No. That's the problem I'm having. Move command doesn't have an option to skip if filename exists. I need to add some kind of conditional variable.
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 25, 2008, 11:44:53 AM
Code: [Select]
@echo off
setlocal enabledelayedexpansion
for %%D in (a b c d e f g) do (
    for /f %%F in ('dir /a-d /b %%D*.*') do (
                set src=%%F
                set dest=%%D\%%F
                if not exist "!dest!" move "!src!" "!dest!"
        )
    )
Title: Re: Move batch command with no replace if filename exists?
Post by: devcom on July 25, 2008, 11:55:29 AM
Code: [Select]
Moves files and renames files and directories.

To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination

To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2

  [drive:][path]filename1 Specifies the location and name of the file
                          or files you want to move.
  destination             Specifies the new location of the file. Destination
                          can consist of a drive letter and colon, a
                          directory name, or a combination. If you are moving
                          only one file, you can also include a filename if
                          you want to rename the file when you move it.
  [drive:][path]dirname1  Specifies the directory you want to rename.
  dirname2                Specifies the new name of the directory.

  /Y                      Suppresses prompting to confirm you want to
                          overwrite an existing destination file.
  /-Y                     Causes prompting to confirm you want to overwrite
                          an existing destination file.

well i have that option

sorty for first post coz i was on phone and did not know about move
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 25, 2008, 12:02:32 PM
Quote from: devcom
well i have that option

Where?

Quote from: gitman7
I know I can add the -y switch to be prompted but there's too many prompts.

Quote from: gitman7
That's the problem I'm having. Move command doesn't have an option to skip if filename exists.

You have the options

/Y to overwrite existing files without asking
/-Y to ask for permission to overwrite

but nowhere that I can see is an option to simply skip files that already exist.

My code above will skip if a destination file already exists.
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 12:06:49 PM
exactly.  there has got to be a way I would think to put an "IF EXIST" command in there somehow.  For example if the filename exists then enter "n" keystroke so I don't have to respond to every prompt or just skip the move command if filename exists. 
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 25, 2008, 12:47:48 PM
exactly.  there has got to be a way I would think to put an "IF EXIST" command in there somehow.  For example if the filename exists then enter "n" keystroke so I don't have to respond to every prompt or just skip the move command if filename exists. 

did you see the batch file I posted above?
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 02:02:22 PM
Yes but I don't understand it at all - way over my head

What is %%F?

and I don't understand this:  setlocal enabledelayedexpansion
or any of the rest of it for that matter.  sorry.  I'm not giving up tho

I'm doing some web searching on that right now
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 02:10:37 PM
What should my batch file look like if I wanted to move all the files from C:\BIN  to  C:\BIN\a\ C:\BIN\b\ C:\BIN\c\  etc. where the first letter of the file matches the letter folder without overwriting existing files in those directories?
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 25, 2008, 02:35:59 PM
What should my batch file look like if I wanted to move all the files from C:\BIN  to  C:\BIN\a\ C:\BIN\b\ C:\BIN\c\  etc. where the first letter of the file matches the letter folder without overwriting existing files in those directories?

Code: [Select]
@echo off
setlocal enabledelayedexpansion
for %%D in (a b c d e f g) do (
    for /f %%F in ('dir /a-d /b s:\bin\%%D*.*') do (
                set src=s:\bin\%%F
                set dest=s:\bin\%%D\%%F
                echo Source file: !src!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                move "!src!" "!dest!"
                ) else (
                echo !dest! already exists
                )
        )
    )
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 02:49:17 PM
Thanks for that!  I'm going to study it and try to figure it out.

Don't know what the do )  means
or the  set src=s:  what is the s for?

and do I need every letter of alphabet in this: (a b c d e f g) ?

Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 03:04:35 PM
My actual source directory is L:\!Temp all 
And my destination folders are  L:\!TLF\!,  L:\!TLF\0, L:\!TLF\1-9, L:\!TLF\a, L:\!TLF\b, etc. up through every letter of the alphabet.  So all files that begin with "a"  go into the "a" folder etc.

I'm trying to rewrite your batch so it works here but can't quite figure it out.

Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 25, 2008, 03:06:29 PM
Quote from: gitman7
Don't know what the do )  means

Those are loops.

Quote from: gitman7
or the  set src=s:  what is the s for?

Sorry I was trying it out on my computer on drive s. I forgot to change it. Below is correct code.

Code: [Select]
@echo off
setlocal enabledelayedexpansion
for %%D in (a b c d e f g) do (
    for /f %%F in ('dir /a-d /b c:\bin\%%D*.*') do (
                set src=c:\bin\%%F
                set dest=c:\bin\%%D\%%F
                echo Source file: !src!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                 move "!src!" "!dest!"
                 ) else (
                 echo !dest! already exists
                 )
       )
    )

Quote from: gitman7
and do I need every letter of alphabet in this: (a b c d e f g) ?

Well, you know that better than I do.




Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 03:17:23 PM
Thanks man!   

what does the %%D mean?

and the %%F?

Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 03:24:34 PM
So does this look right: ?  If my source files are all in "L:\!Temp all"  and the destination folders are all in L:\!TLF\a, etc. with every letter of alphabet in separate folder?

@echo off
setlocal enabledelayedexpansion
for %%D in (! 0-9 a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
    for /f %%F in ('dir /a-d /b L:\!Temp all\%%D*.*') do (
                set src=L:\!Temp all\%%F
                set dest=L:\!TLF\%%D\%%F
                echo Source file: !src!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                   move "!src!" "!dest!"
                   ) else (
                   echo !dest! already exists
                   )
             )
    )
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 25, 2008, 04:56:46 PM
explain about the 0-9 folder
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 09:54:50 PM
Thats a folder for filenames that begin with a number
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 10:03:11 PM
I can skip the files that start with numbers and do those manually.  Would this be right then?

@echo off
setlocal enabledelayedexpansion
for %%D in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
    for /f %%F in ('dir /a-d /b L:\!Temp all\%%D*.*') do (
                set src=L:\!Temp all\%%F
                set dest=L:\!TLF\%%D\%%F
                echo Source file: !src!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                   move "!src!" "!dest!"
                   ) else (
                   echo !dest! already exists
                   )
             )
    )
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 25, 2008, 11:41:50 PM
I can skip the files that start with numbers and do those manually.  Would this be right then?

Since the folder name L:\!Temp all has a space in it, it needs to be quoted thus

for /f %%F in ('dir /a-d /b "L:\!Temp all"\%%D*.*') do (

Otherwise it seems OK. You could try it out with some dummy data or you could change this line

move "!src!" "!dest!"

to this

echo  move "!src!" "!dest!"

& run the batch to see what happens & change it back when you are happy.

I note that the ! filenames got removed too...



 
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 25, 2008, 11:58:36 PM
Wow you rock!  Thanks man.  I tried a few different forums and lots of web pages - I even got all my old books out - one was DOS 3.20 for Blue Chip computer from 1984!  lol  Didn't even have the move command but had xcopy and a few other things.  I still don't understand the whole thing but I think I got most of it. This is the best forum. 

Thanks again - I'm gonna try the echo thing and see how it does.  Tomorrow :-)  Time for bed.
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 26, 2008, 12:10:59 AM
sleep well

Code: [Select]
@echo off
setlocal enabledelayedexpansion
for %%D in (! a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
    for /f %%F in ('dir /a-d /b "L:\!Temp all"\%%D*.*') do (
                set src=L:\!Temp all\%%F
                set dest=L:\!TLF\%%D\%%F
                echo Source file: !src!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                   move "!src!" "!dest!"
                   ) else (
                   echo !dest! already exists
                   )
             )
    )


for %%D in (0 1 2 3 4 5 6 7 8 9) do (
    for /f %%F in ('dir /a-d /b "L:\!Temp all"\%%D*.*') do (
                set src=L:\!Temp all\%%F
                set dest=L:\!TLF\0-9
                echo Source file: !src!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                   move "!src!" "!dest!"
                   ) else (
                   echo !dest! already exists
                   )
             )
    )
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 28, 2008, 08:45:52 AM
When I try running these I get the error:  The system cannot find the file specified 

Can't figure out what's wrong
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 28, 2008, 08:56:02 AM
I even tried renaming the directory !Temp all to !Temp_all to remove the space and changed the bat file to this:  (just removed the quotes)

@echo off
setlocal enabledelayedexpansion
for %%D in (! a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
    for /f %%F in ('dir /a-d /b L:\!Temp_all\%%D*.*') do (
                set src=L:\!Temp_all\%%F
                set dest=L:\!TLF\%%D\%%F
                echo Source file: !src!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                   echo move "!src!" "!dest!"
                   ) else (
                   echo !dest! already exists
                   )
             )
    )

But I still get the error.

The source and destination directories are correct.  I'm not sure how the %%D and %%F are supposed to work.  Or the letter and number folders in the parenthesis - I even tried putting this .bat file in the L:\!Temp_all folder to see if that helped but same error.  TIA any more help :-)
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 28, 2008, 10:43:13 AM
do you see the "source file: !src!" message?
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 29, 2008, 02:25:06 PM
No only message I see is that the file can't be found and it repeats a number of times then the window closes
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 29, 2008, 02:36:03 PM
the ! symbol has a special meaning if delayed expasion is enabled.

Try changing these lines thus

Code: [Select]
set src="L:\!Temp_all\%%F"
set dest="L:\!TLF\%%D\%%F"
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 29, 2008, 03:23:43 PM
Still doesn't work.  This is what I have now:    (I put the echo in too to test)

@echo off
setlocal enabledelayedexpansion
for %%D in (! a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
    for /f %%F in ('dir /a-d /b L:\!Temp_all\%%D*.*') do (
                set src="L:\!Temp_all\%%F"
                set dest="L:\!TLF\%%D\%%F"
                echo Source file: !src!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                   echo move "!src!" "!dest!"
                   ) else (
                   echo !dest! already exists
                   )
             )
    )
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 29, 2008, 03:31:32 PM
Try this

Please state what is reported by the echo lines

Code: [Select]
@echo off
setlocal enabledelayedexpansion
for %%D in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
    for /f %%F in ('dir /a-d /b L:\!Temp_all\%%D*.*') do (
               
                set src="L:\!Temp_all\%%~nxF"
                set dest="L:\!TLF\%%D\%%~nxF"
                echo File name : %%F
                echo Source file: !src!
                echo Dest file    : !dest!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                   echo move "!src!" "!dest!"
                   ) else (
                   echo !dest! already exists
                   )
             )
    )
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 29, 2008, 04:09:03 PM
The system cannot find the file specified.    - that's all it says about 20 times then quits
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 30, 2008, 12:14:24 AM
Is the directory name "L:\!Temp_all" correctly spelled? You said it was called "L:\!Temp all" at first. Are there actually any files in that folder?

Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 30, 2008, 11:01:46 PM
Yes it's correctly spelled.  It was originally !Temp all but then I renamed it !Temp_all to make sure there wasn't any issues with there being a space in the folder name.  There are lots of files in the folder.
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on July 31, 2008, 12:29:31 AM
I should have realised this before. In batch scripting, ! is a special character and is causing the problem.
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on July 31, 2008, 01:23:04 AM
ah...  I'll have to work on that.  Guess I need to rename the folder again :-)
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on August 04, 2008, 09:55:10 PM
That worked!  I just had to remove the ! and rename the directories.  I noticed it doesn't work on any files that have spaces in the filenames though.  Is there a way to fix that or is that a DOS thing you can't get around.  Thanks again for all your help. 
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on August 05, 2008, 12:12:23 AM
Post the code you are now using - there are probably some quote marks needed in certain places.
Title: Re: Move batch command with no replace if filename exists?
Post by: gitman7 on August 05, 2008, 01:55:51 PM
This is what I'm using:

echo off
setlocal enabledelayedexpansion
for %%D in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
    for /f %%F in ('dir /a-d /b L:\Temp_all\%%D*.*') do (
               
                set src="L:\Temp_all\%%~nxF"
                set dest="L:\TLF\%%D\%%~nxF"
                echo File name : %%F
                echo Source file: !src!
                echo Dest file    : !dest!
                if not exist "!dest!" (
                        echo Moving !src! to !dest!
                   move "!src!" "!dest!"
                   ) else (
                   echo !dest! already exists
                   )
             )
    )
Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on August 05, 2008, 02:37:40 PM
try this

for /f "delims==" %%F in ('dir /a-d /b L:\Temp_all\%%D*.*') do (

the delims section should fix the spaces problem

[update]

I tested it & it seems to work

Title: Re: Move batch command with no replace if filename exists?
Post by: Dias de verano on August 06, 2008, 12:58:11 AM
The reason the previous code did not work with filenames with spaces is that, by default, the FOR /F behaviour is to treat the lines processed (in this case, the lines of text output by DIR /a-d /b) as a series of space-delimited "tokens". That is, if it found the line

c:\my folder\whatever path\something.txt

It would treat the lines as having 3 tokens and %%A would only contain the first token

c:\my

because FOR /F would stop at the first space.

This behaviour can be modified with a suitable "delims=" section. I won't go into what "tokens=" does, except to say that you can use it to chop up the line into tokens and isolate them.

The modifier "delims==" instructs FOR /F to consider the delimiters to be the line start and finish, thus preserving the whole line.

The problem would have become visible earlier if the path to the source folder had spaces, since these would have cause the batch to not function at all