Jump to content

Hi guys,
I have a Windows Server 2012r2 server at home, which contains many files and primarily is a file server. I want to get some sorting happening and I was hoping I would be able to script it (I don't know any programming languages unfortunately). I have attached a picture, which is like an algorithm, on how I want the script to run. Basically as the end result I want to have 2 folders, one filled with "Other files" and the other filled with "video files" that were created 12 months ago. But there are alot of other files I dont want, like already extracted Rars and jpgs which I want deleted. Any help would be appreciated.

(Note: I plan on running the script monthly)
 

Untitled.png

Link to comment
https://linustechtips.com/topic/538423-automated-script/
Share on other sites

Link to post
Share on other sites

27 minutes ago, ziogref said:

Hi guys,
I have a Windows Server 2012r2 server at home, which contains many files and primarily is a file server. I want to get some sorting happening and I was hoping I would be able to script it (I don't know any programming languages unfortunately). I have attached a picture, which is like an algorithm, on how I want the script to run. Basically as the end result I want to have 2 folders, one filled with "Other files" and the other filled with "video files" that were created 12 months ago. But there are alot of other files I dont want, like already extracted Rars and jpgs which I want deleted. Any help would be appreciated.

(Note: I plan on running the script monthly)

PowerShell is probably the way to go for this. Here's a start: 

$Date = Get-Date
$Date = $Date.addyears(-1)
$DateString1 = $Date.ToString("yyyMMdd")
$Files = gci "C:\\Users\\ziogref\\Desktop"

ForEach ($File in $Files)
{
    $FileDate = $File.LastWriteTime
    $DateString2 = $FileDate.ToString("yyyyMMdd")
    if ($DateString2 -lt $DateString1) {Copy-Item $File.Fullname "C:\\Temp"; exit}
}
Throw "No files found."

This loops through your desktop and moves any files greater than a year old into a temp folder - I think, bit rusty with PowerShell ;)

 Almost as cool as my temps  

Link to comment
https://linustechtips.com/topic/538423-automated-script/#findComment-7136683
Share on other sites

Link to post
Share on other sites

1 minute ago, Benergy said:

PowerShell is probably the way to go for this. Here's a start: 


$Date = Get-Date
$Date = $Date.addyears(-1)
$DateString1 = $Date.ToString("yyyMMdd")
$Files = gci "C:\\Users\\ziogref\\Desktop"

ForEach ($File in $Files)
{
    $FileDate = $File.LastWriteTime
    $DateString2 = $FileDate.ToString("yyyyMMdd")
    if ($DateString2 -lt $DateString1) {Copy-Item $File.Fullname "C:\\Temp"; exit}
}
Throw "No files found."

This loops through your desktop and moves any files greater than a year old into a temp folder - I think, bit rusty with PowerShell ;)

Didnt think of powershell, I might go through and have a look and see if i can come up with anything and I will post back.

I will create a test environment so I dont screw anything up

Link to comment
https://linustechtips.com/topic/538423-automated-script/#findComment-7136718
Share on other sites

Link to post
Share on other sites

1 hour ago, Benergy said:

PowerShell is probably the way to go for this. Here's a start: 


$Date = Get-Date
$Date = $Date.addyears(-1)
$DateString1 = $Date.ToString("yyyMMdd")
$Files = gci "C:\\Users\\ziogref\\Desktop"

ForEach ($File in $Files)
{
    $FileDate = $File.LastWriteTime
    $DateString2 = $FileDate.ToString("yyyyMMdd")
    if ($DateString2 -lt $DateString1) {Copy-Item $File.Fullname "C:\\Temp"; exit}
}
Throw "No files found."

This loops through your desktop and moves any files greater than a year old into a temp folder - I think, bit rusty with PowerShell ;)

I tested you script and I got this error

Quote

 

You cannot call a method on a null-valued expression.
At C:\test\Powershellscript.PS1:9 char:1
+ $DateString2 = $FileDate.ToString("yyyyMMdd")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

DateString2 : The term 'DateString2' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\test\Powershellscript.PS1:10 char:5
+ if (DateString2 -lt $DateString1) {Copy-Item $File.Fullname "C:\test\ ...
+     ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (DateString2:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

No files found
At C:\test\Powershellscript.PS1:12 char:1
+ Throw "No files found"
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (No files found:String) [], RuntimeException
    + FullyQualifiedErrorId : No files found

 

 

Here is my Script (noting there are file in that test directory)

Quote

 

$Date = Get-Date
$Date = $Date.Addyears(-1)
$DateString1 = $Date.ToString("yyyyMMdd")
$Files = gci "C:\test\source"

ForEach ($File in $Files)
{
$FileDate = $File.LastWrittenTime
$DateString2 = $FileDate.ToString("yyyyMMdd")
if (DateString2 -lt $DateString1) {Copy-Item $File.Fullname "C:\test\dest"; exit}
}
Throw "No files found"

 

 

Link to comment
https://linustechtips.com/topic/538423-automated-script/#findComment-7137335
Share on other sites

Link to post
Share on other sites

So this is what I have found so far

Quote

PS C:\test> Get-ChildItem -Path "C:\test\source" -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddYears(-1)}
| Move-Item -Destination "C:\test\dest"

This will copy all files and folders from source to dest if the requirements meet. However 1 strange this it does (which isnt a concern for me) is if the a folder doesnt meet the criteria but the file in said folder does it will move the file and not the folder and just placing the file in the parent directory. Also since this is a move all file information is retained (file creation date, modification and permissions). Now im going to work on trying to extract files out of folders, I suspect I will have to use a lot of filters for known file types, but I will post back.

Link to comment
https://linustechtips.com/topic/538423-automated-script/#findComment-7138099
Share on other sites

Link to post
Share on other sites

Ok this is what I Have

Quote

cd C:\test
Get-ChildItem -Path "C:\test\source" -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddYears(-1)} | Move-Item -Destination "C:\test\temp"
::This moves all file from "source" to "dest if age is greater than 1 year

cd C:\test\temp
Get-ChildItem -rec -include *.* | Move-Item -destination "C:\test\dest"
::This removes all the files and places them in the one directory

cd C:\test\temp
Remove-Item *
::Deletes all items in Source folder

cd C:\test\dest
Get-ChildItem -include *.1*,*.2*,*.3*,*sample* -recurse | foreach ($_) {Remove-Item $_.FullName}
::delete's unwated files

WAIT FOR FILES TO DELETE

cd C:\test\dest
Get-ChildItem -rec -include *.mkv*,*.mp4*,*.avi*,*.m4v*,*.wmv*,*.webm*,*.flv*,*.mov* | Move-Item -destination "C:\test\videos"
::Moves Videos to "Video's folder

Get-ChildItem -rec -include *.* | Move-Item -destination "C:\test\other"
::Moves Other files to Other

I have 2 questions.

1) How do I get for files to complete moving (there could be over 100,000 files) before running the next command? (same for the delete command)

2) how do I combine this all into 1 PowerShell script?

Link to comment
https://linustechtips.com/topic/538423-automated-script/#findComment-7138662
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×