0

Sorry if this seems like a simple question, but I could not find the right answer anywhere....

I have a set of files in a folder with this naming convention.... tablename_date_time.dat

I need to loop thru the folder and extract the tablename (up to the underscore) and use it in a bcp command, what would be the best way to do this?

So far I have the command, but I can't figure out the substr part.

for /f %%i in (%1) do (
    bcp tablename IN C:\temp\%%i_.dat -n -T
)

Thank you for your help.

Steve
  • 1,028
  • 7
  • 25
  • 42
  • Please, do not use abbreviations! – Hack5 Jun 13 '17 at 13:00
  • Possible duplicate of [Iterate all files in a directory using a 'for' loop](https://stackoverflow.com/questions/138497/iterate-all-files-in-a-directory-using-a-for-loop) – Hack5 Jun 13 '17 at 13:06
  • @Penn - Not a duplicate of that; the user is looking for a way to parse out _part_ of the file name. – Jeff Zeitlin Jun 13 '17 at 13:08
  • @Penn May I ask what abbreviations you are referring to? – Steve Jun 13 '17 at 13:16
  • @JeffZeitlin OK, sounds like he wants a regex. Can't find any docs on how though... – Hack5 Jun 13 '17 at 13:17
  • @Steve "Thru", in the question and title. – Hack5 Jun 13 '17 at 13:18
  • It does look like a regular expression is needed, though depending on how the tablenames are included, it may be possible to use a numeric substring. There is useful information on taking substrings by position and length (rather than character matching) at [SS64](https://ss64.com/nt/syntax-substring.html); beyond that, third-party regular-expression software - `grep` or `awk` are the most likely candidates - will be needed. (Alternatively, the batch file can be completely replaced by a PowerShell script, if the querent finds this acceptable.) – Jeff Zeitlin Jun 13 '17 at 13:24
  • Or a cmd that creates a VBScript, runs it and deletes it again, as [here](https://stackoverflow.com/a/10790213/5509575) – Hack5 Jun 13 '17 at 13:25
  • 1
    Maybe `for /F "delims=_" %%G in ("%%~i") do bcp %%G IN C:\temp\%%i_.dat -n -T`. Hard to answer without knowing `%1`. Please [edit] your question and provide a [mcve]. – JosefZ Jun 13 '17 at 14:37

0 Answers0