Search code examples
batch-filedirectorymove

Batch how to move hidden directories?


I am using the following script lines in a batch script (.bat) to copy the contents of a directory (foo) to another (bar):

move "C:\foo\*.*" "C:\bar\"
for /d %%a in ("C:\foo\*") do move "%%~fa" "C:\bar\"

The first line moves files and the second lines moves folders. However, these aren't moving the hidden directories. .git is a common example. I tried for /d %%a in ("C:\foo\.*") do move "%%~fa" "C:\bar\" with no success.

How can I move my hidden directories along with the rest of my files and directories?

EDIT: The following solution is very close to doing what is required, but fails because the "move" command can't find the hidden folder (tried the same on a .folder that wasn't hidden and it worked):

for /f "tokens=*" %%G in ('dir /b /a:hd "C:\foo\*"') do move "C:\foo\%%G" "C:\bar\"

Solution

  • After some extra research, I found that robocopy seems to be included by default in Windows 10 distributions and robocopy /MOVE allows moving all the needed files and folders in a single line, such as:

    robocopy "C:\foo" "C:\bar" /E /MOVE
    

    Additional logging options can be added to reduce the output to the command line.