recursive move command on windows
Asked Answered
V

7

12

I need to do a .bat copy of a .sh, I don't know much Windows cmd. On Linux I could do

mv ...

or

rsync -a SOURCE/ DEST/ --remove-sent-files --ignore-existing --whole-file

but Windows "move" can't do the same

maybe there is a windows simple alternative, simpler and more performant than

for /R c:\sourceFolder\ %%G in (*) do ( move /Y "%%G" c:\destinationFolder\ )

Linux mv seems to update directories pointer, but the above Windows command will do hard stuff? I guess it's not a good idea for the big folders I need to frequently move

Vlada answered 14/4, 2012 at 16:47 Comment(1)
Use robocopy, included since Vista, or the older xcopy (which is capable, but requires more work adding switches to get it to work right).Pastor
M
10

The move command can move directories as well as files.

cd /d C:\sourceFolder
rem move the files
for %%i in (*) do move "%%i" C:\destinationFolder
rem move the directories
for /d %%i in (*) do move "%%i" C:\destinationFolder
Mallon answered 16/4, 2012 at 20:5 Comment(5)
shouldn't this be for %i? (single %?)Melise
@Melise You need %% if using a batch file, and the question explicitly said it was for a batch file. (Note that the original question also doubled the percent signs.)Mallon
ah, thank you! i was trying it directly through the command prompt and was wondering why it wasn't working. thanks!Melise
Is it possible to use the folder path instead of (*)?Acroterion
So `move * somewhere` does move all files, but directories need to be moved one by one. Sigh...Courcy
B
18

Robocopy did wonders for me:

 robocopy c:\cache c:\cache-2012 ?????-2012*.hash /S /MOV

I used it to move all files with certain mask out of c:\cache and its numerous subdirectories.

Bray answered 1/6, 2013 at 5:11 Comment(2)
best answer for me (since it is part of recent windows versions)Hacker
The main problem with robocopy is it doesn't actually move files, but copy them and them delete the original ones. When moving files in the same drive, it's a big difference to be able to actually move the files (it's instant) instead of just copying them (it's a lot slower if you want to move big files). The main point of using the "move" command is it's really fast if moving files in the same drive, but unfortunately it doesn't move subdirectories when using wildcards.Ava
M
10

The move command can move directories as well as files.

cd /d C:\sourceFolder
rem move the files
for %%i in (*) do move "%%i" C:\destinationFolder
rem move the directories
for /d %%i in (*) do move "%%i" C:\destinationFolder
Mallon answered 16/4, 2012 at 20:5 Comment(5)
shouldn't this be for %i? (single %?)Melise
@Melise You need %% if using a batch file, and the question explicitly said it was for a batch file. (Note that the original question also doubled the percent signs.)Mallon
ah, thank you! i was trying it directly through the command prompt and was wondering why it wasn't working. thanks!Melise
Is it possible to use the folder path instead of (*)?Acroterion
So `move * somewhere` does move all files, but directories need to be moved one by one. Sigh...Courcy
O
2

I know this is an old thread, but since it does not have a correct answer I figured I'd tie it off.

The old DOS command to accomplish this is:

   move <source directory> <destination directory>

So in the OP question:

   move C:\sourceFolder c:\destinationFolder

The folder and everything in the folder (including sub-directories) will be moved.

Oilcan answered 6/6, 2019 at 23:50 Comment(1)
Just remember that move throws "access denied" if target folder existsHogan
V
1

XCOPY should do the trick, I use it it in batch files all the time

something like, if you're just trying to target .sh files

XCOPY /E /H /Y /C "%SOURCEDIR%\*.sh" "%TARGETDIR%"  

Let me know if you have more questions

Viridi answered 16/4, 2012 at 19:46 Comment(1)
xcopy doesn't move files though, just copies them, so you need to delete everything in the source directory afterwardsMaureen
B
0
@echo off
setlocal
set DIR=
set OUTPUTDIR=C:\Documents and Settings\<username>\Desktop\sandbox1\output
for /R %DIR% %%a in (*.jpg) do xcopy "%%a" "%OUTPUTDIR%"
Bidding answered 14/11, 2017 at 0:14 Comment(0)
D
0

I don't have a one liner, but here's what I did using CMD and Notepad++...

  1. F:> DIR /S /B > moveit.bat This gives you all your file names

  2. Use Notepad++ to edit the bat file Replace with Regular Expression, search for ^(.+?)$ and replace with MOVE "$1" %DESTDIR% Save, exit

  3. Execute your batch file.

Discernible answered 27/7 at 19:42 Comment(0)
B
-2

For recursive move in windows, a simple move command is ok. Here is the example, I think it would be helpful.

move D:\Dbbackup\*.dmp* D:\Dbbackup\year\month\

Where .dmp is the extension of the file that would be moved to the location recursive folder Dbbackup , then year, then month.

Berserk answered 8/1, 2014 at 5:27 Comment(1)
That's not a recursive move. Subdirectories will not be moved with that command. The move command doesn't support that, AFAIK.Ava

© 2022 - 2024 — McMap. All rights reserved.