Help can JREPL do this?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
JacobMIX
Posts: 5
Joined: 04 Sep 2018 23:04

Help can JREPL do this?

#1 Post by JacobMIX » 04 Sep 2018 23:17

I wanna use a bat file to delete "[", "]", and anything between them.
Plus replace any "." with spaces, and remove everything after the last "."
Then also remove any blank space " " that might be at the start or the end.
Can't seem to find an easy way to do it. Then i stumbled upon JREPL.BAT, but i have no idea were to start.
Can JREPL even do what i want? If anyone can straight up code it, and post it they get double points.

I wanna use it in a setting like this btw:

Code: Select all

:Loop
For /F "Tokens=1* Delims=:" %%A In ( 'TaskList /V /Fi "ImageName Eq PotPlayerMini64.exe" /Fo List' ) Do @For /F "Tokens=*" %%C In ("%%B") Do @Set "_PotPlayerWindowTitle=%%C"
echo %_PotPlayerWindowTitle% >PotPlayerTitle.txt

"Code here that edits PotPlayerTitle.txt and puts it in PotPlayerEditedTitle.txt"

set /p PotPlayerEditedTitle=<PotPlayerEditedTitle.txt
goto Loop
Last edited by JacobMIX on 05 Sep 2018 17:41, edited 3 times in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help can JREPL do this?

#2 Post by dbenham » 05 Sep 2018 04:16

Untested, as I am not at a Windows machine, but I believe the following should work:

Code: Select all

call jrepl "^[. ]+|[. ]+$|\[[^\]]*\]#\." "# " /t # /f PotPlayerTitle.txt /o PotPlayerTitle.txt
The above uses the /T option to do multiple find/replace in one pass, using # as the delimiter between expressions.

^[. ]+|[. ]+$|\[[^\]]*\] matches:
- any combination of dots and spaces at the beginning of the line
- any combination of dots and spaces at the end of the line
- any string beginning with [ and ending with ]
And the matching string is replaced by nothing (there is no string before the # delimiter in the replace argument)

\. matches a dot which is replaced by a space


Or you can overwrite the original file with

Code: Select all

call jrepl "^[. ]+|[. ]+$|\[[^\]]*\]#\." "# " /t # /f PotPlayerTitle.txt /o -
Or you can eliminate the FOR /F or need for FIND, and write the final result directly to PotPlayerTitle.txt with:

Code: Select all

tasklist /fi "imagename eq PotPlayerMini64.exe" /fo list /v | jrepl "^[. ]+|[. ]+$|\[[^\]]*\]#\." "# " /t # /inc "/window title/i" /exc "/N\/A/" /a /o PotPlayerTitle.txt
This last code assumes JREPL will always make at least one change to the "window title" line, else it won't work. The /A option only writes lines that are altered.


Dave Benham

JacobMIX
Posts: 5
Joined: 04 Sep 2018 23:04

Re: Help can JREPL do this?

#3 Post by JacobMIX » 05 Sep 2018 05:25

Thanks. Your script works almost perfectly. But some stuff at the end isn't gone. (The stuff after the last "." i mean)
Does JREPL have a way to detect the last ".", and delete it with everything ahead of it?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help can JREPL do this?

#4 Post by dbenham » 05 Sep 2018 06:30

Oops. Yes, I forgot about that requirement.

Yes, you can use the negative look ahead feature to find the last dot. With this requirement it is easier to break the operation into 2 JREPL steps, saving the removal of leading and trailing spaces until the second step.

Code: Select all

tasklist /fi "imagename eq PotPlayerMini64.exe" /fo list /v | jrepl "\[[^\]]*\]|\.(?!.*\.).*#\." "# " /t # /inc "/window title/i" /exc /N\/A/ /a|jrepl "^ +| +$" ""

Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help can JREPL do this?

#5 Post by dbenham » 05 Sep 2018 06:42

I should think you don't need (want) to preserve the "Window Title:" text at the beginning, as this is a constant. It is easy to remove it, and then you are guaranteed that all relevant lines have at least one change, so the /A option is safe to use.

I also refined the /INC and /EXC options to be more specific.

Code: Select all

tasklist /fi "imagename eq PotPlayerMini64.exe" /fo list /v | jrepl "^Window Title:|\[[^\]]*\]|\.(?!.*\.).*#\." "# " /t # /inc "/^Window Title:/" /exc "/^Window Title: N\/A/" /a|jrepl "^ +| +$" ""

Dave Benham

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Help can JREPL do this?

#6 Post by Squashman » 05 Sep 2018 09:19

In the future may I suggest you provide a verifiable example of what the input of your example is and what you want to the output to look like. A wordy description can sometimes gets confusing without seeing a real world example of your data.

JacobMIX
Posts: 5
Joined: 04 Sep 2018 23:04

Re: Help can JREPL do this?

#7 Post by JacobMIX » 05 Sep 2018 13:14

It seems to work the first time it runs it, but next loop it doesn't. I get this error: JScript runtime error in Search regular expression: Syntax error in regular expression
I want a cleaned up version of what file the PotPlayer is playing. Something like:
[TextHere].CoolVidoe.-.Hip.video.-.#01.(Pilot)_(Year)_[BD 1920x1080p AVC FLAC]_[More things].mkv - PotPlayer
Which i would like to output as:
CoolVidoe - Hip video - #01 (Pilot) (Year)

Full script i'm using right now:

Code: Select all

@Echo Off
:CheckEasyRP
tasklist /FI "IMAGENAME eq easyrp.exe" 2>NUL | find /I /N "easyrp.exe">NUL
if "%ERRORLEVEL%"=="0" goto LoopStart
goto EasyRPNotRunning


:EasyRPNotRunning
start /d "C:\srcds\Bots\Discord\EasyRP-windows" easyrp.exe
timeout /t 5
goto CheckEasyRP


:LoopStart
timeout /t 2
tasklist /FI "IMAGENAME eq easyrp.exe" 2>NUL | find /I /N "easyrp.exe">NUL
if "%ERRORLEVEL%"=="0" goto EasyRPGo
goto EasyRPNotGo

:EasyRPGo
timeout /t 1
goto PotPlayer

:EasyRPNotGo
exit


:PotPlayer
timeout /t 2
tasklist /FI "IMAGENAME eq PotPlayerMini64.exe" 2>NUL | find /I /N "PotPlayerMini64.exe">NUL
if "%ERRORLEVEL%"=="0" goto PotPlayerRunning
goto PotPlayerNotRunning

:PotPlayerRunning
timeout /t 1
goto StartScript

:PotPlayerNotRunning
taskkill /im "easyrp.exe"
exit


:StartScript
For /F "Tokens=1* Delims=:" %%A In ( 'TaskList /V /Fi "ImageName Eq PotPlayerMini64.exe" /Fo List' ) Do @For /F "Tokens=*" %%C In ("%%B") Do @Set "_PotPlayerWindowTitle=%%C"
echo %_PotPlayerWindowTitle% >PotPlayerTitle.txt


call jrepl "\[[^\]]*\]|\.(?!.*\.).*#\." "# " /t # /f PotPlayerTitle.txt /o PotPlayerEditedTitle.txt
timeout /t 1

set /p PotPlayerTitle=<PotPlayerEditedTitle.txt
timeout /t 1

SetLocal EnableDelayedExpansion
:: Edit the following three lines as needed.
:: Specifiy the full path to the file, or the current directory will be used
Set _PathtoFile=C:\srcds\Bots\Discord\EasyRP-windows\config.ini
Set _OldLine=State=
Set _NewLine=State=%PotPlayerTitle%
:: End of Search parameters
Call :_Parse "%_PathtoFile%"
Set _Len=0
Set _Str=%_OldLine%
Set _Str=%_Str:"=.%987654321
:_Loop
If NOT "%_Str:~18%"=="" Set _Str=%_Str:~9%& Set /A _Len+=9& Goto _Loop
Set _Num=%_Str:~9,1%
Set /A _Len=_Len+_Num
PushD %_FilePath%
If Exist %_FileName%.new Del %_FileName%.new
If Exist %_FileName%.old Del %_FileName%.old
Set _LineNo=0
For /F "Tokens=* Eol=" %%I In (%_FileName%%_FileExt%) Do (
Set _tmp=%%I
Set /A _LineNo+=1
If /I "!_tmp:~0,%_Len%!"=="%_OldLine%" (
>>%_FileName%.new Echo %_NewLine%
) Else (
If !_LineNo! GTR 1 If "!_tmp:~0,1!"=="[" Echo.>>%_FileName%.new
SetLocal DisableDelayedExpansion
>>%_FileName%.new Echo %%I
EndLocal
))
Ren %_FileName%%_FileExt% %_FileName%.old
Ren %_FileName%.new %_FileName%.ini
PopD
Goto :LoopStart
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutines
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Parse
Set _FilePath=%~dp1
Set _FileName=%~n1
Set _FileExt=%~x1
exit /b
Edit never mind. Got it working using your latest script with ">PotPlayerTitle.txt" or "/o PotPlayerTitle.txt" Didn't wanna work if i tried with the whole ( ' HERE ' ) Do @For /F "Tokens=*" %%C In ("%%B") Do @Set "_PotPlayerWindowTitle=%%C" Thing.

Code: Select all

tasklist /fi "imagename eq PotPlayerMini64.exe" /fo list /v | jrepl "^Window Title:|\[[^\]]*\]|\.(?!.*\.).*#\.|\_" "# " /t # /inc "/^Window Title:/" /exc "/^Window Title: N\/A/" /a|jrepl "^ +| +$" "">PotPlayerTitle.txt
Last edited by JacobMIX on 12 Oct 2018 07:56, edited 2 times in total.

JacobMIX
Posts: 5
Joined: 04 Sep 2018 23:04

Re: Help can JREPL do this?

#8 Post by JacobMIX » 05 Sep 2018 17:42

Damn it nope. Seems it still fails after the loop. I get this error now: http://prntscr.com/kr3gu3

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Help can JREPL do this?

#9 Post by Squashman » 05 Sep 2018 18:14

JacobMIX wrote:
05 Sep 2018 17:42
Damn it nope. Seems it still fails after the loop. I get this error now: http://prntscr.com/kr3gu3
IMHO, it would be easier to copy and paste the text from the console window then creating a screenshot and creating a link for it. Also you should be able to do everything you need to do without JREPL. A FOR /F and some string substitution should easily do the trick.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Help can JREPL do this?

#10 Post by dbenham » 05 Sep 2018 18:19

The error is due to a ! within the regex search string and delayedExpansion.

You start out with delayed expansion off, so JREPL works the first time. But after you run JREPL, you SETLOCAL EnableDelayedExpansion, but fail to ENDLOCAL before you loop back. (Actually you have 2 SETLOCAL, but only 1 ENDLOCAL). So now when you loop back the delayed expansion corrupts the regex with !. Even if you didn't get this error, you would eventually get a stack overflow error because you can only have 31(?) active SETLOCAL within one CALL level.

Your code is easily fixed by adding an ENDLOCAL before GOTO :LoopStart


Dave Benham

JacobMIX
Posts: 5
Joined: 04 Sep 2018 23:04

Re: Help can JREPL do this?

#11 Post by JacobMIX » 05 Sep 2018 18:54

You're totally right. Got it fixed now, and it's working perfectly. Thanks for the great help.

Post Reply