forfiles
executes the command line after the /C
option with the currently iterated directory as the working directory. This is also true with the /S
option.
The reference @file
returns the pure (quoted) file name; the reference @relpath
returns a path relative to the given root (behind the /P
option, which defaults to the current directory).
So you could try something like this (note that the cmd /C
prefix is required for cmd
-internal commands like move
, echo
or if
; the upper-case ECHO
just displays the move
command line that would be executed without):
forfiles /S /D -90 /C "cmd /C if @isdir==FALSE ECHO move @relpath 0x22C:\temp\0x22"
This would move all files into the directory C:\temp
, losing the original directory hierarchy however. (Note that the if @isdir==FALSE
query prevents sub-directories from being processed.)
Therefore we need to build the destination directories on our own, like this:
forfiles /S /D -90 /C "cmd /C if @isdir==FALSE (for %F in (@relpath) do @(2> nul mkdir 0x22C:\temp\%~F\..0x22 & ECHO move @relpath 0x22C:\temp\%~F0x22))"
What happens here:
- in general,
0x22
represents a quotation mark "
;
if @isdir==FALSE
ensures to process files only;
- the
for
loop just removes surrounding quotes from the path retrieved by @relpath
when accessed by %~F
; ensure to double the %
signs in case you are using the code in a batch file!
mkdir
creates the parent directory of the currently iterated item; 2> nul
hides the error message in case the directory already exists;
move
is preceded by ECHO
for testing purposes; remove it to actually move files;
If you want to overwrite files in the destination location, simply add the /Y
option to move
.
The following command line might also work for the sample path, but it is going to fail for sure in case the destination path contains SPACEs or other poisonous characters, because quotation is not properly handled:
forfiles /S /C "cmd /C if @isdir==FALSE (2> nul mkdir C:\temp\@relpath\.. & ECHO move @relpath C:\temp\@relpath)"