How to move folders with a loop over folders (in batch mode)?

Situation:

I am trying to move files inside a loop to a shell, but my code is not working.

for /D %%F in (*) do (
   if "%%F" NEQ "%directoryToPutFilesIn%" (
     move /y "%%F" "%directoryToPutFilesIn%"
  )
)

After several hours of testing, I realized this because %% F points to a folder, so the file cannot be moved.

Bad solution:

As I did, I confirmed my suspicions by storing the %% F value in another variable and using this variable the next turn to move the file. Please note that subsequent work requires initialization %precedentFile%for the first move.

for /D %%F in (*) do (
move /y "%precedentFile%" "%directoryToPutFilesIn%"
   if "%%F" NEQ "%directoryToPutFilesIn%" (
     move /y "%%F" "%directoryToPutFilesIn%"
     set precedentFile=%%F
  )

Problem:

This decision is impractical and does not feel right. Is there a way to adapt my current code to do this, or just another way?

+4
source share
1 answer

script:

for /f %%a in ('dir /a:-D /b') do move /Y "%%~fa" "%directoryToPutFilesIn%"

:

dir /a:-D /b : This command will list all files in the directory 
move /Y "%%~fa" "%directoryToPutFilesIn%" : This will move all files in the directory where this command is executed to the destination you have mentioned.
%%~fa : This command will get full qualified path of the file with it name.

. : "" , . H:\Drive,

for /D %%b in (*) do move /Y "%%~fb" "H:\"
+2

Source: https://habr.com/ru/post/1598951/


All Articles