Hi All,

Looking to write a script that look in a directory say Q: and looks for files that were last accessed between 2010 and 2017, i then want to move them along with the directory to a new location to clear space on shared drives.

so say for example i have a file in Q:\video\2016\games\class1\gopro.mp4 i want to move it and keep the directory so that if someone says I needed that file i can put it back and know what they are asking for.

Thanks

Daniel

6 Spice ups

What have you tried?

Maybe start here and fill in details

$olddate = (get-date).addmonths(-1)
$oldfiles = Get-ChildItem Q:\ -rec | where date -let $olddate
$oldfiles | foreach {
  $OF = $_.fullname
  $NF = "...   "   # figure out where you want to move it TO
  Move-Item $OF $NF
}
1 Spice up

I know its not exactly powershell, but a tool that has been around a lot longer that was built for just this type of task is robocopy.

This code will run and produce a logfile listing the files that will be moved without moving them (/l option):

cd q:\

robocopy Q: "[Destination]" /s /z /copy:DATO /dcopy:DAT /mov /minlad:20100101 /maxlad:20171231 /fft /r:4 /w:15 /tbd /np /unilog+:"FilesThatWillBeMoved-log.txt" /l

This code will perform the move and log to the file:

cd Q:\

robocopy Q: "[Destination]" /s /z /copy:DATO /dcopy:DAT /mov /minlad:20100101 /maxlad:20171231 /fft /r:4 /w:15 /tbd /np /unilog+:"FilesThatWillBeMoved-log.txt" /l 

robocopy | Microsoft Docs

/s	Copies subdirectories. Note that this option excludes empty directories.
/z	Copies files in restartable mode.
/copy:<CopyFlags>
	Specifies the file properties to be copied. The following are the valid values for this option:
		D Data
		A Attributes
		T Time stamps
		S NTFS access control list (ACL)
		O Owner information
		U Auditing information
		The default value for CopyFlags is DAT (data, attributes, and time stamps)
/dcopy:<copyflags>	Defines what to copy for directories. Default is DA. Options are D = data, A = attributes, and T = timestamps.
/mov	Moves files, and deletes them from the source after they are copied.
/move	Moves files and directories, and deletes them from the source after they are copied.

/maxlad:<N>	Specifies the maximum last access date (excludes files unused since N).
/minlad:<N>	Specifies the minimum last access date (excludes files used since N) If N is less than 1900, N specifies the number of days. Otherwise, N specifies a date in the format YYYYMMDD.

/fft	Assumes FAT file times (two-second precision).

/r:<N>	Specifies the number of retries on failed copies. The default value of N is 1,000,000 (one million retries).
/w:<N>	Specifies the wait time between retries, in seconds. The default value of N is 30 (wait time 30 seconds).

/tbd	Specifies that the system will wait for share names to be defined (retry error 67).

/l	Specifies that files are to be listed only (and not copied, deleted, or time stamped).
/np	Specifies that the progress of the copying operation (the number of files or directories copied so far) will not be displayed.
/unilog+:<LogFile>	Writes the status output to the log file as Unicode text (appends the output to the existing log file).
/tee	Writes the status output to the console window, as well as to the log file.

Thanks @fids74gf ​ for another answer.

I prefer to use PowerShell commands/cmdlets if I can. Robocopy is a GREAT tool, don’t get me wrong. But for many operations, using cmdlets gives you a bit more flexibility. I also think that structured code is easier to read and understand, which helps if you later have to update your scripts.

MILEAGE VARIES - just my point of view.

1 Spice up