Batch Files: Processing files in alphbetical (Numerical maybe>) Order
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, ima开发者_StackOverflow中文版ge011, ..., 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!
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).
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
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
精彩评论