I have written a PowerShell line directly in PowerShell and it works perfectly but I can't get the .bat to run it.
I think my PowerShell script running is blocked by corporate.
If I can get the same result in CMD then it will solve that issue.
Here is the PowerShell code:
CD C:\Temp
Get-ChildItem -Filter "*-Layout1*" -Recurse | Rename-Item -NewName {$_.name -replace ‘-Layout1’,'' }
This code takes PLANVIEW-Layout1.pdf and turns it into PLANVIEW.pdf but it will take any file name with -Layout1 and remove the -Layout1.
My CAD programs create .pdf's while adding "-Layout1" or "-Model1" to the file name.
I sometimes create 100's of PDF's and renaming all of them one by one is a pain.
The PowerShell code above runs perfectly if I type it in the PowerShell window.
I want other users to be able to use this as well without having to type all the code each time.
EDIT:
Sorry for the messy comments…
When I run this .bat:
@echo off
PowerShell.exe -noexit -ExecutionPolicy Bypass -File "MyScript.ps1"
Pause
with MyScript.ps1:
CD C:\Temp
Get-ChildItem -Filter "*-Layout1*" -Recurse | Rename-Item -NewName {$_.name -replace ‘-Layout1’,'' }
I now get this in the CMD:
You must provide a value expression on the right-hand side of the '-replace' operator. At C:\Temp\MyScript.ps1:3 char:85 * Get-ChildItem -Filter "-Layout1" -Recurse | Rename-Item -NewName {$_.name -replace <<<< a?~-Layout1a?T,'' } + CategoryInfo :ParserError: (:) []. ParentContainsErrorRecordException + FullyQualifiedErrorId :ExpectedValueExpression
Best Answer
Regarding the error output and the ps1 code, it seems that the " ‘ " quote around ‘-Layout1’ are not recognized well.
You may try to remplace them by simple quotes like that :