-2

Am trying to write a batch script to automatically read the PDFs using PDF names in a folder and generate 3 input files from them.

The PDF file names in the folders would in below formats :

1-2345-600B FLAT 3MM MK7 MINE.pdf
7-8910-100C FLAT 3MM MK7 MINE.pdf

I would want to create one of the input files as this format:

Part ID|Rev ID|Part Name|Dataset|datasetname|type|ref

Example:

1-2345-600|B|FLAT 3MM MK7 MINE|/Foldername/PDF/1-2345-600B FLAT 3MM MK7 MINE.pdf|1-2345-600B FLAT 3MM MK7 MINE|PDF|PDF_Reference

7-8910-100|C|FLAT 3MM MK7 MINE|/Foldername/PDF/7-8910-100C FLAT 3MM MK7 MINE.pdf|7-8910-100C FLAT 3MM MK7 MINE.pdf|PDF|PDF_Reference

Here, I was thinking to get the PDF file names seperately in a text file and then parse it to output in above format in another batch file. But, when see that it might be difficult considering the complexities in creating a line with above different parsing options from the PDF file name. Please help in a simpler way of coding this. Am new to the batch file coding world.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • 3
    Welcome at SO. Unfortunately, we are not a code writing service. However, you could start with `for /R "\Foldername" %G in ("*mine.pdf") do @for /F "tokens=1*" %g in ("%~nG") do @echo %g^|%h^|%~pnxG^|%~nxG` command for the first look. I'd recommend next required reading: http://ss64.com/nt/ and http://ss64.com/nt/syntax.html – JosefZ Sep 16 '15 at 06:33
  • 1
    your Output examples are inconsequent: should there be a `.pdf` in the fifth field or not? Please edit your question. – Stephan Sep 16 '15 at 06:39

1 Answers1

0

It's not that complicated. But I guess, for a newbie it's hard to work out the correct Syntax.

First step is analyzing the Input data. Luckily, it seems, they all have the same format and same length for the partnumber. So you can split into "PartRevision" and "rest".

As the Revision is just the last letter of the PartRevision, it's easy to split. The rest is "only" choosing appropriate variable modifiers.

As the pipe symbil (|) has a Special function in Batch, you have to escape them with a caret (^)

@echo off
setlocal enabledelayedexpansion

for /f "tokens=1,* delims= " %%a in ('dir /s /b *.pdf') do (
  set part=%%~na
  echo !part:~0,-1!^|!part:~-1!^|%%~nb^|%%~dpna %%b^|%%~na %%~nb^|PDF^|PDF_Reference
)

see for /?, set /?

for the !variable!-Syntax, see here

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91