阅读目录中的文件与文件名批量preFIX的顺序? [英] Read files in directory in order of filename prefix with batch?

查看:64
本文介绍了阅读目录中的文件与文件名批量preFIX的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些SQL脚本与一些的,例如pfixed目录,$ P $。

I have a number of SQL scripts in a directory, prefixed with a number, e.g.

目前,我能够通过他们这个脚本循环:

Currently, I'm able to loop through them with this script:

@ECHO OFF
FOR /r . %%F IN (*.sql) DO (
    ECHO File:"%%F"
)

然而,由于该Windows读取目录中的文件的方式,10和11 2前阅读等。

However, because of the way that Windows reads files in a directory, 10 and 11 are read before 2, etc.:

我如何遍历每个文件的目录,在preFIX 的顺序?

How can I loop through each file in the directory, in order of the prefix?

推荐答案

在需要它的文件名下方插入前导零的批处理文件。

The Batch file below insert a leading zero in filenames that need it.

@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%f in ('dir /B *.sql') do (
   set "filename=%%f"
   if "!filename:~1,1!" equ "-" ren "%%f" "0%%f"
)

修改:新溶液

EDIT: New solution added

下面的批处理文件显示正确的顺序文件,而无需将其重命名。

The Batch file below show the files in right order without rename them.

@echo off
setlocal EnableDelayedExpansion
rem Create an array with filenames in right order
for %%f in (*.sql) do (
   for /F "delims=-" %%n in ("%%f") do (
      set "number=00000%%n"
      set "file[!number:~-6!]=%%f"
   )
)
rem Process the filenames in right order
for /F "tokens=2 delims==" %%f in ('set file[') do (
   echo %%f
)

这篇关于阅读目录中的文件与文件名批量preFIX的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆