批处理文件:按字母顺序(可能是数字>)处理文件

发布于 2024-08-18 13:10:46 字数 580 浏览 8 评论 0原文

我目前正在尝试使用 imagemagick 处理一堆文件 在Windows中使用批处理文件,它们都按数字编号 如下: 图片00 图片01, 图片02, ..., 图片010, 图片011, ..., 图片0100, image0101

等等,但是当我尝试处理它想要运行的文件时 image00、image01、image010、image0100、image0101、image0102 等。

我的代码如下


SETLOCAL 启用延迟扩展
设置计数=0
FOR %%a in (*.bmp) DO

IF !错误级别!==0

设置 TFILE=0!计数!
SET TFILE=地形!TFILE:~-4!.jpg
设置/计数+=1
ECHO %%a >output.txt
转换 %%a -压缩无损!TFILE!
)

有什么方法可以让它按顺序处理这些文件,目前我有一个解决方法,但这意味着当稍后使用图像时我必须不断更改一些脚本文件在。我宁愿让所有文件都具有相同的“地形”名称,并在后面添加递增的数字。

预先感谢各位!

I am currently trying to process a bunch of files with imagemagick
using a batch file in windows, they are all numbered numerically as
follows:
image00
image01,
image02,
...,
image010,
image011,
...,
image0100,
image0101

and so on, but when i try to process the file it wants to run though
image00, image01, image010, image0100, image0101, image0102 and so on.

my code is as follows


SETLOCAL EnableDelayedExpansion
SET COUNT=0
FOR %%a in (*.bmp) DO
(
IF !ERRORLEVEL!==0
(
SET TFILE=0!COUNT!
SET TFILE=Terrain!TFILE:~-4!.jpg
SET /A COUNT+=1
ECHO %%a >output.txt
convert %%a -compress LOSSLESS !TFILE!
)
)

is there any way i can make it so that it will process these files in order, for the time being i have a work-around but it means that i continually have to change some script files when the images are used later on. I would much rather have all of the files be the same 'Terrain' name with incrementing number following.

Thanks in advance guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝戈者 2024-08-25 13:10:46

您可能可以创建更多批处理代码来按顺序“排序”数字,但是,由于您可以使用 imagemagick,我想您还可以下载其他内容。我的建议是你可以尝试使用 GNU 排序(在 coreutils 中)。然后在批处理中,执行类似伪代码的操作

for ... ( dir /b *bmp | gnu_sort -n ) do (
  echo "do your stuff"
)

您可以检查 Windows 附带的排序是否具有数字排序选项。 (我上次检查它没有这个选项)。

you can probably create some more batch code to "sort" the numbers by order, however, since you can use imagemagick, i suppose you can also download other stuff. my suggestion is you can try and use GNU sort (in coreutils ). Then in your batch , do some thing like this

pseudocode :

for ... ( dir /b *bmp | gnu_sort -n ) do (
  echo "do your stuff"
)

you could probably check whether the sort that comes with Windows has a numerical sorting option. (the last time i check it doesn't have this option).

一页 2024-08-25 13:10:46

您可以将文件重命名为 image000 image001、image002、...、image010,

您可以将文件名拆分为如下所示:

@echo off 

    setlocal ENABLEDELAYEDEXPANSION 

    if not defined TRACE (
        set TRACE=REM
    )

    %TRACE% On 

    for %%a In (data\*.*)  do call :EachFile %%a 


    endlocal

goto :eof 


:EachFile %%a 
    set Name=%~n1
    @Echo %Name%

    set NUm=%Name:~6,9%
    set /a Num=Num+100000
    @echo %Num%

    echo ren %1  %~dp1Image%Num%%~x1
goto :eof 

You could rename the files to be image000 image001, image002, ..., image010,

You could split up the file name something like this:

@echo off 

    setlocal ENABLEDELAYEDEXPANSION 

    if not defined TRACE (
        set TRACE=REM
    )

    %TRACE% On 

    for %%a In (data\*.*)  do call :EachFile %%a 


    endlocal

goto :eof 


:EachFile %%a 
    set Name=%~n1
    @Echo %Name%

    set NUm=%Name:~6,9%
    set /a Num=Num+100000
    @echo %Num%

    echo ren %1  %~dp1Image%Num%%~x1
goto :eof 
浅浅淡淡 2024-08-25 13:10:46

它很难找到,因为从文档中看它并不明显。解决方案是将 FOR 命令与 dir 命令结合使用。我会做这样的事情:

@echo off
ECHO Listed in name order: %1 
ECHO ------------------------------------------------------
FOR /F "tokens=*" %%G IN ('dir /b /o:n %1') DO echo %%G

It's hard to find, because it's not really obvious from the documentation. The solution is to use the FOR command in combination with the dir command. I would do something like this:

@echo off
ECHO Listed in name order: %1 
ECHO ------------------------------------------------------
FOR /F "tokens=*" %%G IN ('dir /b /o:n %1') DO echo %%G
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文