简体   繁体   English

如何制作批处理文件中删除的参数列表?

[英]How to make a list of arguments which is dropped on a batch file?

I'm trying to create a list of all of the files that are dragged and dropped onto a script, separated by semicolons. 我正在尝试创建所有文件的列表,这些文件被拖放到脚本上,并用分号分隔。

set b =
FOR %%a IN (%*) do (
    set "b=%b%%%a;"
) > test.tmp
echo b
pause

This is what I have so far, but the script is not displaying the list of files. 这是我到目前为止,但脚本没有显示文件列表。 What am I doing wrong? 我究竟做错了什么?

based on this : 基于

sample code in the file F:\\printArguments.bat may be like that: 文件F:\\printArguments.bat中的示例代码可能是这样的:

FOR %%a IN (%*) do (
    CALL:PrintTheArgument %%a
)
pause
GOTO:EOF

:PrintTheArgument

echo. %~1

GOTO:EOF

You can test it for example like that: 你可以测试它,例如:

F:\printArguments.bat "edek" "z" "fabryki" "kredek"

To echo the full paths and filename of the files you are to drag/drop, without double quotes: echo拖放的文件的完整路径和文件名,请不要使用双引号:

@echo off
(for %%a in (%*) do (
   echo %%~a;
  )
)> test.tmp
type test.tmp
pause

To include double quotes and full path: 要包含双引号和完整路径:

@echo off
(for %%a in (%*) do (
   echo %%a;
  )
)> test.tmp
type test.tmp
pause

to echo filename only, without path: 仅回显文件名,不带路径:

@echo off
(for %%a in (%*) do (
   echo %%~nxa;
  )
)> test.tmp
type test.tmp
pause

and to echo filename only, excluding path or extension: 并且仅echo文件名,不包括路径或扩展名:

@echo off
(for %%a in (%*) do (
   echo %%~na;
  )
)> test.tmp
type test.tmp
pause

type test.tmp is just to show you the content of the file after written, you can remove it if you do not need it. type test.tmp只是为了向您显示写入后的文件内容,如果不需要,可以将其删除。

I suggest you also read the help for for command: 我建议你也看到了帮助for命令:

  • for /?

Lastly, if you really want to set a variable (though not needed) you need to enabldelayedexpansion or call echo %%b%% by doubling the % 最后,如果您确实想设置一个变量(虽然不是必需的),则需要enabldelayedexpansion或通过将%加倍来call echo %%b%%

@echo off
setlocal enabledelayedexpansion

set "b="

for %%a in (%*) do (
    set "b=!b!%%a;"
)

if defined b (
    > test.tmp echo(!b:~0,-1!
)

pause

To append to b in the for loop, delayed expansion may be needed. 要在for循环中附加到b ,可能需要延迟扩展。 Use ! ! instead of % for delayed expansioned varaibles. 而不是延迟扩展的变量的% To trim the trailing ; 修剪尾随; , first check if the variable is defined, then trim the last character by using -1 in !b:~0,-1! ,请先检查是否定义了变量,然后在!b:~0,-1!使用-1修剪最后一个字符!b:~0,-1! , which gets all characters from position zero to the 2nd last character. ,它从位置0到第二个最后一个字符获取所有字符。

Note: set b = is variable name of b with a space. 注意: set b =是带有空格的b变量名。

View set /? 视图set /? about how to get substrings from strings. 关于如何从字符串中获取子字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM