Krom
Krom

Reputation: 37

Commands to move folders and files within of a certain age to another Directory

I have a directory that is quite large and would like to move the folders/files that are older than let's say 90 days to another directory.

My starting point is this cmd command

forfiles /s /m *.* /d -90 /c "cmd /c echo @file is at least 90 days old."

This returns the files that are older than 90 days, but I need to replace the echo with a move command. I am not sure how this will work with regard to bringing the files over intact with their parent directories.

Would this work to keep the folder structure?

forfiles /s /m *.* /d -90 /c "move @file c:\temp"

Would anyone have any advice or foresee any issues in trying to do it this way?

Robocopy is not an option.

Upvotes: 0

Views: 6701

Answers (1)

aschipfl
aschipfl

Reputation: 34929

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)"

Upvotes: 4

Related Questions