Computer Hope
Microsoft => Microsoft DOS => Topic started 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
-
You mean sth like this?
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 ?
-
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.
-
@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!"
)
)
-
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
-
well i have that option
Where?
I know I can add the -y switch to be prompted but there's too many prompts.
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.
-
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.
-
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?
-
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
-
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?
-
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?
@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
)
)
)
-
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) ?
-
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.
-
Don't know what the do ) means
Those are loops.
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.
@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
)
)
)
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.
-
Thanks man!
what does the %%D mean?
and the %%F?
-
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
)
)
)
-
explain about the 0-9 folder
-
Thats a folder for filenames that begin with a number
-
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
)
)
)
-
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...
-
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.
-
sleep well
@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
)
)
)
-
When I try running these I get the error: The system cannot find the file specified
Can't figure out what's wrong
-
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 :-)
-
do you see the "source file: !src!" message?
-
No only message I see is that the file can't be found and it repeats a number of times then the window closes
-
the ! symbol has a special meaning if delayed expasion is enabled.
Try changing these lines thus
set src="L:\!Temp_all\%%F"
set dest="L:\!TLF\%%D\%%F"
-
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
)
)
)
-
Try this
Please state what is reported by the echo lines
@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
)
)
)
-
The system cannot find the file specified. - that's all it says about 20 times then quits
-
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?
-
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.
-
I should have realised this before. In batch scripting, ! is a special character and is causing the problem.
-
ah... I'll have to work on that. Guess I need to rename the folder again :-)
-
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.
-
Post the code you are now using - there are probably some quote marks needed in certain places.
-
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
)
)
)
-
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
-
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