0

I need to loop through a set of files in a directory, matching a wildcard pattern and extract a portion of of the file name in between two other patterns. Unix (i.e. bash) equivalent command would be something like this:

ls -1 "$wildcard1"*"$wildcard2" | while read filename; do
string2find=`echo $filename | cut -d $delim1 -f2 | cut -d $delim2 -f1`
echo $string2find
#do something with string2find variable here
done

In batch file, I tried this

for %%f in (WILD1*WILD2) do (

echo %%f | cut -d_ -f2 | cut -d"." -f1> temp.out  <--here, need something between an "_" and a "."
type temp.out <-- see the right string in the file
set var=   <clear the variable
set /p var=<temp.out
echo %var% <-- ECHO is off message I am getting here 
del temp.out
REM need to do something with var here

)

All the examples I was able to find is devoid of piped commands.

MelBurslan
  • 2,383
  • 4
  • 18
  • 26
  • First step is delayed variable expansion. https://stackoverflow.com/questions/9102422/windows-batch-set-inside-if-not-working – avery_larry Nov 22 '19 at 03:46
  • Second step look at `for /?` to get the output of a command into a variable. – avery_larry Nov 22 '19 at 03:47
  • Third step -- certain character need to be escaped with `^` to be used inside other commands. – avery_larry Nov 22 '19 at 03:48
  • Example: `for %%I in (*_*.txt) do for /F "eol=| tokens=1* delims=_" %%J in ("%%~nI") do echo %%K` searches for non-hidden `*.txt` files containing at least one underscore and outputs the string between first occurrence of 1 or more underscores and file extension. For a file with name `Test1_File 1.txt` is output `File 1` and for a file with name `Second____File 2_Example.txt` is output `File 2_Example`. If that is the command line you need and you would like to know how this command line works in detail, let me know and I will write an answer with full explanation. – Mofi Nov 22 '19 at 06:22
  • Open a [command prompt](https://www.howtogeek.com/235101/), run `for /?` and read the whole output help. The `for /F` usage is not easy to understand for a beginner in batch file writing. So I have written lots of answers with a very detailed explanation how the batch file works using a `for /F` loop and how the string parsed by __FOR__ with option `/F` is processed step by step depending on the other used options like `eol=`, `tokens=` and `delims=`, [small list of such answers](https://stackoverflow.com/search?q=user%3A3074564+%5Bbatch-file%5D+for+delims+tokens+split). – Mofi Nov 22 '19 at 06:33

0 Answers0