admin管理员组文章数量:1362778
I need to save in a .bat file a powershell command and execute the .bat so I get the same result than what I would get from doing it manually.
For example I have a folder in c: called "my mp3"
so I have some files:
"c:\my mp3\13 # _Labyrinth__D Ma.mp3"
"c:\my mp3\14 # _Labyrinth__D Ma.mp3"
"c:\my mp3\15 # _Labyrinth__D Ma.mp3"
"c:\my mp3\16 # _Labyrinth__D Ma.mp3"
And so on. I have this powershell command that I have saved in a .ps1 file called b1.ps1:
get-childitem *.mp3*,*.wav*,*.m4v*,*.mp4*,*.mov*,*.jpg*,*.mkv*,*.mpg*,*.mpeg*,*.flv*,*.vob*,*.m4v*,*.ts*,*.tga*,*.bmp* | foreach { rename-item $_ $_.Name.Replace("!","").replace('#','').replace("'","").replace('ù','').replace('_','').replace('{','').replace('}','').replace('&','').replace('?','').replace(',','').replace(';','').replace(':','').replace('$','').replace('^','').replace('\','').replace('"','').replace('@','').replace('*','').replace('(','').replace(')','').replace('-','').replace('=','').replace('+','').replace('/','').replace('>','').replace('ę','').replace('ą','').replace('ś','').replace('ż','').replace('ź','').replace('ć','').replace('ń','').replace('ł','').replace('ó','').replace('è','')}
Now I have to execute the script, possibly using a simple .bat
that I save in "c:\my mp3"
or another folder, so that in the folder working folder it "apply" the script (that remove some special characters in the filenames).
How can I do? thanks
I need to save in a .bat file a powershell command and execute the .bat so I get the same result than what I would get from doing it manually.
For example I have a folder in c: called "my mp3"
so I have some files:
"c:\my mp3\13 # _Labyrinth__D Ma.mp3"
"c:\my mp3\14 # _Labyrinth__D Ma.mp3"
"c:\my mp3\15 # _Labyrinth__D Ma.mp3"
"c:\my mp3\16 # _Labyrinth__D Ma.mp3"
And so on. I have this powershell command that I have saved in a .ps1 file called b1.ps1:
get-childitem *.mp3*,*.wav*,*.m4v*,*.mp4*,*.mov*,*.jpg*,*.mkv*,*.mpg*,*.mpeg*,*.flv*,*.vob*,*.m4v*,*.ts*,*.tga*,*.bmp* | foreach { rename-item $_ $_.Name.Replace("!","").replace('#','').replace("'","").replace('ù','').replace('_','').replace('{','').replace('}','').replace('&','').replace('?','').replace(',','').replace(';','').replace(':','').replace('$','').replace('^','').replace('\','').replace('"','').replace('@','').replace('*','').replace('(','').replace(')','').replace('-','').replace('=','').replace('+','').replace('/','').replace('>','').replace('ę','').replace('ą','').replace('ś','').replace('ż','').replace('ź','').replace('ć','').replace('ń','').replace('ł','').replace('ó','').replace('è','')}
Now I have to execute the script, possibly using a simple .bat
that I save in "c:\my mp3"
or another folder, so that in the folder working folder it "apply" the script (that remove some special characters in the filenames).
How can I do? thanks
Share Improve this question edited 2 days ago Compo 38.8k5 gold badges31 silver badges45 bronze badges asked 2 days ago marcoroccomarcorocco 11 bronze badge New contributor marcorocco is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 8 | Show 3 more comments1 Answer
Reset to default 2You can obviously run the PowerShell file directly from your batch file using its -File
option.
@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File "%~dp0b1.ps1"
Obviously you can change the -ExecutionPolicy
to whatever suits your environment.
However there's no need to use the .ps1
file, as you can do that directly as a PowerShell -Command
from the batch file.
@%SystemRoot%\System32\chcp 65001 1>NUL
@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "Get-ChildItem *.mp3*,*.wav*,*.m4v*,*.mp4*,*.mov*,*.jpg*,*.mkv*,*.mpg*,*.mpeg*,*.flv*,*.vob*,*.m4v*,*.ts*,*.tga*,*.bmp* | ForEach { Rename-Item $_ $_.Name.Replace(\"!\",\"\").Replace('#','').Replace(\"'\",\"\").Replace('ù','').Replace('_','').Replace('{','').Replace('}','').Replace('&','').Replace(',','').Replace(';','').Replace('$','').Replace('@','').Replace('(','').Replace(')','').Replace('-','').Replace('=','').Replace('+','').Replace('ę','').Replace('ą','').Replace('ś','').Replace('ż','').Replace('ź','').Replace('ć','').Replace('ń','').Replace('ł','').Replace('ó','').Replace('è','')}"
Please ensure, as you are using recognizable unicode characters in your replacements, and changing the codepage accordingly, that you save the written batch file using an editor which also saves it as UTF8-NoBOM.
本文标签: windowsrun powershell ps1 to be embedded in a cmd or bat fileStack Overflow
版权声明:本文标题:windows - run powershell .ps1 to be embedded in a cmd or .bat file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743819056a2544454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
%~dp0
will not be correct. Perhaps you should explain to us where every file you are using resides! – Compo Commented 2 days ago( $_ -replace '[!#ù_{}&?,;":$^\@*()-=+>ęąśżźćńłóè]', '' )
There are a couple characters that are awkward to include the in the expression ( square brackets[]
and double quote"
)... not impossible, but awkward enough to maybe do them separate. But the rest of this will also perform way better, since.Replace()
allocates a whole new string for each call. – Joel Coehoorn Commented 2 days ago