Search through command line arguments.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
schweigk
Posts: 3
Joined: 08 Oct 2019 06:25

Search through command line arguments.

#1 Post by schweigk » 08 Oct 2019 06:54

Hello, I`ve created a batch file where the user can drag and drop a cad file onto it, and it will be translated in to a 3Dpdf.
All of this will be done with a batch file in combination other text files.

But the 3Dpdf translator can also attach other files to (into) the 3Dpdf.
So I intend that the user can drag and drop multiple files on the batch file.
And then I have to search through %1 until however many files where dragged (?).

Firstly I have to find the file with an *.prt extension to define which cad file should be translated (files seem to be ordered alphabetically) and set a variable (e.g. CAD_FILE).
Two files with the *.prt extension are not allowed so I have to give an error if two cad files (*.prt extension) are dragged onto the batch file.

All other files have to be written in to an attachments.txt file with a line break after each file.

I have tried many things but I cannot find a working solution.
I`m thankful for any help or Advice.
Br

aGerman
Expert
Posts: 3706
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Search through command line arguments.

#2 Post by aGerman » 08 Oct 2019 11:15

This piece of code should meet your requirements:

Code: Select all

@echo off &setlocal
if "%~1"=="" exit /b

set /a "prt=0, other=0"
>"attachments.txt" (
  for %%i in (%*) do (
    if /i "%%~xi"==".prt" (
      set "CAD_FILE=%%~i"
      set /a "prt+=1"
    ) else (
      set /a "other+=1"
      echo %%i
    )
  )
)

if %prt% neq 1 (
  del "attachments.txt"
  echo ERROR exactly one file with extension .prt required
  pause
  exit /b
)

if %other%==0 del "attachments.txt"

echo prt file: "%CAD_FILE%"
echo %other% files in "attachments.txt"

pause
Steffen

schweigk
Posts: 3
Joined: 08 Oct 2019 06:25

Re: Search through command line arguments.

#3 Post by schweigk » 08 Oct 2019 22:37

Hello Steffen,

that`s exactly what I needed.
I still have a lot to learn with batch files.

Thank you very much.

Post Reply