Breaking PS1 line within BAT file [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Breaking PS1 line within BAT file [SOLVED]

#1 Post by DOSadnie » 06 Jul 2023 06:13

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?
Last edited by DOSadnie on 15 Jul 2023 04:08, edited 2 times in total.

OJBakker
Expert
Posts: 90
Joined: 12 Aug 2011 13:57

Re: Breaking PS1 line within BAT file

#2 Post by OJBakker » 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):

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"

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Breaking PS1 line within BAT file

#3 Post by Aacini » 06 Jul 2023 15:09

You can review extensive examples of this method at this thread.

Antonio

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Breaking PS1 line within BAT file

#4 Post by DOSadnie » 11 Jul 2023 06:05

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Breaking PS1 line within BAT file

#5 Post by Aacini » 11 Jul 2023 07:49

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...

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Breaking PS1 line within BAT file

#6 Post by DOSadnie » 13 Jul 2023 05:53

The above works, so thank you very much


And the lines

Code: Select all

Set-Location -Path 'C:\'; ^
and

Code: Select all

%EndPowershell%
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

Post Reply