Page 1 of 1
Breaking PS1 line within BAT file [SOLVED]
Posted: 06 Jul 2023 06:13
by DOSadnie
I had this
PS1 script
Code: Select all
$directories = @(
"C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker\logs"
)
foreach ($directory in $directories) {
Set-Location -Path $directory
Get-ChildItem -Filter "*.log~" | Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Archive } | Remove-Item
}
Set-Location -Path "C:\"
Exit
which I have inserted to a
BAT file in form of one long line
Code: Select all
powershell.exe -Command "$directories = @('C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker\logs'); foreach ($directory in $directories) { Set-Location -Path $directory; Get-ChildItem -Filter '*.log~' | Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Archive } | Remove-Item }; Set-Location -Path 'C:\'; Exit"
And they both work A-OK. But is there a way to brake than oneliner within
BAT file so that it will become easier to read for my hypothetical future re-workings and adaptations of it?
Re: Breaking PS1 line within BAT file
Posted: 06 Jul 2023 07:23
by OJBakker
Use the standard line-continuation of cmd using a caret at the end of the line.
The following will probably work (untested):
Code: Select all
powershell.exe -Command "$directories = @('C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker\logs');^
foreach ($directory in $directories) { Set-Location -Path $directory;^
Get-ChildItem -Filter '*.log~' | Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Archive } | Remove-Item };^
Set-Location -Path 'C:\';^
Exit"
Re: Breaking PS1 line within BAT file
Posted: 06 Jul 2023 15:09
by Aacini
You can review extensive examples of this method at
this thread.
Antonio
Re: Breaking PS1 line within BAT file
Posted: 11 Jul 2023 06:05
by DOSadnie
OJBakker wrote: ↑06 Jul 2023 07:23
Use the standard line-continuation of cmd using a caret at the end of the line.
The following will probably work (untested):
[...]
It did not
I tried adjusting it myself and also failed
Aacini wrote: ↑06 Jul 2023 15:09
You can review extensive examples of this method at
this thread.
Then I feed ChatGPT the code from this
viewtopic.php?t=6936#p50965 as a book of rules - but it also failed by producing
Code: Select all
powershell.exe -Command ^
"$directories = @('C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker\logs'); ^
foreach ($directory in $directories) { ^
Set-Location -Path $directory; ^
Get-ChildItem -Filter '*.log~' | Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Archive } | Remove-Item; ^
}; ^
Set-Location -Path 'C:\'; ^
Exit"
and many other versions
Re: Breaking PS1 line within BAT file
Posted: 11 Jul 2023 07:49
by Aacini
Your "book of rules" failed in several points:
- My code have not "-Command" part and does not enclose the command in quotes. This is simpler and aids to avoid subtle errors.
- Your code left a blank line. If you want to insert a blank line, you have to put a caret at end (the same as all lines).
- You forget that this is still Batch file code, so you have to "caret-escape" all special characters: | < > &
This should work:
Code: Select all
powershell.exe ^
$directories = @('C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker\logs'); ^
foreach ($directory in $directories) { ^
Set-Location -Path $directory; ^
Get-ChildItem -Filter '*.log~' ^| Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Archive } ^| Remove-Item ^
}; ^
Set-Location -Path 'C:\'; ^
Exit
%EndPowershell%
Antonio
PS - Try to just remove the blank line from your code. If this don't works, you still have to escape the pipeline...
Re: Breaking PS1 line within BAT file
Posted: 13 Jul 2023 05:53
by DOSadnie
The above works, so thank you very much
And the lines
and
can be removed - and so the final code can look like this
Code: Select all
powershell.exe ^
$directories = @( ^
'C:\Users\YOUR-USER-NAME\AppData\Roaming\ASCOMP Software\BackUp Maker\logs' ^
); ^
foreach ($directory in $directories) { ^
Set-Location -Path $directory; ^
Get-ChildItem -Filter '*.log~' ^| Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Archive } ^| Remove-Item ^
}; ^
Exit