开发者

File names not sorted

setlocal EnableDelayedExpansion
set "var=xxxxxxxxxxx\"
Test.txt echo !var!
set "var1=$(LOCAL_PATH)/batch/"
set "var2=:/data/local/12m/batch/"
set "var3=$(LOCAL_PATH)/"
set "var4=:/data/local/12m/"

for %%a in (batch/*.bat) do (
    >>Test.txt echo !var1!%%a!var2!%%a \
) 

for %%a in (*.bmp) do (
    >>Test.txt echo !var3!%%a!var4!%%a \
) 

for %%a in (*.wav) do (
    >>Test.txt echo !var3!%%a!var4!%%a \
) 

for %%a in (*.cfg) do (
    >>Test.txt echo !var3!%%a!var4!%%a \
) 

problem : the filenames should be in sorted order in Test.txt but it is not(The .bat files should be in sorted order followed by .bmp files in sorted order etc). How to fix this?

Note: There is a >> operator in front of Test.txt everywhere but for some reason this editor removed it... e.g it is >>test.txt echo !var1"

EDIT by jeb: You should use code formatting and read the How to Format help at the right side.

Thanks SSE @jeb : I noticed that the files are sorted if I run the batch file in my local drive/folder and the files are not sorted if I run the batch file in a mapped drive. I need this to work in my mapped drive. Copying contents to individual output files per file type and then copying all the contents to one single file also does not sort files in mapped drive. I am using windows 7. Is this a windows 7 issue? The map开发者_运维知识库ped drive is a map of my local workspace on a linux server. I believe that it follows ext2 file system.

@Niel @Robert Harvey : I tried for /F "delims=" %%a in ('dir /b /on batch\*.bat') do, but it does not copy any batch file at all... my output file is empty


for doesn't sort your file names. You could create (four) temporary files and sort the result and output that to your final file. Or you could use extended for syntax to read the output of dir/b/o.


Perhaps your files have numbers as names, then they are also sorted on a stringcompare basis, they aren't sorted numerical.

Sample. 2 is greater then 10, as they are compared as strings.
So a normal (and correct) sorted list could be

  • 1
  • 10
  • 12
  • 2
  • 20

If your sort fails at another point, please show some samples


setlocal EnableDelayedExpansion
set "var=xx"
>>Test1.txt echo !var!
set "var1=yy"
set "var2=zz"
set "var3=aaa"
set "var4=bb"
set "var5=cc"

for %%a in (batch/*.bat) do (
>>Test2.txt echo !var1!%%a!var2!%%a \
)
for %%a in (*.bmp) do (
>>Test3.txt echo !var3!%%a!var4!%%a \
)
for %%a in (*.wav) do (
>>Test4.txt echo !var3!%%a!var4!%%a \
) 
for %%a in (*.cfg) do (
>>Test5.txt echo !var3!%%a!var4!%%a \
) 
@echo off
sort Test2.txt>>Test1.txt
sort Test3.txt>>Test1.txt
sort Test4.txt>>Test1.txt
sort Test5.txt>>Test1.txt

setlocal EnableDelayedExpansion
>>Test1.txt echo !var5!

del Test2.txt
del Test3.txt
del Test4.txt
del Test5.txt

Now am writign to five different files and am sorting each file before appending to the first one. This works for me now. Any sugegestion to improve this code is welcome

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜