Do you have hundreds of thousands of files stored in even more drives and lost the overview already looong time ago? Right! #Me too...whatever. Time for spring cleaning! Let's figure out how to get a plan.
HINT: Find the finished script at the very bottom!
- You need to install "Visual Studio Code" from here (it's 4 free)
- By installing it will ask you if you want to add it to THE PATH. That's okay. It simply means you can access this tool via the standard console of your computer.
Wiki: "PATH [...] specifying a set of directories where executable programs are located"
- directly after creating a new file we save it as "PS1" in the root folder we want to search through:
- Install the PowerShell extension:
- Restart as Administrator
- Let the fun begin!
Let's start coding. Or..wait! We don't have a plan what we want to achieve. In programming 50% of your work is to have a good understanding where to go before we even do a step.
- get the location of the script (later you put it in any folder you like to browse through)
- look for all the files (children) of the chosen directory
- get all files of a specific filetype (e.g. .mp3)
- create a .txt file
- list all files in a .txt file(name of the file & path were to find them)
- empty the list each time you run the script (otherwise it will just continue listing everything at the bottom)
Aaaand...whats that? Here is a quick guide to Visual Studio Code (= VSC). It's a really slim editor so let's make it quick.
Basically all you need is the editor window where you will write your code (where the cursor is). The Debug window will be very helpful because there we will see all the files, directories etc.
Okidoki..now the fun part 🙂
- We store the path we want to search through in a local variable "TargetPath"
- "Test-Path" is a command that simply checks if the path (our "TargetPath") exists on the running system or not.
- If our path return true (means that it exists) it will print the path in the console.
- No magic - but let's check if it works!
- Open the Debug-Mode
- Open the Console and hit "run":
- Ok. Now we are at C:\. Before we continue if it also works with a different path:
- Hooray! Now create some test mp3 files (you can simply rename a .txt file to whatever.mp3) in the path. I'll stick to the Desktop here. Let''s collect some childs with:
$childs = Get-ChildItem $TargetPath -Recurse -Filter *.mp3 Write-Output "$($childs)"
The local variable "$childs" = [gets child items] in [$our chosen path] and [- in any subfolder] [- filtered to files that have an ".mp3" ending]
- Alright! Now we create a new .txt-file where we will store all the data later.
New-Item $TargetPath\MyMissingFiles_$((Get-Date).ToString('yyyy-MM-dd')).txt -ItemType file
We create a [new item] in the [path of our choice]\[called "MyMissingFiles_currentDate"].txt as a [regular file].
The date may seem a bit complex but once you got it you will never forget it 🙂
$ = local variable
(Get-Date) = the current date or a date that you need to specify will return as an object
.ToString = we want to have the date-object as a string
('yyyy-MM-dd') = specify the format as the date will we dispayed
You can find more infos and other date-formats here.
- YESSS! The console shows the result we expected:
- Great! We have an empty file...wonderful. Not feed it with some files. Those three files we want to see in out .txt file soon:
You will habe this view if you put on a breakpoint and run the debug mode.
- I stored the .txt we created in a local variable "$Output" (so we don't need to re-write everything again) and saved all items in it:
Well uhm yeah...thats a pipe |
It catches the output of the command and pass it to another command. Means we grab the fullname of a child and hand it over to the "Add-Content" command. It's a lightweight writing for an 'and'. Grab it AND write it.
- All the files are well-listed in out .txt file we created:
- Time for improvement!
To make it quicker to use the script it makes sense to store the [file format] we are searching for in a local variable at the top.
Oh boi! Here you can copy/pasta the script.