Advertisement
T3RRYT3RR0R

Multipurpose Batch Substring String Modification Function

Feb 3rd, 2020
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 9.73 KB | None | 0 0
  1. @Echo Off & Mode 1000
  2.  
  3. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Mod-string Function By T3RRY
  4. :::
  5. ::: Multi-Purpose String Modification Function with various options to suit a range of needs.
  6. :::
  7. ::: For use On Windows 10 in Scripts that need to process Large volumes of Strings and Find, Append, Remove, Or Modify Known Substrings.
  8. :::
  9. :::  Note: This Function Is for String / SubString Modification in defined variables.
  10. :::
  11. ::: Ensure Substring targeted for Modification is Unique in the String.
  12. ::: Case Sensitive to limit False positives.
  13. ::: Doublequote Strings containing Spaces or Special Characters
  14. :::
  15. ::: REM All Switches return a string length result in the variable StringLen
  16. :::
  17. ::: REM if the folder containing modstring is in your system PATH variable, type modstring in cmd.exe to see the Syntax.
  18.  
  19.  
  20. :ModString REM Usage - ModString <stringVar> <SubString> </Switch> </XS,/IB,/IA only: "New Substring">
  21.  
  22. ::::: SWITCHES:
  23. ::: - Function must be called with ONLY ONE of the following Switches to define the Modification Type.
  24. :::
  25. ::: /AR removes everything AFTER  the substring, REMOVES the Substring.
  26. ::: /AK removes everything AFTER  the substring,   KEEPS the Substring.
  27. ::: /BR removes everything BEFORE the substring, REMOVES the Substring.
  28. ::: /BK removes everything BEFORE the substring,   KEEPS the Substring.
  29. ::: /RS REMOVES the SUBSTRING from the string.
  30. ::: /XS EXCHANGES the SUBSTRING with new substring Defined in Parameter 4.
  31. ::: /IB INSERTS new substring Defined in Parameter 4 BEFORE the substring.
  32. ::: /IA INSERTS new substring Defined in Parameter 4  AFTER the substring.
  33. ::: /DS adds a DATE time SUFFIX to the STRING
  34. ::: /DP adds a DATE time PREFIX to the STRING
  35. ::: /FS FIND SUBSTRING in string. Does Not Modify the String. True or False value stored in the Variable: Str_Find
  36.  
  37.     Setlocal EnableDelayedExpansion
  38.  
  39. ::: Test Parameters 1 2 and 3 for definition. Displays Syntax if Parameters are not Defined.
  40.  
  41.     IF "%~1"=="" CALL :MS_Syntax 1
  42.     IF "!%~1!"=="" CALL :MS_Syntax 1b
  43.     IF "%~2"=="" CALL :MS_Syntax 2
  44.     IF "%~3"=="" CALL :MS_syntax 3 Missing
  45.  
  46.     IF Defined ParamError (
  47.         ECHO  & REM The preceeding Character is the BEL character, Not the ANSI ESC character used for Color Codes.
  48.         ECHO(Exit
  49.         pause >nul
  50.         Cls
  51.         Exit /B 1
  52.     )
  53.  
  54. ::: Store the Value of the String from the Source Variable into tmpStr Variable
  55.  
  56.     Set "tmpStr=!%~1!"
  57.  
  58. ::: Modify the Switch for Conditional Validation.
  59.  
  60.     Set "Switch=%~3"
  61.     Set "Switch=!Switch:/=!"
  62.     Set "Switch=!Switch:\=!"
  63.  
  64. ::: Test definition of Fourth Parameter for Relevent Switches.
  65.  
  66.     FOR %%F IN (XS,IA,IB) Do (
  67.         If /I "%%F"=="!Switch!" (
  68.             IF "%~4"=="" (
  69.                 ECHO  & REM The preceeding Character is the BEL character, Not the ANSI ESC character used for Color Codes.
  70.                 CALL :MS_Syntax 4
  71.                 ECHO(Exit
  72.                 pause >nul
  73.                 Cls
  74.                 Exit /B 1
  75.             )
  76.         )
  77.     )
  78.  
  79. ::: Get Length of Substring and String for Use in Substring Modification Functions
  80.  
  81.     Call :StrLen Len "%~2"
  82.     CALL :StrLen FullLen "!%~1!"
  83.     IF Errorlevel 1 (
  84.         ECHO  & REM The preceeding Character is the BEL character, Not the ANSI ESC character used for Color Codes.
  85.         CALL :MS_Syntax 5 "Variable content exceeds maximum Length"
  86.         ECHO(Exit
  87.         pause >nul
  88.         Exit /B 1
  89.     )
  90.  
  91. ::: Test Value of Parameter 3 /Switch for Valid Switch, Progress to relevent Function if True, Display Syntax Help if Not.
  92.  
  93.     FOR %%S IN (AR,AK,BR,BK,RS,XS,IA,IB,FS,DS,DP) Do (
  94.         IF /I "!Switch!"=="%%S" (GOTO :MS_Switch_%%S)
  95.     )
  96.    
  97.     ECHO  & REM The preceeding Character is the BEL character, Not the ANSI ESC character used for Color Codes.
  98.     CALL :MS_Syntax 3 "Invalid Switch"
  99.     ECHO(Exit
  100.     pause >nul
  101.     Exit /B 1
  102.  
  103. :StrLen REM Usage: <ResultVar> <StringVar>
  104.  
  105.     Setlocal EnableDelayedExpansion
  106.  
  107.     Set "tmpLen=%~2"
  108.  
  109.     For /L %%L in (1,1,500) Do (
  110.         Set "LenTrim=!tmpLen:~0,-%%L!"
  111.         If "!LenTrim!"=="" (
  112.             Endlocal & Set "%~1=%%L"
  113.             Exit /b 0
  114.         )
  115.     )
  116.  
  117. REM Limited to 500 Characters to Improve Function Speed.
  118.     ECHO( Substring Exceeds Maximum Length (500 Characters)
  119.     Endlocal
  120.     Exit /B 1
  121.  
  122. :MS_Switch_DP
  123. :MS_Switch_DS
  124.  
  125.     Set "appendDT=%DATE%"
  126.     Set "appendDT=%appendDT: =%"
  127.     Set "appendDT=%appendDT%%TIME%"
  128.     Set "appendDT=%appendDT:/=%"
  129.     Set "appendDT=%appendDT::=%"
  130.     Set "appendDT=%appendDT:.=%"
  131.     Set "appendDT=%appendDT: =0%"
  132.     Set "appendDT=%appendDT:~3,12%"
  133.     IF "%switch%"=="DP" (Set "tmpStr=%appendDT%%~2") Else (Set "tmpStr=%~2%appendDT%")
  134.     GOTO :MS_Set
  135.  
  136. :MS_Switch_RS
  137.  
  138.     Set "tmpStr=!tmpStr:%~2=!"
  139.     GOTO :MS_Set
  140.  
  141. :MS_Switch_XS
  142.  
  143.     Set "tmpStr=!tmpStr:%~2=%~4!"
  144.     GOTO :MS_Set
  145.  
  146. :MS_Switch_IB
  147.  
  148.     Set "tmpStr=!tmpStr:%~2=%~4%~2!"
  149.     GOTO :MS_Set
  150.  
  151. :MS_Switch_IA
  152.  
  153.     Set "tmpStr=!tmpStr:%~2=%~2%~4!"
  154.     GOTO :MS_Set
  155.  
  156. ::: The below switch subroutines all Assign a False value to the str_Find Variable if the String Does Not Contain the Substring
  157. ::: and a True value if the String contains the Substring.
  158. ::: MS_Switch_FS is the only subroutine that Does not Modify the original String in doing So.
  159.  
  160. REM The Following subroutines are all Case Sensitive to Ensure Exact Match. If case Insensitivity is required, Change:
  161. REM (cont)  `IF "!ModStr!"=="%~2"` TO `IF /I "!ModStr!"=="%~2"`
  162.  
  163. :MS_Switch_FS
  164.  
  165.     For /L %%S in (0,1,!FullLen!) DO (
  166.         Set "ModStr=!tmpStr:~%%S,%Len%!"
  167.         IF "!ModStr!"=="%~2" (
  168.             ECHO(%~2 Found in %tmpStr%
  169.             GOTO :MS_Set
  170.         )
  171.     )
  172.     GOTO :MS_SubFalse
  173.  
  174. :MS_Switch_BK
  175.  
  176.     For /L %%S in (0,1,!FullLen!) DO (
  177.         Set "ModStr=!tmpStr:~%%S,%Len%!"
  178.         IF "!ModStr!"=="%~2" (
  179.             Set "tmpStr=!tmpStr:~0,%%S!%~2"
  180.             GOTO :MS_Set
  181.         )
  182.     )
  183.     GOTO :MS_SubFalse
  184.  
  185. :MS_Switch_BR
  186.  
  187.     For /L %%S in (0,1,!FullLen!) DO (
  188.         Set "ModStr=!tmpStr:~%%S,%Len%!"
  189.         IF "!ModStr!"=="%~2" (
  190.             Set "tmpStr=!tmpStr:~0,%%S!"
  191.             GOTO :MS_Set
  192.         )
  193.     )
  194.     GOTO :MS_SubFalse
  195.  
  196. :MS_Switch_AR
  197.  
  198.     For /L %%S in (0,1,!FullLen!) DO (
  199.         Set "ModStr=!tmpStr:~%%S,%Len%!"
  200.         IF "!ModStr!"=="%~2" (
  201.             Set "tmpStr=!tmpStr:~%%S,%FullLen%!"
  202.             Set "tmpStr=!tmpStr:~%Len%,%FullLen%!"
  203.             GOTO :MS_Set
  204.         )
  205.     )
  206.     GOTO :MS_SubFalse
  207.  
  208. :MS_Switch_AK
  209.  
  210.     For /L %%S in (0,1,!FullLen!) DO (
  211.         Set "ModStr=!tmpStr:~%%S,%Len%!"
  212.         IF "!ModStr!"=="%~2" (
  213.             Set "tmpStr=!tmpStr:~%%S,%FullLen%!"
  214.             GOTO :MS_Set
  215.         )
  216.     )
  217.     GOTO :MS_SubFalse
  218.  
  219. ::: On Success of Switch subroutines.
  220.  
  221. :MS_Set
  222.  
  223.     (
  224.     ENDLOCAL
  225.         FOR %%T IN (AR,AK,BR,BK,FS) DO (
  226.             IF /I "%switch%"=="%%T" (
  227.                 Set "Str_Find=True"
  228.             )
  229.         )
  230.     Set "%~1=%tmpStr%"
  231.     Echo(%tmpStr%
  232.     Set Len=%Len%
  233.     Set StringLen=%FullLen%
  234.     Exit /b 0
  235.     )
  236.  
  237. ::: On Not Encountering the Targeted Substring in the String during /FS /AR /AK /BR /BK Switch subroutines.
  238. ::: If the targeted substring is not found, No Modification Will be Performed.
  239.  
  240. :MS_SubFalse
  241.  
  242.     (
  243.     ECHO(%~2 Not Found in %tmpStr%
  244.     ENDLOCAL
  245.     Set "Str_Find=False"
  246.     Set Len=%Len%
  247.     Set StringLen=%FullLen%
  248.     Exit /b 1
  249.     )
  250.  
  251. REM ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: ModString Function Syntax
  252.  
  253. :MS_Syntax
  254.     ECHO(
  255.     ECHO(ModString Syntax:
  256.     ECHO(
  257.     Set "ParamError=true"
  258.  
  259.     IF "%~1"=="1" (
  260.         ECHO( Paramater 1 Missing. Must contain the Variable Name. For Use within a For Loop, Use
  261.         ECHO( 'Setlocal EnableDelayedExpansionm' prior to the Loop and Set the Loop Variable to an Indexed Variable IE:
  262.         ECHO(
  263.         ECHO(  Set /a "_i+=1"
  264.         ECHO(  Set "Var[^!_i^!]=%%%%A"
  265.         ECHO(
  266.         ECHO( And use ModString as Follows :
  267.         ECHO( If ModString is a subroutine   - CALL :ModString "Var[^!_i^!]" Substring /Switch
  268.         ECHO( If ModString is a .bat program - CALL C:\Your\File\Path\ModString.bat "Var[^!_i^!]" Substring /Switch
  269.         ECHO( If ModString is a .bat program and the path loaction is stored in your System Environment Path Variable :
  270.         ECHO( ModString "Var[^!_i^!]" Substring /Switch
  271.         exit /b
  272.     )
  273.  
  274.     IF "%~1"=="1b" (
  275.         ECHO( Paramater 1 Variable is not Defined. Variable must be set with a value prior to using ModString
  276.         exit /b
  277.     )
  278.  
  279.     IF "%~1"=="2" (
  280.         ECHO( Paramater 2 Missing. Must contain the target Substring  Ie: _2020
  281.         exit /b
  282.     )
  283.  
  284.     IF "%~1"=="3" (
  285.         ECHO( Parameter 3 - %~2. Must Contain A valid Switch to select substring modification type.
  286.         ECHO(  Valid Switches are:
  287.         ECHO(
  288.         ECHO(   /AR REMOVES   everything  AFTER the Substring, REMOVES the Substring.
  289.         ECHO(   /AK REMOVES   everything  AFTER the Substring,   KEEPS the Substring.
  290.         ECHO(   /BR REMOVES   everything BEFORE the Substring, REMOVES the Substring.
  291.         ECHO(   /BK REMOVES   everything BEFORE the Substring,   KEEPS the Substring.
  292.         ECHO(   /RS REMOVES   the SUBSTRING from the string.
  293.         ECHO(   /XS EXCHANGES the SUBSTRING with the Substring Defined in Parameter 4.
  294.         ECHO(   /IB INSERTS   new substring Defined in Parameter 4 BEFORE the substring.
  295.         ECHO(   /IA INSERTS   new substring Defined in Parameter 4  AFTER the substring.
  296.         ECHO(   /DS adds a DATE time SUFFIX to the STRING
  297.         ECHO(   /DP adds a DATE time PREFIX to the STRING
  298.         ECHO(   /FS FIND SUBSTRING in string. True / False value stored in the Variable: Str_Find
  299.         exit /b
  300.     )
  301.  
  302.     IF "%~1"=="4" (
  303.         ECHO(
  304.         ECHO( Parameter 4 Must be Defined with a String to Insert when using the /!switch!  switch.
  305.         ECHO(  Example:
  306.         ECHO(
  307.         ECHO( CALL :ModString VarName "targetstring" /!Switch! "newstring"
  308.         ECHO(
  309.         exit /b
  310.         )
  311.  
  312.     IF "%~1"=="5" (
  313.         ECHO(
  314.         ECHO(  %~2.
  315.         ECHO( Default Limit is 500 characters.Adjust limit in StringLength subroutine if required.
  316.         ECHO(
  317.     )
  318.        
  319.        
  320.  
  321. :::
  322. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: End Mod-String Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement