Batch For loop.. wildcards to determine length of searched files?

大憨熊 提交于 2019-12-24 17:50:08

问题


I'm stuck on a piece of code that I mainly didn't write myself. I'm looking for a code that runs through all files with extension .dwg, and start with K_E , and have length 9. instinctively I used wildcards, but that doesn't work..

K_E??????.dwg would do the trick in my head..but doesn't.

the reason I need this length, is that in deeper folders there are K_E??????xx.dwg and other files.

The other files in subfolders can have a range of other names, the general idea is that I only want files that are exactly named K_E[insert 6numbershere].dwg Either that, or the limit of folders depth = 2. I also tried the wildcard in the folder names to allow the code to look in only 2 deep folders for K_E*.dwg files, but that also didn't work. something like C:\Users\b00m49\Desktop\LSPTEST**\K_E*.dwg could also work..

the code is supposed to open the drawings, apply a script, and move on to the next file.

this is what I'm working with so far.

 for /r "C:\Users\b00m49\Desktop\LSPTEST" %%a in (K_E*.dwg) do ( start /wait "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")

回答1:


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\b00m49\Desktop\LSPTEST"
SET "sourcedir=U:\sourcedir"
for /r "%sourcedir%" %%a in (K_E*.dwg) do (
 FOR /f "tokens=1,6delims=\" %%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 would need to change the setting of sourcedir to suit your circumstances. I used a test directory.

The required START commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(START to START to actually start the processes.

I'm not sure what you mean by "two levels down" - I've assumed the target directory +two levels. Since your actual dirctory has 3 more levels than my test directory, you'd probably need to change 1,6 to 1,9

Simply use the tokenising feature to ensure that there are no more than ? levels of directory, where the drivename and filename count as levels. Ignore the file if there are.

Then get the name - we know the first 3 characters are K_E but we want exactly 6 characters after, so see whether there are characters 10+ and if there are not, make sure character 9 exists (characters count from 0) and if both of these conditions are valid then it must be K_E*6characters*

Could ensure that all 6 characters are numeric if required, but not actually specified.

BTW - to add information, please edit it into your question so people don't need to chase all over the thread.


@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\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")
   )
  )
 )
)
pause
GOTO :EOF

This worked for me.

Notes:

The pause line should stop the procedure after it displays the command to be executed.

You would need to change U: to C: in the setting of sourcedir to have it process your c: drive.

You would need to change ECHO(start to start in order to actually execute the autocad processing. This technique (echoing the command) is used to show the command in order to have it verified. Note I've also deliberately inserted a pair of rabbit's-ears ("") because the first quoted argument to start is taken as the window-title.




回答2:


So you just want to exclude files containing backup in their names? This should work:

@echo off
setlocal enabledelayedexpansion
for /r "C:\Users\b00m49\Desktop\LSPTEST" %%a in (K_E*.dwg) do (
    set filename=%%a
    if "!filename:backup=!"=="!filename!" (
        rem start /wait "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr"
    )
)
pause


来源:https://stackoverflow.com/questions/35886622/batch-for-loop-wildcards-to-determine-length-of-searched-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!