Was looking this over and I found this to be very useful for a task I have with a external hard drive that is loaded with scattered data.
I added the
/s switch to focus also on all files in all sub directories starting at the root trigger point of the batch to xcopy these files as well into the grouped by file extension file folders.
I also got rid of the DEL command because I dont want to destroy the file structure, although total chaos, and end up with just .fileextension for each file extension discovered and file placed within, but instead added this logging to file ability in place of it
@echo.%%A>>Copied.txt to have a list of every file copied.
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('dir /b /s /a-d') do (
set count=1
if not exist "%CD%\%%~xA" MD "%CD%\%%~xA"
if not exist "%CD%\%%~xA\%%A" (xcopy "%%A" "%CD%\%%~xA") else (
:1
if not exist "%CD%\%%~xA\%%A(!count!)" (xcopy "%%A(!count!)" "%CD%\%%~xA") else (
set /a count=!count!+1
goto 1)
)
@echo."%%A">>Copied.txt)
But what is strange and I cant figure out is why when finding an alternate of the same name it is not adding (1) or (2) etc at the end of the name to keep same file names seperate. It prompts do you want to overwrite instead of adding the (1) etc to the end of the file name such as 2007taxes(1).pdf and 2007Taxes(2).pdf ... Any idea what may be going wrong?
I thought that maybe instead of IF NOT EXIST for
if not exist "%CD%\%%~xA\%%A(!count!)" (xcopy "%%A(!count!)" "%CD%\%%~xA") else (
set /a count=!count!+1
goto 1)
should be IF EXIST then increment +1, but when trying to fix this I get stuck in endless loop because the condition doesnt change to exit the loop.