Solved for /f tokens=* delims= %%i in, internal Command

March 29, 2016 at 00:48:17
Specs: Windows 7
Hi there, i have an internal Command : for /f "tokens=* delims= " %%i in (ILS-rules.txt) do CALL :onerule %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v but the command does not recognize a comma??

Batch:
echo config firewall policy
for /f "tokens=* delims= " %%i in (ILS-rules.txt) do CALL :onerule %%i %%j %%k %%l %%m %%n %%o %%p %%q %%r %%s %%t %%u %%v
echo end
goto :EOF

:onerule
echo edit %1
echo set srcintf %2
echo set srcaddr %3
echo set dstintf %4
echo set dstaddr %5
echo set service %6
echo set comment %7
echo set action accept
echo set logtraffic all
echo set logtraffic-start enable
echo next

ILS-rules.txt:

1,vlan_24 vlan_6 vlan_8,G_hSB_ADC_ILS,any,G_hSB_ADC_ILS,ad_dc,"Domain Controller Sync",
2,vlan_24 vlan_6 vlan_8,G_hSB_ADC_ILS,any,G_hSB_ADC_ILS,ad_dc,"Domain Controller Sync",
3,vlan_24 vlan_6 vlan_8,G_hSB_ADC_ILS,any,G_hSB_ADC_ILS,ad_dc,"Domain Controller Sync",


See More: for /f tokens=* delims= %%i in, internal Command

Report •


#1
March 29, 2016 at 02:44:55
Since you pass the FOR catched parameters to a CALL statement the comma is lost because comma and space are the same delimiter for the command interpreter. You need to embrace your parameters by double quotes then refer them using the modifier ~ to strip away tne quotes, i.e.

"%%k"" then %%~k or better in your case %~3 and so on.


Report •

#2
March 31, 2016 at 01:09:09
hi ivo, thanks for your help. i think it is more logical this to implement via powershell . see here my idea:
_________________________________________

$input = Get-content C:\xxx\ILS-rules.txt
$output = "C:\xxx\output.txt"
'config firewall policy' > $output
$input | ForEach-Object {
$arr1 = $_ -split ','
If ($arr1.Count -ne '1' -And $arr1[0] -ne '') {
"edit" + $arr1[0] >> $output
$vlan = $arr1[1]
$vlan | ForEach-Object {
$arr2 = $_ -split ' '
$count = $arr2.Count -2
$vlan1 = ""
for($i=0; $i -le $count; $i++){$vlan1 = $vlan1 + " " + $arr2[$i]}
"set srcintf" + $vlan1 >> $output
"set srcaddr " + $arr2[$count +1] >> $output
}
"set dstintf " + $arr1[2] >> $output
"set dstaddr " + $arr1[3] >> $output
"set service " + $arr1[4] >> $output
"set comment " + $arr1[5] >> $output
"set action accept" >> $output
"set logtraffic all" >> $output
"set logtraffic-start enable" >> $output
"next" >> $output
}
}

________________________

What do you think about this?
Frank


Report •

#3
March 31, 2016 at 08:56:26
✔ Best Answer
Well, if it works it's hard to argue against it. Personally I'd clean it up a bit, but that's more for maintenance and readability than anything.
$input = Get-content C:\xxx\ILS-rules.txt
$output = "C:\xxx\output.txt"
$(
  'config firewall policy' 
  $input | 
  where { ![string]::IsNullOrWhiteSpace($_) } |
  ForEach-Object {
    $arr1 = $_ -split ','
    "edit" + $arr1[0] 

    $vlan = $arr1[1]
    $sep = $vlan.LastIndexOf(' ')
    "set srcintf {0}`nset srcaddr {1}" -f $vlan.Substring(0, $sep), 
                                          $vlan.Substring($sep + 1)

    "set dstintf " + $arr1[2] 
    "set dstaddr " + $arr1[3] 
    "set service " + $arr1[4] 
    "set comment " + $arr1[5] 
    "set action accept" 
    "set logtraffic all" 
    "set logtraffic-start enable" 
    "next" 
  }
) > $output

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •
Related Solutions


Ask Question