批处理循环,在子目录中打开固定长度的文件名 [英] Batch-Loop that opens fixed length filenames in subdirectories

查看:198
本文介绍了批处理循环,在子目录中打开固定长度的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了这么多的代码(和你们帮忙)搜索文件/ 35888287?noredirect = 1#comment59439826_35888287>这里),但它并没有做它应该做的。



批处理是打开所有K_E * .dwg文件并对这些文件执行脚本。
K_E * .dwg文件的长度应该是9.示例K_E123456.dwg。



到目前为止,这段代码打开一个dos窗口并关闭它再次..
我的文件夹结构是这样的:



C:\ Users \\\\\\\\\\\\\\\\\\\\ \\ K_E000031.dwg

\ Users \b00m49\Desktop\LSPTEST\0031\01\K_E010031.dwg



C:\ Users \b00m49\Desktop\LSPTEST\0032\00\K_E000032.dwg

C :\ Users \b00m49\Desktop\LSPTEST\0033\00\K_E000033.dwg
...



任何帮助进入运行这个代码会很棒。

我对批处理很新,所以关于步骤的解释可能会得心应手,可能会让我解决一些小问题。

b
$ b

  @ECHO OFF 
SETLOCAL ENABLEDELAYEDEXPANSION
SETsourcedir = C:\Users\b00m49\Desktop\LSPTEST
for / r%sourcedir%%% (K_E * .dwg)do(
FOR / ftokens = 1,9delims = \%% m IN(%% a)DO(
REM %% n包含dirname或文件名或空
REM %% n只在0,1或2个级别为空时为空。
IF%% n==(
SETname = %%〜na
IF!name:〜9!==IF!name: 〜8!neq(
REM名字长度不超过9个字符,但长度大于8
ECHO(start / waitC:\ Program Files \ Autodesk \ Autocad 2013 \acad.exe%% a/ bC:\Users\b00m49\Desktop\LSPTEST\expSQM.scr)




GOTO:EOF


解决方案

您可以使用这个方法,它提供了一个更好的标识所需的文件名:

  @echo off 
setlocal

setsourcedir = C:\Users\b00m49\Desktop\LSPTEST
cd%sourcedir%
for / Fdelims =% %a in('dir / S / B K_E * .dwg ^ | findstr\\K_E [0-9] [0-9] [0-9] [0-9] [0-9] [0 -9] \.dwg')do(
ECHO start / waitC:\ Program Files \ Autodesk \ Autocad 2013 \acad.exe%% a/ b C:\Users\b00m49\Desktop\LSP TEST \expSQM.scr



< r%sourcedir%%% a 命令被替换为 cd%sourcedir%和后面的 dir / S 命令;此方法处理相同的文件:源代码目录中任何级别的所有文件。文件列表通过 | 管道传递给 findstr filter 命令,允许只传递那些\K_E+6 digits +.dwg 的名字。有关更多详细信息,请键入 findstr /?。输出是由为/ Fdelims =%% a 命令。



使用开始/等待命令?其净效果与单独执行 acad.exe 命令一样...


I got this far into making a code (with you guys help on here) but it doesn't do what it's supposed to do..

The goal of the batch is to open all K_E*.dwg files and execute a script on those. The length of the K_E*.dwg files should be 9. Example K_E123456.dwg.

So far, this code opens a dos-window and closes it again.. my folders structure is like this :

C:\Users\b00m49\Desktop\LSPTEST\0031\00\K_E000031.dwg

C:\Users\b00m49\Desktop\LSPTEST\0031\01\K_E010031.dwg

C:\Users\b00m49\Desktop\LSPTEST\0032\00\K_E000032.dwg

C:\Users\b00m49\Desktop\LSPTEST\0033\00\K_E000033.dwg ...

any help into running this code would be great..

I am very new to batch, so explanation about steps may be handy and might allow me to solve small problems.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\b00m49\Desktop\LSPTEST"
for /r "%sourcedir%" %%a in (K_E*.dwg) do (
 FOR /f "tokens=1,9delims=\" %%m IN ("%%a") DO (
  REM %%n contain dirname or filename or empty
  REM %%n is only empty for 0, 1 or 2 levels down.
  IF "%%n"=="" (
   SET "name=%%~na"
   IF "!name:~9!"=="" IF "!name:~8!" neq "" (
    REM the name is not longer than 9 characters, but IS longer than 8
     ECHO(start /wait "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")
   )
  )
 )
)
GOTO :EOF

解决方案

You may use this method instead, that offer a better identification of the desired file names:

@echo off
setlocal

set "sourcedir=C:\Users\b00m49\Desktop\LSPTEST"
cd "%sourcedir%"
for /F "delims=" %%a in ('dir /S /B K_E*.dwg ^| findstr "\\K_E[0-9][0-9][0-9][0-9][0-9][0-9]\.dwg"') do (
   ECHO start /wait "" "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr"
)

The for /r "%sourcedir%" %%a command is replaced by a cd "%sourcedir%" and a posterior dir /S command; this method process the same files: all files at any level inside the source dir. The list of files is passed via a | pipe into findstr filter command, that allows to pass just those names that have "\K_E"+6 digits+".dwg" precisely. For further details, type findstr /?. The output is taken by the for /F "delims=" %%a command.

PS - Why you use start /wait command? its net effect is the same as execute the acad.exe command alone...

这篇关于批处理循环,在子目录中打开固定长度的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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