-2

I am new to batch scripting

I am supposed to write a batch file to read a text file and two command line parameter say ,"task" and "choice".There can be two values for choice-"enable" and "disable"

Now i would want to input the file line by line and match the starting of line with "task" command line argument entered followed by a colon(:) followed by anything . Now if the choice is "enable" then i have to put ":N" in the respective lines in which the task matches if it doesnt contain a :N already

My text file would contain entries like:

24343:abc:dsd:N
233:zxzxzc
2344:cxzc:xzc

and if i run a command like

myscript.bat 2344 enable

the output of the script should be that the file should be

24343:abc:dsd:N
233:zxzxzc
2344:cxzc:xzc:N

I have been trying to write the code for this for two whole days but still havent been successful.

After all the reading,this is what i have written till now

@echo off
set /A taskname= %1
set choice= %2

FOR /F "tokens=* delims=" %%x in (testdoc.txt) do (
  echo %x%|findstr /R "^'%1'.*[^:][^N]$"
  if errorlevel 1 (echo does not contain) else (echo contains)
   )

In this,i was trying to compare line by line with the regex but it doesnt work as intended. Any help would be appreciated Thanks

Tushar Yadav
  • 55
  • 1
  • 6

1 Answers1

0

Regular expression replaces are not possible with pure usage of Windows command line interpreter cmd.exe or the console applications installed with Windows. This would require usage of a scripting language/interpreter with support for regular expression replaces in files like PowerShell or JScript which would be most likely better choices for this task.

However, a pure batch file solution is also possible for this task as it can be seen on commented batch code below with lots of extra features.

@echo off
set "TempFile=
rem Is first parameter /? for getting help?
if "%~1" == "/?" goto ShowHelp

rem Is the batch file not started with any none empty parameter?
if not "%~1" == "" (
    rem Does the first parameter not consist of only digits 0-9?
    for /F "delims=0123456789" %%I in ("%~1") do goto ShowHelp
)

rem Is there also specified a second parameter?
if not "%~2" == "" (
    rem Is the second parameter neither enable nor disable (case-insensitive)?
    if /I not "%~2" == "disable" if /I not "%~2" == "enable" goto ShowHelp
)


rem Setup a local environment for this batch file.
setlocal EnableExtensions DisableDelayedExpansion

rem Define the name of the text file without or with path to modify.
rem Define the name of the temporary file needed to modify the file.
set "TextFile=TextFile.txt"
set "TempFile=%TEMP%\%~n0.tmp"

rem Does the text file to modify exist at all?
if not exist "%TextFile%" goto MissingFile


rem Was a task number specified on starting this batch file?
if not "%~1" == "" set "TaskNumber=%~1" & goto FindTask

rem Prompt the user for the task number and make sure that the user really
rem enters a number by verifying user input using a very secure method.
:PromptNumber
set "TaskNumber="
set /P "TaskNumber=Enter task number: "
if not defined TaskNumber goto PromptNumber
setlocal EnableDelayedExpansion
for /F "delims=0123456789" %%I in ("!TaskNumber!") do endlocal & goto PromptNumber
endlocal


:FindTask
rem Does the file to modify contain the number at beginning of a
rem line as specified with first parameter and followed by a colon?
%SystemRoot%\System32\findstr.exe /B /L /M /C:"%TaskNumber%:" "%TextFile%" >nul 2>&1
if errorlevel 1 goto MissingNumber


rem Has the user specified the action to perform as second parameter.
if /I "%~2" == "enable"  set "TaskAction=1" & goto ModifyFile
if /I "%~2" == "disable" set "TaskAction=2" & goto ModifyFile

rem Prompt the user for the action to perform.
%SystemRoot%\System32\choice.exe /N /M "Press Y to enable or N to disable task: "
set "TaskAction=%ERRORLEVEL%"


rem Copy the file with ignoring empty lines and lines starting with a
rem semicolon to temporary file with modifying all lines starting with
rem the specified task number according to specified action to perform.
rem But delete the temporary file before if existing by chance.
:ModifyFile
del "%TempFile%" 2>nul
set "FileModified="
for /F "usebackq tokens=1* delims=:" %%I in ("%TextFile%") do (
    if not "%%I" == "%TaskNumber%" (
        echo %%I:%%J>>"%TempFile%"
    ) else (
        set "TextLine=%%I:%%J"
        call :ModifyLine
    )
)

rem Was no line modified on copying all the lines to temporary file?
if not defined FileModified del "%TempFile%" & goto EndBatch

rem Move the temporary file over the text file to modify.
move /Y "%TempFile%" "%TextFile%" 2>nul

rem Was the text file overwritten by command MOVE?
if not errorlevel 1 goto EndBatch

rem Inform the user that the text file to modify could not be
rem modified because of being read-only or missing appropriate
rem NTFS permissions or a sharing access violation occurred.
del "%TempFile%"
for /F %%I in ("%TextFile%") do set "TextFile=%%~fI"
echo/
echo ERROR: "%TextFile%" could not be modifed.
echo/
echo Please make sure the file has not read-only attribute
echo set, is not opened in any application and you have
echo the necessary permissions to overwrite this file.
goto HaltBatch


rem This is a subroutine which modifies a line with right task
rem number according to action to perform and outputs this line
rem into the temporary file. It records also if the line needed
rem to be modified at all.
:ModifyLine
if %TaskAction% == 1 (
    if not "%TextLine:~-2%" == ":N" (
        set "TextLine=%TextLine%:N"
        set "FileModified=1"
    )
) else (
    if "%TextLine:~-2%" == ":N" (
        set "TextLine=%TextLine:~0,-2%"
        set "FileModified=1"
    )
)
>>"%TempFile%" echo %TextLine%
goto :EOF


rem Get name of file with full path which works also for not existing
rem file and inform the user about missing file to modify with full
rem path to see also where this batch file expected it on execution.
:MissingFile
for /F %%I in ("%TextFile%") do set "TextFile=%%~fI"
echo/
echo ERROR: "%TextFile%" does not exist.
goto HaltBatch


:MissingNumber
rem The specified number does not exist in the file to modify
rem at beginning of a line. Inform the user about this error.
echo/
echo ERROR: %TaskNumber% not found in file "%TextFile%".
goto HaltBatch


:ShowHelp
echo/
echo Usage: %~nx0 [task] [disable ^| enable]
echo/
echo        task ...... number of the task to enable or disable.
echo        disable ... disable the specified task.
echo        enable .... enable the specified task.
echo/
echo %~nx0 can be also started without any parameter.
echo In this case the task number and the action to perform
echo can be entered during the execution of the batch file.

:HaltBatch
echo/
pause
echo/

:EndBatch
if defined TempFile endlocal

The command line set "TextFile=TextFile.txt" must be modified to your environment.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • choice /?
  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

Further read following:

Mofi
  • 46,139
  • 17
  • 80
  • 143