[Help]Rename string

Discussion in 'Scripting' started by Thomas Dubreuil, Dec 27, 2018.

  1. Thomas Dubreuil

    Thomas Dubreuil MDL Member

    Aug 29, 2017
    200
    302
    10
    Hi, I'm struggling with a script part of my optimization project...Basically I want to rename this:

    Code:
    SERVICE_NAME: CaptureService_373ab
    SERVICE_NAME: cbdhsvc_373ab
    SERVICE_NAME: CDPUserSvc_373ab
    SERVICE_NAME: ConsentUxUserSvc_373ab
    SERVICE_NAME: DevicePickerUserSvc_373ab
    SERVICE_NAME: DevicesFlowUserSvc_373ab
    SERVICE_NAME: PimIndexMaintenanceSvc_373ab
    SERVICE_NAME: PrintWorkflowUserSvc_373ab
    SERVICE_NAME: UnistoreSvc_373ab
    SERVICE_NAME: UserDataSvc_373ab
    SERVICE_NAME: WpnUserService_373ab
    (the numbers change randomly)
    to this:
    Code:
    SERVICE_NAME:CaptureService
    SERVICE_NAME:cbdhsvc
    SERVICE_NAME:CDPUserSvc
    SERVICE_NAME:ConsentUxUserSvc
    SERVICE_NAME:DevicePickerUserSvc
    SERVICE_NAME:DevicesFlowUserSvc
    SERVICE_NAME:PimIndexMaintenanceSvc
    SERVICE_NAME:PrintWorkflowUserSvc
    SERVICE_NAME:UnistoreSvc
    SERVICE_NAME:UserDataSvc
    SERVICE_NAME:WpnUserService
    Here's the (part of) code I came with...

    Code:
    :: GET SERVICES NAME
        sc query type= service state= all | findstr /r /C:"SERVICE_NAME:">tmpsrv.txt
        :: RENAMING
            :: SERVICES WITH SPACE IN NAME PROBLEM: REMOVING SPACE BEFORE SERVICE NAME TO REPLACE NEXT (for /f "tokens=2* delims=: ") BY (for /f "tokens=2* delims=:")
            (for /f "usebackq tokens=*" %%# in ("tmpsrv.txt") do (
                echo "%%#" | find /i "SERVICE_NAME: " 1>NUL && (
                    set "line=%%#"
                    call set "line=%%line:SERVICE_NAME: =SERVICE_NAME:%%"
                    call echo %%line%%
                ) || (
                    echo %%#
                )
            ))>"tmpsrv_1.txt"
            :: WINDOWS RANDOM _xxxxx SERVICES NAMES TRICK
            (for /f "usebackq tokens=*" %%# in ("tmpsrv_1.txt") do (
                echo "%%#" | find /i "svc_" 1>NUL && (
                    set "line=%%#"
                    call set "line=%%line:~0,-6%%"
                    call echo %%line%%
                ) || (
                    echo %%#
                )
            ))>"tmpsrv_2.txt"
            (for /f "usebackq tokens=*" %%# in ("tmpsrv_2.txt") do (
                echo "%%#" | find /i "captureservice" 1>NUL && (
                    set "line=%%#"
                    call set "line=%%line:~0,-6%%"
                    call echo %%line%%
                ) || (
                    echo %%#
                )
            ))>"tmpsrv_3.txt"
            (for /f "usebackq tokens=*" %%# in ("tmpsrv_3.txt") do (
                echo "%%#" | find /i "wpnuserservice" 1>NUL && (
                    set "line=%%#"
                    call set "line=%%line:~0,-6%%"
                    call echo %%line%%
                ) || (
                    echo %%#
                )
            ))>"tmpsrv_4.txt"
        :: MAKE RESTORE BATCH
        @echo @echo off> "%FILENAME%"
        for /f "tokens=2* delims=:" %%j in (tmpsrv_4.txt) do @( sc qc %%j |findstr START_TYPE >tmpstype.txt && for /f "tokens=4 delims=:_ " %%s in (tmpstype.txt) do @echo sc config "%%j" start= %%s >>"%FILENAME%")
    It works but it's a bit "long" to search...I couldn't find a way to do in one unique findstr
    Also in LTSC there are more services than in Windows server

    Would be something like: last tokens + delims=_ + find random numbers (the 5 digits are random)= rename line -6 characaters

    Is there someone who could save? :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Thomas Dubreuil

    Thomas Dubreuil MDL Member

    Aug 29, 2017
    200
    302
    10
    I (almost) start to understand but made my brain fried hehe
    is that right now? anyone....? (3,7 seconds now instead of 8 seconds, better anyway)
    Code:
    ...
    :: Get DATE and TIME
       for /f "tokens=1, 2, 3, 4 delims=-/. " %%j in ('Date /T') do set FILENAME=Current Services_%%j-%%k-%%l_%%m
       for /f "tokens=1, 2 delims=: " %%j in ('TIME /T') do set FILENAME=%FILENAME%%%jh%%k.bat
    :: Get SERVICES NAME
       sc query type= service state= all | findstr /r /C:"SERVICE_NAME:">tmpsrv.txt
    :: Renaming strings
       (for /f "tokens=*" %%j in (tmpsrv.txt) do (
       set "line=%%j"
       call set "line=%%line:SERVICE_NAME: =SERVICE_NAME:%%"
       call echo %%line%%
       ))>tmpsrv_1.txt
       (for /f "tokens=2* delims=:" %%j in (tmpsrv_1.txt) do (
       echo %%j | findstr /r "[0-9]" | findstr /r "_" >NUL && (
       set "line=%%j"
       call set "line=%%line:~0,-6%%"
       call echo SERVICE_NAME:%%line%%
       ) || (
       echo SERVICE_NAME:%%j
       )
       ))>tmpsrv_2.txt
    ....
       for /f "tokens=2* delims=:" %%j in (tmpsrv_2.txt) do @( sc qc "%%j" |findstr START_TYPE >tmpstype.txt && for /f "tokens=4 delims=:_ " %%s in (tmpstype.txt) do @echo sc config "%%j" start= %%s>>"%FILENAME%")
    ...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. wilenty

    wilenty MDL Member

    Jan 15, 2014
    168
    301
    10
    #3 wilenty, Jan 3, 2019
    Last edited: Jan 3, 2019
  4. Thomas Dubreuil

    Thomas Dubreuil MDL Member

    Aug 29, 2017
    200
    302
    10
    #4 Thomas Dubreuil, Jan 3, 2019
    Last edited: Jan 3, 2019
    (OP)
    It's for my own script project so it is not very helpful (if at all), I had to extract your script.

    2nd, your script can't save services with space in name (while mine does), actually misses 6 services
    This was a part of my question so you're not answering nor providing solution. And it's not "that much" faster at the end.
    good try though...and thank you anyway, I will study the part I'm interested in and adapt for my project.

    ps: And what with the password thing? it's not treasure hunt mate...
    Actually just asked simple help in SCRIPT forum, but you send an exe with no explanation with this password enigma. Anyway...
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...