Hi,
I'm wondering if it's possible to run a PowerShell script from Command Executor for batch file renaming. My PowerShell command looks something like this:
Dir | Rename-Item –NewName {$_.name –replace "OldName","NewName"}
I realize this can be done with the folder delivery node, but if I wanted to keep the file name suffix: "OldName 089352" and only change the prefix: "NewName 089352", I don't know of any other way. I'm not really interested in a separate file renaming program.
Run Powershell Rename script in Command Executor
-
- Posts: 15
- Joined: Fri Oct 16, 2015 2:40 pm
Run Powershell Rename script in Command Executor
I love it when a plan comes together!
-- Colonel John "Hannibal" Smith
-- Colonel John "Hannibal" Smith
Re: Run Powershell Rename script in Command Executor
That is essentially what you do when going your powershell approach, no matter how you twist and turn it. My guess is that you actually just want a portable way to do your stuff that is e.g. saved within the workflow and will work when exporting/importing the workflow on another system without copying additional files "manually". If that is the goal, you could virtually do anything you want, you'd just need to find a way how to include (or fetch externally) the stuff with dependencies that you are going to use in the workflow.
Straight forward answer on "how to utilize powershell in a workflow":
There multiple ways to execute PS stuff, you can for example just use a command executor and do
cmd /C powershell -C "your powershell stuff"
But that will not be compatible to pipe symbol | ...at least i didnt yet find a quick way. Besides of course there are many other limitations and it is kind of hard to use.
Another example i often use is to write scripts to disk using the write text processor, save it in %s_job_work% directory and for powershell name it renamescript.ps1. When writing the script to disk using text file proc, you can nicely insert ffastrans variables into the script wherever you like, like this:
On Commandline:
Code: Select all
Dir "\\localhost\c$\temp" | Rename-Item –NewName {$_.name –replace "OldName","NewName"} -ErrorAction 'Ignore'
Code: Select all
Dir "%s_original_path%" | Rename-Item –NewName {$_.name –replace "OldName","NewName"} -ErrorAction 'Ignore'
cmd /C powershell -executionpolicy unrestricted -file "%s_job_work%\renamescript.ps1"
emcodem, wrapping since 2009 you got the rhyme?
-
- Posts: 15
- Joined: Fri Oct 16, 2015 2:40 pm
Re: Run Powershell Rename script in Command Executor
Thanks for your response! I made a workflow from what I think you are suggesting but haven't been able to get it to work. (attached)
- Attachments
-
- BATCH FILE RENAME.json
- (2.68 KiB) Downloaded 151 times
Re: Run Powershell Rename script in Command Executor
I think it works as it is if you set the "Encoding" bottom right in the write text processor to "ansi" OR "utf8 BOM". currently it is set to utf8 (without byte order marker BOM), so powershell has no chance to guess the correct encoding of the script and it assumes ansi but actually it is utf8 more or less.
emcodem, wrapping since 2009 you got the rhyme?
-
- Posts: 15
- Joined: Fri Oct 16, 2015 2:40 pm
Re: Run Powershell Rename script in Command Executor
I switched it to ansi and it worked like a charm. Thank you so much! So, as I understand it, the Command executor script is telling it to run the PowerShell script in the text file processor.
I love it when a plan comes together!
-- Colonel John "Hannibal" Smith
-- Colonel John "Hannibal" Smith
Re: Run Powershell Rename script in Command Executor
It's very simple: Write text file writes a script to disk and command line processor executes a commandline to execute the powershell script file.jukeboxjim wrote: ↑Wed Nov 02, 2022 11:12 am the Command executor script is telling it to run the PowerShell script in the text file processor.
You could reproduce it manually by writing the script from notepad, save the file and execute the script via command line
emcodem, wrapping since 2009 you got the rhyme?
-
- Posts: 15
- Joined: Fri Oct 16, 2015 2:40 pm
Re: Run Powershell Rename script in Command Executor
Cool beans
I love it when a plan comes together!
-- Colonel John "Hannibal" Smith
-- Colonel John "Hannibal" Smith