Batch-Loop that opens fixed length filenames in subdirectories

回眸只為那壹抹淺笑 提交于 2020-01-07 02:01:31

问题


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

回答1:


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...




回答2:


I'm new to this website, and didn't notice that someone edited his comment into something that works..

I don't completely understand how, but here's the code.

@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



回答3:


Change

 ECHO(start /wait "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")

to

 start /wait "" "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")

or to

 start "" "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")

which should run all of the conversions in parallel.



来源:https://stackoverflow.com/questions/35897863/batch-loop-that-opens-fixed-length-filenames-in-subdirectories

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