Windows上下文菜单&批量 - >编辑所选文件

时间:2017-10-24 08:51:30

标签: windows batch-file contextmenu

我有一个批处理文件应该像这样工作:

  • 搜索特定的txt文件
  • 如果找到文本,请删除包含文本的行
  • 如果有2个或更多空格将它们转换为1个空格
  • 将文件扩展名更改为特定的

Basicaly我编辑一个由不同程序生成的文件并将其转换,以便我自己的程序可以读取它。 (这意味着每次都有不同的名称和扩展名)

我创建了一个新的txt文件并复制&从原始文件粘贴到txt文件,然后运行批处理文件。

我想跳过创建新文件的部分,并将批处理文件添加到Windows上下文菜单中,这样我就可以右键单击原始文件并转换文件。但我不知道如何将原始文件设置为要编辑的文件。

(我还需要在Windows XP上运行)

Curent代码:

@ECHO OFF
setlocal enabledelayedexpansion

::Create txt file
set "fisiertxt=%CD%\New Text File.txt"
if not exist "%fisiertxt%" REM. > "New Text File.txt"

::Start txt file
start notepad "New Text File.txt"

::check if file is still open
:verifica
tasklist /FI "IMAGENAME eq Notepad.exe" 2>NUL | find /I /N "Notepad.exe" >NUL
if "%ERRORLEVEL%"=="1" goto Aici 
goto verifica

::Delete file if its empty
:Aici

>nul findstr "^" "New Text File.txt" || del "New Text File.txt"

:: check if there are delimitators delete them
find /c "99999 0 0 0" "New Text File.txt"
if "%ERRORLEVELs%"=="1" goto FaraSeparator
pause

:: if yes make a new file winought them


set count=1
for /f "delims=" %%A in ('find /V /I "99999 0 0 0" "New Text File.txt"') do (
    if !count!==1 echo.>document.txt
    if !count! GTR 1 echo %%A>>document.txt
    set /A count=!count!+1)



::
:: delete the exit file
:: delete the first row (the exit file has an emplty first row)
:: replace 2 or more spaces with 1 space
::

DEL Drumuire.mnu   2>nul /F /Q
:: replace with ->
SET delim= 
:: set number of lines to delete 
FOR /f "skip=1delims=" %%i IN (document.txt) DO (
    SET line=%%i
    (SET newline=)
    SET count=0
    CALL :SCHIMBARI
)

GOTO :eof

:SCHIMBARI
SET c1=%line:~0,1%
SET line=%line:~1%
IF "%c1%"==" " (SET /a count+=1) ELSE (
    IF %count%==0 (SET newline=%newline%%c1%) ELSE (
        IF %count%==1 (SET newline=%newline% %c1%) ELSE (
            SET newline=%newline%%delim%%c1%)
    SET count=0
    )
)
IF DEFINED line GOTO SCHIMBARI
::
:: You may want to preserve trailing spaces
:: or convert them...
::
IF %count%==0 GOTO SCOATE
IF %count%==1 SET newline=%newline% &GOTO SCOATE
SET newline=%newline%%delim%
:SCOATE
>>Drumuire.mnu ECHO %newline%
DEL document.txt
goto done
:: 
:: --------------------------------------------------------------------------
::


:winought delimitator
DEL Drumuire.mnu   2>nul /F /Q
:: replace with ->
SET delim= 
:: set number of lines to delete 
FOR /f "usebackq delims=" %%i IN ("New Text File.txt") DO (
    SET line=%%i
    (SET newline=)
    SET count=0
    CALL :PRESCHIMBA
)

GOTO :eof

:PRESCHIMBA
SET c1=%line:~0,1%
SET line=%line:~1%
IF "%c1%"==" " (SET /a count+=1) ELSE (
    IF %count%==0 (SET newline=%newline%%c1%) ELSE (
        IF %count%==1 (SET newline=%newline% %c1%) ELSE (
            SET newline=%newline%%delim%%c1%)
    SET count=0
    )
)
IF DEFINED line GOTO PRESCHIMBA
::
:: You may want to preserve trailing spaces
:: or convert them...
::
IF %count%==0 GOTO PRINT
IF %count%==1 SET newline=%newline% &GOTO PRINT
SET newline=%newline%%delim%
:PRINT
>>Drumuire.mnu ECHO %newline%


:done

1 个答案:

答案 0 :(得分:0)

正如评论中提到的@Stephan,您可以使用以下步骤完成目标。

1:使用参数/参数(docs

编写批处理文件
  

<强> context.bat

@echo off
rem set up a couple variables for easier modification
set "tfile=New Text File.txt"
set "searchstring=9999"

rem output some things for information purposes
echo Source File: %1
echo Target File: %tfile%
echo Target Folder: %cd%

rem copy the file
echo Copying file...
copy %1 "%tfile%"

rem check for the search string defined above
echo Checking for search string...
find /c "%searchstring%" "%tfile%" >NUL
if "%ERRORLEVEL%"=="0" GOTO FOUNDSTRING
if "%ERRORLEVEL%"=="1" GOTO NOTFOUND

:NOTFOUND
echo Did not find search string (%searchstring%)
GOTO ENDER

:FOUNDSTRING
echo Found search string (%searchstring%)
GOTO ENDER

:ENDER
rem remove the next line once everything is working so you don't have to press a key each time it runs
pause

2:添加到Windows&#34;发送到&#34;:

copy context.bat %appdata%\Microsoft\Windows\SendTo

3:右键单击,发送至&gt; context.bat,利润!

You targeted D:\test\test.txt!
Press any key to continue . . .