如何对放入批处理文件的参数进行排序? [英] How to sort the arguments dropped to a batch file?
问题描述
我正在编写一个批处理文件,该文件查找并执行放置在其上的所有目录中的所有update.bat
文件.
I am writing a batch file that finds and executes all update.bat
file inside all the directories dropped onto it.
这里的问题是我希望参数(即目录的路径)按名称排序,但事实证明它们是按修改后的日期排序的.
The problem here is that I expect the arguments (i.e directories' path) comes in ordered by name but it turns out they are sorted by the modified date.
这是Windows(Windows 7)的默认行为吗?有什么建议可以解决这个问题吗?
Is this the default behavior of Windows (Windows 7)? Any suggestion to solve this?
这是我的批处理脚本:
@echo off
Setlocal EnableDelayedExpansion
if [%1]==[] goto :no_update_dropped
set LOG_FILE=update_log.txt
echo You are about to run these updates:
for %%G IN (%*) do (
if exist %%~sG\NUL echo %%G
)
pause
for %%G IN (%*) do (
if exist %%G\NUL (
if exist %%G\update.bat (
call %%G\update.bat %LOG_FILE%
) else (
echo No update.bat found in %%G.
goto :no_batch_found
)
)
)
goto :success
:no_update_dropped
echo NO UPDATE FOLDER FOUND
echo Drag and drop one or more update folder to run.
goto :exit
:no_batch_found
echo UPDATE NOT COMPLETED!
goto exit
:success
echo all updated has been run successfully
goto :exit
:exit
pause
最好的问候.
推荐答案
您可以像这样在您的for循环中对参数列表进行排序:
You can sort your argument list right in your for loop like this:
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('(for %%i in (%*^) do @echo %%~i^)^|sort') do (
set dirname=%%a
set dirname=!dirname:~0,-1!
echo use "!dirname!" without the trailing space
)
P.S.似乎sort
在字符串(WTF ????)的末尾添加了一个空格,因此您必须摆脱它.我更改了代码.
P.S. It seems like sort
appends a space to the end of string,(WTF ????) so you'll have to get rid of it. I changed the code.
最后,在 dbenham的解释的帮助下,这变成了:
Finally with the help of dbenham's explanation this becomes:
for /f "delims=" %%a in ('cmd /c "for %%i in (%*) do @echo %%~i"^|sort') do (
echo use "%%a"
)
P.P.S这应该更安全地使用逗号(当然,必须用引号引起来)
P.P.S This should work safer with commas in names (of course, they must be quoted)
for /f "delims=" %%a in ('cmd /c ^"for %%i in ^(%*^) do @echo %%~i^"^|sort') do (
echo use "%%a"
)
这篇关于如何对放入批处理文件的参数进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!