delay-bind

Automatically batch renaming files according to a formula including parent folder name using Powershell

喜你入骨 提交于 2019-12-11 05:34:38
问题 How can I batch rename files in powershell using the following code: $nr=1;Get-ChildItem -Filter *.jpg | Rename-Item -Newname {"PPPPPPP_{0:d3}.jpg" -f $global:nr++} where PPPPPPP is the name of parent folder containing these files. Expected Output : PPPPPPP_001.jpg PPPPPPP_002.jpg PPPPPPP_003.jpg Files are located in C:\USER\MAIN\BLABLABLA\PPPPPPP folder. 回答1: Get the parent directory's name via $_.Directory.Name inside the script block. Cast the $nr variable to [ref] so that you can modify

For PowerShell cmdlets, can I always pass a script block to a string parameter?

为君一笑 提交于 2019-11-27 05:32:33
I'm looking at the document of Rename-Item and there is an example like this. PS C:\>Get-ChildItem *.txt | Rename-Item -NewName { $_.name -Replace '\.txt','.log' } This example shows how to use the Replace operator to rename multiple files, even though the NewName parameter does not accept wildcard characters. This command renames all of the .txt files in the current directory to .log. The command uses the Get-ChildItem cmdlet to get all of the files in the current folder that have a .txt file name extension. Then, it uses the pipeline operator (|) to send those files to Rename-Item . The

For PowerShell cmdlets, can I always pass a script block to a string parameter?

情到浓时终转凉″ 提交于 2019-11-26 11:37:05
问题 I\'m looking at the document of Rename-Item and there is an example like this. PS C:\\>Get-ChildItem *.txt | Rename-Item -NewName { $_.name -Replace \'\\.txt\',\'.log\' } This example shows how to use the Replace operator to rename multiple files, even though the NewName parameter does not accept wildcard characters. This command renames all of the .txt files in the current directory to .log. The command uses the Get-ChildItem cmdlet to get all of the files in the current folder that have a