Hi All,<\/p>\n
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.<\/p>\n
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.<\/p>\n
Thanks<\/p>\n
Daniel<\/p>","upvoteCount":6,"answerCount":4,"datePublished":"2020-01-31T22:04:45.000Z","author":{"@type":"Person","name":"danhall-tech","url":"https://community.spiceworks.com/u/danhall-tech"},"suggestedAnswer":[{"@type":"Answer","text":"
Hi All,<\/p>\n
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.<\/p>\n
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.<\/p>\n
Thanks<\/p>\n
Daniel<\/p>","upvoteCount":6,"datePublished":"2020-01-31T22:04:45.000Z","url":"https://community.spiceworks.com/t/writing-a-script-to-look-for-old-files/749328/1","author":{"@type":"Person","name":"danhall-tech","url":"https://community.spiceworks.com/u/danhall-tech"}},{"@type":"Answer","text":"
What have you tried?<\/p>\n
Maybe start here and fill in details<\/p>\n
$olddate = (get-date).addmonths(-1)\n$oldfiles = Get-ChildItem Q:\\ -rec | where date -let $olddate\n$oldfiles | foreach {\n $OF = $_.fullname\n $NF = \"... \" # figure out where you want to move it TO\n Move-Item $OF $NF\n}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2020-01-31T22:44:06.000Z","url":"https://community.spiceworks.com/t/writing-a-script-to-look-for-old-files/749328/2","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":"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.<\/p>\n
This code will run and produce a logfile listing the files that will be moved without moving them (/l option):<\/p>\n
cd q:\\\n\nrobocopy 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\n<\/code><\/pre>\nThis code will perform the move and log to the file:<\/p>\n
cd Q:\\\n\nrobocopy 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 \n<\/code><\/pre>\n