Question

The question is as the title says. I made a flawed move when moving a selection of files using a batch... It's too late now as I closed it accidentally, but for future reference I'd like the knowledge. The code is as follows:

@ECHO OFF
SETLOCAL
SET relroot=g:\Pictures\Uda 18
(SET relroot=g:\Pictures\Uda 18)
SET "relroot=g:\Pictures\Uda 18"
SET "destdir=g:\Pictures\Uda 18\Sets"
:again
(SET artist=)
SET /p artist="Artist? "
IF NOT DEFINED artist GOTO :eof
MD "%destdir%\%artist%" 2>nul
FOR /f "delims=" %%i IN ( 
   ' dir /s /b /a-d "%relroot%\*%artist%*" '
   ) DO (
 ECHO %%i
MOVE "%%i" "%destdir%\%artist%\%%~nxi" >nul)
)
GOTO again

The code's purpose it to take an input, find all files with the input in their name within the directory 'Uda 18', and move them to a folder named after the input under the directory 'Uda 18/Sets'. Without thinking, I input '_n.jpg' to move all such files, and it performed the operation. But then I realised I'd moved even things from within 'Uda 18/Sets', and effectively de-organised files I had previously organised by hand. I now realise I need to exclude said directory from the search, but that's not the focus of this question.

Was it helpful?

Solution

You can build a "backmove" function. First modify your for loop and create the file undo.txt:

FOR /f "delims=" %%i IN ( 
   ' dir /s /b /a-d "%relroot%\*%artist%*" '
   ) DO (
>>undo.txt ECHO %%i^|%destdir%\%artist%\%%~nxi
MOVE "%%i" "%destdir%\%artist%\%%~nxi" >nul)

.. and to "undo" run the backmove function from the folder with the undo.txt file:

:backmove
for /f "tokens=1*delims=|" %%i in (undo.txt) do move "%%~j" "%%~i"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top