Windows 上下文菜单和批处理 - >编辑所选文件



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

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

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

我创建一个新的txt文件

,从原始文件复制并粘贴到txt文件,然后运行批处理文件。

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

(我也需要在Windows XP上运行它)

库伦特代码:

@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

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

1:使用参数/参数(文档)编写批处理文件

上下文.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:添加到窗口"发送到":

copy context.bat %appdata%MicrosoftWindowsSendTo

3:右键单击,发送到>上下文.bat,利润!

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

最新更新