| 1. |
Solve : Move batch command with no replace if filename exists?? |
|
Answer» 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: 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. 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. Quote from: 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. 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 nowWhat 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? Quote from: 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? 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 ) ) )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. 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. Thanks man! what does the %%D mean? and the %%F? |
|