将文件拖放到批处理文件上?

huangapple go评论57阅读模式
英文:

drag and drop files onto the batch file?

问题

这是我改过的:

@Echo off

setlocal enabledelayedexpansion

set "batchDir=%~dp0"
cd /d "%batchDir%"

for %%v in (%*) do (
    if exist "%%v" (
        set "filePath=%%v"
        set "fileName=%%~nxv"
        set "outputFile=!fileName!_new"
        idosomething -f "!filePath!" -o "!outputFile!"
    )
)

pause
英文:

I have a script that looks like this :

@Echo off

cd C:\Users\Example\Subexample
for /r "C:\Users\Myname\Downloads" %%v in (*.txt) do idosomething -f "%%v"

pause

this first tells current directory changes to Subexample where idosomething.exe is located
and it does the recursive operation for all text files in Downloads folder. the output file is then saved in the same location with suffix "_new" automatically attached by the program.

now, how can I just drag and drop one or more text files from any directories onto batch file and make it do the same operation? If possible, the batch file should be able to be put in any folder of my liking.

thank you!

答案1

得分: 1

首先,在%USERPROFILE%目录或其他您选择的目录中创建一个批处理文件,文件名可以是ProcessTextFiles.cmd或其他您选择的文件名,内容如下:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not "%~1" == "" goto ProcessArgs

echo %~nx0 must be run with one or more folder paths
echo or with one or more text file names as argument(s).
echo(
pause
goto EndBatch

:ProcessArgs
set "ExeFileName=C:\Full Path\Executable.exe"
for %%I in (%*) do (
    if exist "%%~fI\" (
        echo Processing all .txt files in directory structure beginning from:
        echo "%%~fI"
        for /F "eol=| delims=" %%J in ('dir "%%~fI\*.txt" /A-D-L /B /S 2^>nul') do "%ExeFileName%" -f "%%J"
    ) else if /I "%%~xI" == ".txt" (
        if exist "%%~fI" (
            echo Processing file: "%%~fI"
            "%ExeFileName%" -f "%%~fI"
        ) else (
            echo Skipping not existing file: "%%~fI"
        )
    ) else echo Skipping argument: "%%~I"
)

:EndBatch
endlocal

然后,打开Windows文件资源管理器,浏览到目录%SystemRoot%\System32(Windows系统目录),右键单击文件cmd.exe,选择“发送到”->“桌面(创建快捷方式)”。

接着,最小化Windows文件资源管理器窗口,找到桌面上新创建的快捷方式文件,右键单击并选择“重命名”以更改文件名为有意义的名称。然后右键单击已创建并重命名的快捷方式文件,选择“属性”,在“目标”属性后追加一个空格和/D /C "%USERPROFILE%\ProcessTextFiles.cmd"

还应该输入有意义的“注释”。在“发送到”子菜单中将显示该快捷方式文件的名称。最后,使用<kbd>Ctrl+X</kbd>剪切修改后的快捷方式文件,在Windows文件资源管理器中导航到%APPDATA%\Microsoft\Windows\SendTo目录,并使用<kbd>Ctrl+V</kbd>粘贴快捷方式文件。

Windows文件资源管理器中选择一个或多个目录和/或.txt文件,右键单击,选择“发送到”子菜单中的快捷方式文件名,即可启动Windows命令处理器以处理文件管理器中选择的所有目录和文件名作为cmd.exe传递给批处理文件的附加参数。

英文:

Create first a batch file in directory %USERPROFILE% or any other directory of your choice with a file name like ProcessTextFiles.cmd or any other file name of your choice with the following command lines:

<!-- language: lang-none -->

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not &quot;%~1&quot; == &quot;&quot; goto ProcessArgs

echo %~nx0 must be run with one or more folder paths
echo or with one or more text file names as argument(s).
echo(
pause
goto EndBatch

:ProcessArgs
set &quot;ExeFileName=C:\Full Path\Executable.exe&quot;
for %%I in (%*) do (
    if exist &quot;%%~fI\&quot; (
        echo Processing all .txt files in directory structure beginning from:
        echo &quot;%%~fI&quot;
        for /F &quot;eol=| delims=&quot; %%J in (&#39;dir &quot;%%~fI\*.txt&quot; /A-D-L /B /S 2^&gt;nul&#39;) do &quot;%ExeFileName%&quot; -f &quot;%%J&quot;
    ) else if /I &quot;%%~xI&quot; == &quot;.txt&quot; (
        if exist &quot;%%~fI&quot; (
            echo Processing file: &quot;%%~fI&quot;
            &quot;%ExeFileName%&quot; -f &quot;%%~fI&quot;
        ) else (
            echo Skippig not existing file: &quot;%%~fI&quot;
        )
    ) else echo Skipping argument: &quot;%%~I&quot;
)

:EndBatch
endlocal

This batch file executes a short usage help on running it without any argument string like on double clicking on this batch file. The usage help should be extended to describe what this batch file is for and how to use it.

Start next Windows File Explorer and browse to directory %SystemRoot%\System32 (Windows system directory) and click with secondary (usually right) pointing device (mouse) button on the file cmd.exe to open the context menu for the Windows Command Processor which is processing batch files like the above one.

Click with primary (usually left) pointing device button on the item Show more options on Windows 11 and click next on any Windows in submenu Send to on the context menu item Desktop (create shortcut). Then minimize Windows File Explorer window to see your desktop with the new shortcut file with name cmd.exe or cmd.exe - Shortcut depending on version of Windows.

Click with secondary pointing device button on the newly created shortcut file and (after clicking on Show more options on Windows 11) click on context menu item Rename to change the name of the shortcut file to something meaningful. The name of the shortcut file is later the name of a context menu item in context submenu Send to.

Click with secondary pointing device button on the newly created and already renamed shortcut file and click on the context menu item Properties to open Properties dialog window for the shortcut.

Modify the property Target by appending a space character and /D /C &quot;%USERPROFILE%\ProcessTextFiles.cmd&quot;

There should be entered also a meaningful Comment. There can be selected a nice icon displayed later in the context submenu Send to and of course all other properties on the other tabs to whatever you like before closing the dialog window with button OK to save all changed properties and close the window.

Cut the modified shortcut file with <kbd>Ctrl+X</kbd>, restore the Windows File Explorer window, browse to the directory %APPDATA%\Microsoft\Windows\SendTo and paste with <kbd>Ctrl+V</kbd> the shortcut file.

There can be selected in Windows File Explorer (or any other file manager with Explorer context menu support) one or more directories and/or .txt files before opening the context menu with secondary pointing device button and (after clicking on Show more options on Windows 11) clicking in submenu Send to on the context menu item with name of the shortcut file. Then explorer.exe (or any other file manager executable) starts the Windows Command Processor to process the specified batch file with all names of directories and files selected in file manager appended as additional arguments passed by cmd.exe to the batch file.

Please note that there is a command line length limitation which means it is not possible to select thousands of .txt files and process them all with a single execution of the batch file.

The batch file supports also file/folder arguments with relative paths for manual execution from within a command prompt window or for calling it from another batch file with one or more file/folder names.

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

  • call /? ... explains %~nx0 and %*
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • set /?
  • setlocal /?

答案2

得分: 0

不确定我是否理解您的问题,但这种方法允许处理拖放到.bat文件上的一个或多个文件名:

@echo off

cd /d C:\Temp

:loop
if exist "%1" (
  echo %1
  shift
  goto loop
)

cd
pause
英文:

Not sure I got your question right, but this approach would allow processing one or more filenames dragged onto the .bat file:

@echo off

cd /d C:\Temp

:loop
if exist &quot;%1&quot; (
  echo %1
  shift
  goto loop
)

cd
pause

huangapple
  • 本文由 发表于 2023年3月12日 16:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定