![]() ![]() The value of NewName is a script block that runs before the value is submitted to the NewName parameter. To recap: The Get-ChildItem command gets all the items (we filter only the files using the *.* wildcard) in the current folder then pipes (sends) them to Rename-Item command. We’ve simply changed the substitute value to underscore. This is the part of the code that you can change to suit your needs.įor instance if you want to replace all the spaces with underscores, you’d change this part of the code to ‘ ‘,’_’. The replace operator accepts two values, what it needs to find, and what you want to replace it with. We use it in our script block to find the spaces in our filenames and replace them with hyphens. The -replace is the comparison operator that finds a specific pattern and replaces it with a specified value. What it does here is tell the rename command that we only want to change the names of our files. ![]() Name is a property of the Get-ChildItem command. So we are referencing whatever output we got from our Get-ChildItem command, which is a list of all the files in the parent folder and subfolders. The $_ is a PowerShell shorthand of saying everything before the pipe. The first step is to copy this line of code: Get-ChildItem -Recurse *.* | Rename-Item -NewName curry brackets mark the beginning and end of the script block. So, today I’m going to share with you how I replace spaces with hyphens in filenames using a PowerShell script. Now, Windows does not have an easy way to batch rename your files or folders.įor as long as I can remember I have used the Command Prompt (CMD) to replace spaces with hyphens in Windows filenames.īut for the last couple of years, I have been using PowerShell. Replacing spaces with hyphens also has the advantage of creating more readable urls! The spaces rule comes from having weird bugs when we upload transcripts – especially if the filename ends with a space. Our file naming scheme consists of a couple of rules: never us spaces in file names and use the YYYY-MM-DD date format. And if you work with a lot of files, it’s important to have a file naming scheme. In transcription, we handle a lot of audio, video, and text files. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |