I am trying to get a list of folders that have a specific file *.t
in it (there is not more than one such file in a folder):
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('Dir /B /A:-D /S "D:\Old\Stuff\*.t"') do
(@echo %%~dpa>>Folders.txt)
endlocal
This works and gives me a file with the full path and a \
at the end...
e.g.
D:\Old\Stuff\Folder 1\
D:\Old\Stuff\Folder 2\
D:\Old\Stuff\Folder 3\
I then want to move said folders with it's contents to another location, but it's not working:
for /f "delims=" %%i in (Folders.txt) do Move %%i "D:\New\Stuff\"
What am I doing wrong?
Since there are no duplicates possible, the solution is quite simple; you do not even need a temporary file Folders.txt
:
for /F "delims=" %%F in ('dir /S /A:-D /B "D:\Old\Stuff\*.t"') do (
move "%%~dpF." "D:\New\Stuff\"
)
%%~dpF
returns the parent directory of each found *.t
file with a trailing backslash. Just append a .
, meaning the current directory, so the trailing \
does no longer cause problems with the move
command.
Do not attempt to remove the trailing backslash, because this could cause problems in some special situations: assume the target directory is D:\
, so removing the \
leaves D:
which is not the same as D:\
(but D:\.
is).
Even in case there could be more than one *.t
file in a certain directory, the code would work, although the move
command threw an error for every duplicate, because the directory has already been moved before. Simply appending or prepending 2> nul
to the move
command line hid those (and other) errors.